Deploying Mini Project with Docker-compose on CentOS 7: A Step-by-Step Guide

M. Cagri AKTAS
5 min readAug 19, 2023

--

In this project, we will create a small application using two containers (web and redis) with docker-compose. Our goal is not just the project itself, but to focus on the logic, installation, and startup process of docker-compose:

If you begginer to Docker please read my other article about Docker.

Deploying Postgresql and pgAdmin4 with Docker on CentOS7: A Step-by-Step Guide

What’s mean docker-compose: Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define an entire application stack, including multiple services, networks, and volumes, using a single YAML configuration file. This file defines the services, their configurations, and how they interact with each other. Docker Compose simplifies the process of managing complex applications by providing a way to start and stop all the services together with a single command.

You can read offical article in here.

Project: In our project, we will create a webpage in HTML format where users can upload only .csv files. We will then generate a bar chart displaying the count of products purchased by individuals. During the upload process, users will provide a name, and based on that name, we will retrieve historical records from Redis.

All the files are available in the image below. You can access these files through my GitHub

1-) app.py: This file contains the code for the our application. It is a typical application file.

Lines 8 and 9 contain the code I used to establish the connection between Flask and Redis.

Lines 11 and 12 are not crucial since they pertain to the HTML code that I will open in the browser.

The upload() function contains our main Python code.

docker-compose.yml:

version: '3.8'

services:
redis:
image: redis
web:
build: .
ports:
- "5000:5000"

In these lines of code, we first specify the version of the YAML file and then add the applications that we will use as services.

We have a total of two applications that we will use. That’s why we are creating two separate Docker containers. This is what we call docker-compose :) These applications are Redis and Web.

In the “web” section, we use “build: .” to instruct Docker Compose to build from the current directory where we are located when we first set up. We also set the port to 5000:5000.

2-) Dockerfile:

FROM python:3.9

WORKDIR /app

COPY requirements.txt requirements.txt

RUN pip install -r requirements.txt

COPY . .

EXPOSE 5000

ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0

CMD ["flask", "run"]

1. FROM python:3.9: This line sets the base image for your Docker image. It specifies that you want to start with a base image that has Python 3.9 installed. This base image will provide the runtime environment for your application.

2. WORKDIR /app << This line sets the working directory inside the container to /app. Any subsequent instructions will be executed in this directory. It is a best practice to set the working directory to an appropriate location for your application code. As seen, when we enter the container, we arrive at the path /app.

3. COPY requirements.txt requirements.txt: This line copies the requirements.txt file from your local directory to the /app directory inside the container. This file typically lists the Python dependencies required by your application.

4. RUN pip install -r requirements.txt: It upgrades pip itself and then installs the packages listed in the requirements.txt file.

5. COPY . . : This line copies all the files and directories from your local directory to the current directory (/app) inside the container. It includes your Flask application code and any other necessary files.

6. EXPOSE 5000: This line specifies that the container will listen on port 5000. Please be carrefuly this because if you using 5000 port, you can’t build clearly Dockerfile.

7. ENV FLASK_APP=app.py:This line sets an environment variable named FLASK_APP to the value app.py. This environment variable is used by the Flask framework to identify the main application file.

8. ENV FLASK_RUN_HOST=0.0.0.0: This line sets the FLASK_RUN_HOST environment variable to 0.0.0.0, which means that the Flask development server should bind to all network interfaces in the container. This allows the server to accept connections from outside the container.

9. CMD [“flask”, “run”]: This line specifies the command that will be executed when the container starts. It runs the flask run command, which starts the Flask development server to run your application.

3-) requirements.txt: The requirements.txt file lists the Python dependencies required by the application. It specifies the packages that need to be installed for the application to run properly.

4-) templates > index.html and retrieved.html: These files contain basic HTML code for the project.

BUILD & START

When we run the Docker-compose.yml file, the instructions in the Dockerfile are executed to build the Docker image. But don’t forget, you must build you’re current project location.

docker-compose build

If everything is set up correctly, we can start the containers using,

docker-compose up -d

Where the “-d” flag means to run in detached mode. So you can still using you console!

So we can start the app in any browser “localhost:5000”, because the container’s IP address is set to localhost (0.0.0.0) in the Dockerfile, and we have exposed port 5000, which means our application is accessible on port 5000.

This is not a big project so you can’t get grap when you upload diffirent dataset. So just try my dataset, you can find dataset in my github.

If you want to stop containers,

# docker-compose stop < container id >
# out redis container id is = 9be63178680f
# our web container id is = 3dcf10421366

docker stop 9be63178680f
docker stop 3dcf10421366

If you want to start,

docker start 9be63178680f
docker start 3dcf10421366

I hope you can understand everything :) If you need to ask something about project please send me a mail. “ mucagriaktas@gmail.com

Cheers in peace! :)

--

--