Docker Compose Tutorial Series featured image

Docker Compose Tutorial Series Part 1 :Create YML file and Run it

Docker compose is a great framework for deploying your production code in a short period of time. Rather than writing all the options code in Command prompt why not create a Docker Compose YAML file and tell all the things you want to do for docker. In this entire Docker Compose Tutorial , you will learn how to create a Docker Compose YML file and run it through steps by step.

Before going to create the file first I will tell you what is the problem or the Use Case I want to do so that you understand it easily.

Use Case

Create two containers of Python Flask app one is running on the port 5000 and the r is running on the port 5001.

Please note that I am using the Linux Ubuntu  Operating system and make sure the docker is installed.

Step 1: Install the Docker Compose.

If you have not installed docker compose then install it in Linux command using.

sudo apt-get install docker-compose

Step 2: Create a YAML file.

After installing the docker compose now its turn for creating a YML file.

touch docker-compose.yml

Step 3:  Write the Instructions in the File

The next step is to write the instructions for creating services and containers in the YAML file.  As I have already mentioned that I will run two flask containers on the same network but on different ports 5000 and 5001. Before writing the command in docker compose fist lets understand the docker command we use to run containers manually.

For creating a Network

docker network create flask-net

Creation of Volume

docker volume create flask-vol

Docker File

For running the Flask app you have to create a Docker File to tell the docker compose to do some task. Docker File will contain the following thing.

python:latest
WORKDIR /app
COPY requirements.txt ./
RUN pip3 install -r requirements.txt
COPY . /app
CMD ["python3", "run.py"

Final Commands

docker run --name app1 --network flask-net -v flask-vol:/flask_app -p:5000:5000  -d flask-image
docker run --name app2 --network flask-net -v flask-vol:/flask_app -p:5001:5000  -d flask-image

All the above things you have to do manually. But all these things can be managed by docker-compose file.  It is an YAML file and you have to define all the services syntaxaliy. Like in our example I have written the following lines in docker compose file.

version: '3'
services:
 web1:
  build:
   context: .
   dockerfile: Dockerfile
  image: app1
  ports:
   - "5000:5000"
  networks:
   - flask-net
  volumes:
   - flask-vol1:/app
 web2:
  build:
   context: .
   dockerfile: Dockerfile
  image: app2
  ports:
   - "5001:5000"
  networks:
   - flask-net
  volumes:
   - flask-vol2:/app
networks:
 flask-net:

volumes:
 flask-vol1:
 flask-vol2:

Here I am doing the following things

  1. Creating services with the name web1 and web2 and the images (app1 and app2) are being made using a Dockerfile that I have already defined.
  2.  After that, I am assigning port 5000 and 5001 for app1 and app2.
  3. Creating a network flask-net and two volumes flask-vol1 and flask-vol2 and mapping it with the docker Workdir directory app.

Step 4: Run the Docker Compose file.

After creating the file now its time to run the file.

sudo docker-compose up -d

It will create two images app1 and app2 with flask-net network and two volumes flask-vol1 and flask-vol2 automatically.  You will see it in the terminal like this.

running docker-compose file

You can see the web1 is running on port 5000 and web2 is running on 5001 port.

running flask app on two different port

To see all the running services use ps command

sudo docker-compose ps

see all the running containers in docker compose

If you want to stop all the services you can use down command.

sudo docker-compose down

You can also check the volumes and network created by using the ls command.

sudo docker volume ls

volumes created using docker compose file

sudo docker network ls

network created using docker compose file

This way you can automate all the works that you do manually in command prompt. There are also other things you can do inside the docker compose YAML file. I will tell all these things in the next series of Docker Compose Tutorial. Therefore subscribe to us to get the next tutorial directly in your inbox. If you have any queries about then contact us.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner