WTF is Docker?

Written by nitin_l | Published 2019/03/20
Tech Story Tags: whats-docker | docker-explained | docker | wtf-is-docker | programming

TLDRvia the TL;DR App

A brief explanation of Docker and Docker compose in English

Contents:

  • What is docker?
  • Deploying a Node.js app in Docker
  • What is docker compose and how to use it?

What is Docker?

Docker is a tool that lets you deploy apps in containers. Containers are lightweight virtual machines.

You can create a linux container, setup your app in it and share the container with others.

It is like sharing a laptop where the project is already setup and running.

Portability is a huge advantage of Docker. There are several other benefits of using Docker. For now, let’s go ahead and learn how to use it.

Deploying a Node.js app in Docker

Let’s create a simple Node.js app and deploy it in Docker.

The nodejs site has a brilliant example, I’ll follow the same steps here:

Create the Node.js app

  1. In a new directory, create a package.json file that describes your app and its dependencies:

<a href="https://medium.com/media/09bb3dc9f5c618f9e47154fd2135121e/href">https://medium.com/media/09bb3dc9f5c618f9e47154fd2135121e/href</a>

Run the command npm install to install the dependencies listed above.

2. Create a server.js file that defines a web app using Express.js framework.

<a href="https://medium.com/media/6dddf73843041250e66d66e88c37adc3/href">https://medium.com/media/6dddf73843041250e66d66e88c37adc3/href</a>

The app is ready, run the command npm start to run it. Open http://0.0.0.0:8080 in the browser to view your app.

Dockerize the app

  1. Install Docker 🐳
  2. Create a file named Dockerfile. This defines your container.

<a href="https://medium.com/media/a1b1fca3a354eb93cc79df51d0cbf642/href">https://medium.com/media/a1b1fca3a354eb93cc79df51d0cbf642/href</a>

What’s going on in this file?

FROM node:8 — This is the base image. Image is a snapshot/blueprint of a container. A running docker image is a container. node image is a linux image with node.js and npm installed.

In this Dockerfile, we use the node image and make some additions to it to create a new image.

Let’s go through the additions we’re making to the node image:

  • WORKDIR /usr/src/app : Create a folder named app in the image and set it as the current working directory.
  • COPY package*.json ./ : Copy package.json and package-lock.json files from your computer (called Docker host) into ./ folder in the image (which is set to/usr/src/app in step 1).
  • RUN npm install : This will run the given command in the image.
  • CMD npm start : CMD defines the entrypoint command that is run when you start a container from this image. An image can have multiple RUN commands, but only one CMD.

Now that your image is defined, let’s build it.

3. Building the image 🛠 : $ docker build -t node-app-image .

This command will download(pull) the node image from Docker Hub, which is a public library (registry) of docker images.

For each line in Dockerfile, an intermediate image is built. The final image is created after running the whole Dockerfile.

You can now view all images on your system:

$ docker images

REPOSITORY               TAG       IMAGE ID            CREATED
node                     8         2a3ba2f43d22        2 minutes ago
node-app-image           latest    53eeb30b855e        1 minute ago

4. DEPLOY!! 🚀

Run the image: $ docker run -p 41122:8080 -d node-app-image

Port 8080 of the container is mapped to port 41122 of your system, open http://localhost:41122 in browser to view the app.

Try these commands:

# List the running containers
$ docker ps

# Print logs of a container (use container id from above command)
$ docker logs 8473137bc8e1

# Go inside the running container
$ docker exec -it <container_id> /bin/bash

# Command to return outside: exit

# Stop a container
$ docker stop <container_id>

# List all containers (including stopped ones)
$ docker ps -a

# Remove stopped container
$ docker rm <container_id>

# Remove an image
$ docker rmi node-app-image

Congratulations! 🎉 🎉 🎉

Take a breather. Here’s what you’ve learnt so far:

  1. Cool phrases — container, image, registry, docker hub, Dockerfile
  2. Mystic arts — How to write Dockerfiles and create images.
  3. Witchcraft— spells to control containers

Now that you know WTF is Docker, let’s go the next step:

WTF is Docker compose?

Docker compose is used to handle many containers.

Say you need a mysql db and redis along with the node.js app, what would you do?

Create 3 Dockerfiles, build 3 images and run 3 images. — humans have evolved beyond this.

Instead, create a file docker-compose.yml :

<a href="https://medium.com/media/d0798e568827263424da713e4128be8b/href">https://medium.com/media/d0798e568827263424da713e4128be8b/href</a>

Now run docker-compose up -d and watch the magic happen.

docker ps will show 3 containers running.

Try these commands:

# Stop the entire stack
$ docker-compose stop

# Stop and remove all containers
$ docker-compose down

# Start a specific service
$ docker-compose up -d redis

Docker compose makes it super easy to manage your project if it has multiple containers.

You made it!!

Photo by David Beatz on Unsplash

Welcome to the world of docker.

Further readings:

  1. Best practices for writing Dockerfiles — A very good read that will tell you more about how things work in Docker.
  2. Official Docker docs — They are pretty good once you know the basic concepts.

Published by HackerNoon on 2019/03/20