This assignment aims to provide hands-on experience in Dockerizing Flask applications, first individually and then using Docker Compose for managing multiple applications.
I used my previous Flask App for the base.html
file and added in new things for the contactus.html
file.
- I first created a Dockerfile that contains the following:
FROM python:3.7-alpine
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["python", "app.py"]
- Then I created an
app.py
file - Lastly, I created a
requirements.txt
file with flask in it
To Dockerize my FLask Application, I did the following steps:
- First build the docker image with
docker build -t <name> .
- Then do
docker images
to show the docker images. Make sure your<name>
is there. - Then run the command with
docker run -d -p 5001:5000 <name>
- Type in
docker ps
to check your docker container information. - When you are done with your Docker image, you can stop the image from running with
docker stop <container ID>
- If needed, you can delete images with
docker system prune -a -f
Importance of each step in the Dockerfile:
FROM
specifies which base operating system from the hub.docker.com with python installed in itWORKDIR
folder that houses this codeCOPY
copies the content of the current directory of the Dockerfile into the app directory within the containerRUN
requirements.txt is to just install the python dependencies in requirements.txt (flask)EXPOSE 5000
is to show that the docker container will be designed to listen on port 5000CMD
is to run the app.py through python when the container starts to run
- I created two folders under the Part2 Folder for the two flask applications: flask1 and flask2
- In each folder contains:
- Dockerfile that contains:
FROM python:3.10-slim-buster WORKDIR /app COPY . /app RUN pip install -r requirements.txt EXPOSE 5000 CMD ["python", "app.py"]
- app.py file
- requirements.txt file with flask in it
- templates folder for HTML
- Dockerfile that contains:
- In the Part2 Folder, I have a
docker-compose.yml
file that contains:
version: '3'
services:
flask_app_1:
build: ./flask1
ports:
- "7000:5000"
volumes:
- ./flask1:/app
flask_app_2:
build: ./flask2
ports:
- "5020:5000"
volumes:
- ./flask2:/app
Explanation of each section of the docker-compose.yml
file:
version
is the version of the Docker Compose file that is being used (we are using version 3)services
is the section with the different services/containers for this applicationflask_app_1
is the first flask appbuild: ./flask1
directs the image built to flask1's dockerfileports
maps port 5000 from the conatiner to port 7000 for the host.volumes
any changes that occur in the local files will be brought to the container here (same concept for flask_app_2 as well)
To run this, I did the following steps:
- Start off by building the images:
docker-compose up --build
- If i need to reconnect to it, I did
docker-compose up
- If you want to see a list of your containers, use
docker-compose ps
- If needed, you can stop it with
docker-compose down
Docker Compose helps orchestrate multiple containers without running each container separately while Docker will run individually. If you have multiple containers and you only use Docker alone, then you will need to start and command each container separately while Docker Compose contains a docker-compose.yml
file that helps you run your entire application at the same time, making it easier for you to run and stop each application.
No challenges encountered.