Everything on Docker (Part ll)

In this article, we are going to use docker-compose techniques to use multiple containers separately to build an app.

What is docker compose?

“Docker Compose is a tool for running multi-container applications on Docker defined using the Compose file format.”

It will make sense soon enough. We are going to create two containers for the app. The app will be based on “how many times the app is visited”. Classic!😆 I am not going to talk about the coding part rather the docker part only.

Step 1: Let’s consider we have the following application on our hands. A node js project that will count how many times a user visits the site.

Step 2: Now, it’s time to create Dockerfile for this app and build the image. Do you remember how to create a Dockerfile? — 1. Define the base image 2. Set the working directory (to separate out the files) 3. COPY the files to the container location 4. Install the dependencies. 5. Set the default command to start the app every time you run the container.

Dockerfile and build image!


  • Why did we use “WORKDIR ‘/app’”? : Think
  • Why did we put “COPY package.json .” before installation dependencies? : Think
  • You understand “COPY . .” right? : Think

Now, let’s try to run the image that we created!

We have encountered a redis connection refused error. That makes sense! We did not start any redis server on our machine. Let’s do that now using the docker image.

We are still seeing the same error, which connection refused for redis. Let’s dissect the situation now:

  • We have a docker image for the app and created an instance that is called a container. And we need redis to work it out.
  • Then we download another image from the docker hub and created another instance for the redis to start the redis server.
  • That being said, we have two containers now!
  • But we did not connect them together to make it work!

So what do we have to do to resolve this issue?

We have to set up the networking parts for the containers so that they are connected properly. We can either use docker CLI or docker-compose. We will use docker-compose here.

Docker CLI vs Docker Compose?

“The docker cli is used when managing individual containers on a docker engine. It is the client command line to access the Docker daemon API.

The docker-compose cli can be used to manage a multi-container application. It also moves many of the options you would enter on the docker run cli into the docker-compose.yml file for easier reuse. It works as a front end "script" on top of the same docker API used by docker, so you can do everything docker-compose does with docker commands and a lot of shell scripting. See this documentation on docker-compose for more details.”

Step 3: Now that, we understand a little bit about docker-compose. let’s write it up!

First, we have to set the version for docker. Services allow us to set the instructions to create containers by using specific images or Dockerfile. For example, “redis-server” and “node-app” are the container names. Therefore, the “redis-server” container will be created by the “redis” image, and the “node-app” container will be created by the “Dockerfile” we created. And for the main app, we have set the port mapping.

There are some command changes when we are using docker-compose. Let’s look into that first.

  • docker run <image> →docker-compose up
  • docker build . & docker run <image> → docker-compose up — build

Step 4: It’s time to run the images again to see if it works now!

The first part: “Creating network “visits_default” with the default driver” verifies that docker-compose will connect these two containers automatically.


There you go! We have successfully used docker-compose to connect two containers to run the app!

Some important commands in Docker:

  • docker run -d redis →It will run the container in the background because of -d, which is called “detached”.
  • docker-compose up -d → Similar to the previous one but this one is using docker-compose.
  • docker-compose down → It will stop the containers!
  • docker-compose ps → This will show the status of docker-compose containers. It needs to run from the appropriate directory (docker-compose.yml).

Automatic restart policies?

Sometimes an error occurs and the container might be down. However, we can add a new policy to docker-compose to re-start the container.

restart: always!

There are three other scenarios: 1. “no” 2. on-failure and 3. unless-stopped. For more information, check the documentation of start containers automatically.

In summary, now we know how to work with multiple containers to make an app works properly. In the next one, I will discuss how to deploy a dockerized app to cloud services such as AWS! Cheers! 🙌




Python Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Role Of Clean Code And Comments In HacktoberFest2021

Django & React: JWT Authentication

How to Run ESXi from a USB Flash Drive

Sorting in Algorithm

Retry Policies

Augmented Reality with Swift and ARToolKit for iPhone

Project to make Clone of Freshly- An E-commerce of Food

AWS EC2 instances overview.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mahedi Hasan Jisan

Mahedi Hasan Jisan

Python Developer

More from Medium

FastAPI JWT authentication

Banking App With FastApi and Tkinter

Analyzing and Optimizing Database interactions with Django and pyodbc

FastAPI, Building A Simple CRUD Application With FastAPI.