Wondering how to dockerize the existing python flask application with Docker Compose? Well by reading this article, you will be the best in dockerizing the flask application with Docker Compose. To do so, you will just have to learn what is Docker, Docker container and Docker Compose at first so that we will find things easier and comprehensible.
What is Docker?
In simple words, Docker is a tool which is used to automate the deployment of applications in lightweight containers so that applications can work effectively and efficiently in different environments.
Containers are helpful in such a way that allows a developer to package up an application with all the parts that require including libraries and other dependencies and deploy it as a single package. By doing this, the developer can rest assured by simply writing codes and without having to worry about the machine as the application now runs on any Linux machine.
What are Docker containers?
A Container is a software package that comprises all the dependencies required to run an application. They are the ready applications created from Docker Images and thus hold the entire package that is required to run the applications.
Note: A Docker image is a file, consisting of multiple layers that are used to execute code in a Docker container.
Multiple containers run on the same hardware and maintain isolated applications. Docker can be a bit of a virtual machine. However, the Docker container beats the virtual machine by its inundated beneficial features. Here are a few benefits of using Docker containers:
- Docker containers occupy less space
- Short boot-up time
- Containers have a better performance as they are hosted in a single Docker engine
- Higher efficiency
- Data volumes can be shared and reused among multiple containers
What is Docker Compose?
Docker Compose is a tool for defining and running multi-container Docker applications. We can use YAML files to configure the application’s services. All it takes is a single command to create and start all the services from your configuration. Simply Run Docker-compose up and Compose starts and runs your entire app.
Why do we need to dockerize the application anyways?
It is because the Docker containers encapsulate everything an application needs to run. They allow applications to be shuttled with no trouble between environments. Any host with the Docker runtime installed on its machine can run a Docker container. It can be a developer’s laptop or a public cloud instance.
Create a new folder with the label app for your project and Clone your project from git.
mkdir app git clone <project repository>
Create a Virtual Environment for your application and install all the dependencies in it.
To create a virtual environment
sudo apt–get install virtualenv
virtualenv venv -p python3 source venv/bin/activate pip install -r requirements.txt
Make sure your application is running and your project structure should look like: (Note that here our project repository is web).
So now, we are ready to dockerize the application.
Steps to dockerize an application
The first and foremost step to dockerize an application is to ensure that you have Docker installed on your machine. Follow the step by step procedure illustrated below to dockerize the application.
- Creating a Dockerfile
- Creating a Docker Compose file.
- Run the Docker-Compose file
Creating a Dockerfile:
cd project folder touch Dockerfile
Create a Dockerfile and copy the contents into the dockerfile created. And we are using tiangolo/uwsgi-nginx-flask:python3.6 as the base image. It already has the nginx Configuration. So we don’t need to create a separate nginx docker file.
# Use the official image as a parent image FROM tiangolo/uwsgi-nginx-flask:python3.6 # Run the command inside your image file systems RUN mkdir -p /usr/src/app # Set the working directory WORKDIR /usr/src/app # Copy the file from your host to your current location COPY web/requirements.txt /usr/src/app # Run the command inside your image filesystem RUN pip install --no-cache-dir -r requirements.txt ENV FLASK_APP=app.py # Run the specified command within the container. CMD flask run -h 0.0.0.0 -p 5000
In the above code,
The FROM instruction here initializes a new build stage and sets the Base Image for following instructions and a valid Docker file should begin with a FROM instruction.
The RUN instruction executes any command in a new layer on top of the existing image and commits the results. The resulting committed image is used for the next step in the Dockerfile.
The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. If the WORKDIR does not exist, it will be created even if it’s not used in any subsequent Dockerfile instruction.
The COPY instruction copies new files or directories from <src> and includes them to the filesystem of the container at the path <dest>.
The CMD instruction has three forms:
- CMD [“executable”,”param1″,”param2″] (exec form, this is the preferred form)
- CMD [“param1″,”param2”] (as default parameters to ENTRYPOINT)
- CMD command param1 param2 (shell form)
There should only be one CMD instruction in a Dockerfile. If more than one CMD is listed, then only the last CMD will be taken into effect.
The key purpose of a CMD is to provide defaults for an executing container.
ALSO READ: How To Build A Dating App Like Tinder
Creating a Docker Compose file:
Create a Docker compose file and paste the following contents in it.
cd app touch docker-compose.yml
version: '3' services: postgres: restart: always image: postgres:10 environment: - POSTGRES_USER=postgres - POSTGRES_PASSWORD=postgres - POSTGRES_DB=chat volumes: - ./postgres-data/postgres:/var/lib/postgresql/data ports: - "5431:5432" app: restart: always build: context: . dockerfile: web/Dockerfile ports: - 5000:5000 volumes: - ./web:/usr/src/app stdin_open: true tty: true
App, postgres, and nginx are the services which we are going to run.
In the above code,
Decode the Docker-compose file.
The version used here is Docker-compose 3. The services used here are the main application, Postgres database, adminer and nginx server. However, we need not write nginx service in docker compose as we have used the nginx as the base image for the application docker file.
We are using postgres:10 image which is a base image for the service and it will pull automatically from Docker hub. Then under the environment, we mentioned the postgres user, password and database name for the database.
no is the default restart policy and it does not restart a container under any circumstance. When always is specified, the container always restarts. The on-failure policy restarts a container if the exit code indicates an on-failure error.
restart: "no" restart: always restart: on-failure restart: unless-stopped
Docker Volumes – Bind mounts
Bind mounts are parts of the host file system that could be mounted directly inside the Docker container. To introduce a bind mount, simply state the host directory that you want to share and the mount point inside the Docker container where it ought to be mounted:
Volumes: – – ./postgres-data/postgres:/var/lib/postgresql/data
For instance, I am using the path /home/<USER>/projects/ghost. You can use whatever the path on your Docker host you want that you have access for.
Mapping the ports to create a connection between the Container and Host. The mapping of the ports is in the HOST:CONTAINER format.
Mention the created Docker file of the application under build context to create a Docker image from the Docker file. Type restart: always so the container will always restart. We have used both the host port and container port as 5000. Make certain that the mentioned host port is free to use so that we can access our application in the host port 5000 otherwise we will get an error like the port is already in use. We mounted the volumes to usr/src/app.
All we need to do now is to run the docker compose file to run the services. We can access the application in the host port 5000.
The command Docker-compose up. Compose starts and runs your entire app.
To access the application http://0.0.0.0:5000/
To stop the service, Docker-compose down.
In this article, we walked you through how to dockerize the flask application with Docker compose. You will be now able to dockerize the flask application with ease.
If you have a business idea in your mind and searching for a reliable web development company, you are in the right place. Hire the best Python developers in the industry from Agira technologies.