Descriptions of the files: In this project, we will build a mini calculator app using Docker Compose. To Dockerize an application, you need four files:
- Dockerfile
- app.py
- requirements.txt
- docker-compose.yml
-
Dockerfile: The Dockerfile is a text file that defines the steps to create a Docker image. It specifies the base image, sets the working directory, defines environment variables, copies files, installs dependencies, exposes ports, and sets the command to run the application. Dockerfile's steps:
-
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.
-
WORKDIR /code << This line sets the working directory inside the container to /code. 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.
-
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.
-
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.
-
COPY requirements.txt requirements.txt << This line copies the requirements.txt file from your local directory to the /code directory inside the container. This file typically lists the Python dependencies required by your application.
-
RUN pip install --upgrade pip && pip install -r requirements.txt << This line runs the pip command inside the container to install the necessary Python packages. It upgrades pip itself and then installs the packages listed in the requirements.txt file.
-
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.
-
COPY . . << This line copies all the files and directories from your local directory to the current directory (/code) inside the container. It includes your Flask application code and any other necessary files.
-
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.
-
-
app.py: This file contains the code for the calculator application. It is a typical application file.
-
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.
-
docker-compose.yml: The docker-compose.yml file is used to define and configure the Docker services. It specifies how the Docker containers should be built and run. It includes information about the Docker image, ports to expose, and any additional configuration.
When we run the Docker-compose.yml file, the instructions in the Dockerfile are executed to build the Docker image.
docker-compose build
Docker randomly assigns a name to this build, so we can give a tag (name) when building a Dockerfile, like this:
docker-compose build -t project_test
PS: We're using Docker-Compose because we have two containers in this project; the first one is Redis, and the second one is the web app. This means Docker uses just a single container, while Docker Compose uses more than one container.
If everything is set up correctly, we can start the containers using,
"docker-compose up".
To run the containers in the background, we can use,
"docker-compose up -d"
where the "-d" flag means to run in detached mode.
So we can start the app in any browser " localhost:5000 "
PS: CSS and HTML codes from chatgpt... :)