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 are lightweight virtual machines. containers 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 , I’ll follow the same steps here: example Create the Node.js app 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 framework. Express.js <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 in the browser to view your app. http://0.0.0.0:8080 Dockerize the app 🐳 Install Docker Dockerfile. This defines your container. Create a file named <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. is a snapshot/blueprint of a container. node image is a linux image with node.js and npm installed. Image A running docker image is a container. 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 ) into ./ folder in the image (which is set to/usr/src/app in step 1). Docker host 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. 🛠 : $ docker build -t node-app-image . 3. Building the image This command will download(pull) the node image from , which is a public library ( ) of docker images. Docker Hub registry 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: Cool phrases — container, image, registry, docker hub, Dockerfile Mystic arts — How to write Dockerfiles and create images. 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? humans have evolved beyond this. Create 3 Dockerfiles, build 3 images and run 3 images. — 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 on David Beatz Unsplash Welcome to the world of docker. Further readings: — A very good read that will tell you more about how things work in Docker. Best practices for writing Dockerfiles — They are pretty good once you know the basic concepts. Official Docker docs