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.
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:
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.
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.
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:
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:
Now that you know WTF is Docker, let’s go the next step:
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 :
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.
Welcome to the world of docker.