Creating the docker-compose file – Containerization with Docker

The next step in the process is to create a docker-compose file. A docker-compose file is a YAML file that contains a list of services, networks, volumes, and other associated configurations. Let’s look at the following example docker-compose.yaml file to understand it better:

version: “2.4”

services:

flask:

image: “bharamicrosystems/python-flask-redis:latest”

ports:

  • “80:5000”

networks:

  • flask-app-net

redis:

image: “redis:alpine” networks:

  • flask-app-net

command: [“redis-server”, “–appendonly”, “yes”]

volumes:

  • redis-data:/data networks:

flask-app-net: driver: bridge

volumes: redis-data:

The YAML file describes two services – flask and redis.

The flask service uses the python-flask-redis:latest image – the image we built with the preceding code. It also maps host port 80 to container port 5000, exposing this application to your host machine on port 80, and you can access it via http://localhost.

The redis service uses the officialredis:alpine image and does not expose any port, as we don’t want this service outside the container network’s confines. However, it declares a persistent volume, redis-data, that comprises the /data directory. We can mount this volume on the host filesystem for persistence beyond the container life cycle.

There is also a flask-app-net network that uses the bridge driver, and both services share the same network. This means the services can call each other by using their service names. If you look at the app.py code, you will see that we established a Redis service connection using the redis hostname.

To apply the configuration, simply run docker-compose up -d:

$ docker compose up -d  
[+] Running 17/17  
flask 9layers []0B/0BPulled 10.3s
redis 6layers []0B/0BPulled 9.1s
[+] Building 0.0s (0/0)  
[+] Running 4/4  

Network docker-compose_flask-app-net   Created 0.1s

Volume “docker-compose_redis-data”       Created 0.0s

Container docker-compose-flask-1          Started 3.8s

Container docker-compose-redis-1          Stated

Now, let’s list the Docker containers to see how we fare:

$ docker ps      
CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
9151e72f5d66redis:“docker-3 minutesUp 36379/tcpdocker-compose
 alphoneentrypoint.s…”agominutes -redis-1
9332c2aaf2c4bharamicrosystems“flask run”3 minutesUp 30.0.0.0:80->docker-compose
 /python-flask- agominutes5000/tcp,-flask-1
 redis:latest   :::80->5000/tcp 

We can see that two containers are running for both services. We can also see host port 80 forwarding connections to container port 5000 on the flask service.

The redis service is internal and therefore, there is no port mapping.

Let’s run curl localhost and see what we get:

$ curl localhost

Hi there! This page was last visited on 2023-06-01, 06:54:27.

Here, we get the last visited page from the Redis cache according to the sample Flask application code.

Let’s run this a few times and see whether the time changes:

$ curl localhost

Hi there! This page was last visited on 2023-06-01, 06:54:28.

$ curl localhost

Hi there! This page was last visited on 2023-06-01, 06:54:51.

$ curl localhost

Hi there! This page was last visited on 2023-06-01, 06:54:52.

We can see that the last visited time changes every time we curl. Since the volume is persistent, we should get similar last visited times even after a container restarts.

First, let’s curl and get the last visited time and also the current date:

$ curl localhost && date

Hi there! This page was last visited on 2023-06-01, 06:54:53.

Thu Jun  1 06:55:50 UTC 2023

Now, the next time we curl, we should get a date-time similar to 2023-06-01, 06:55:50. But before that, let’s restart the container and see whether the data persists:

$ docker compose restart redis

[+] Restarting 1/1

Container docker-compose-redis-1   Started

Now that Redis has been restarted, let’s run curl again:

$ curl localhost

Hi there! This page was last visited on 2023-06-01, 06:55:50.

As we can see, we get the correct last visited time, even after restarting the redis service. This means that data persistence works correctly, and the volume is adequately mounted.

You can do many other configurations on docker compose that you can readily get from the official documentation. However, you should now have a general idea about using docker compose and its benefits. Now, let’s look at some of the best practices associated with Docker Compose.

Leave a Reply

Your email address will not be published. Required fields are marked *