DEV Community

Cover image for Ops Docker it is #2: Build a Docker Image for Flask Application
Asmi Jafar
Asmi Jafar

Posted on

Ops Docker it is #2: Build a Docker Image for Flask Application

Let's first start with creating the Docker file where we will pull Python 3.9 image.

Flask Dockerfile template

FROM python:3.9.7
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 5000
ENTRYPOINT ["python3"]
CMD ["app.py"]
Enter fullscreen mode Exit fullscreen mode

Here we are pulling Python 3.9.7 image from the Python dockerhub repository.

COPY
Copy the current directory contents into the container at /app.

WORKDIR
It's a directory inside container image that can be set with the WORKDIR instruction in the Dockerfile.

RUN command will install all the required packages for the Dockerfile.

EXPOSE
The EXPOSE instruction tells Docker to get all its information required during the runtime from a specified Port.

ENTRYPOINT
The initiating point for the dockerfile while we run it. ENTRYPOINT command and parameters will not be overwritten from the command line. Instead, all command-line arguments will be added after ENTRYPOINT parameters.

CMD
CMD instruction is used to define what command the container should execute when launched and CMD sets default command and/or parameters, which can be overwritten from the command line when the docker container runs.

Now Let's run the Docker command for building the image

docker build -t asmi-flask .
This will build the image(asmi-flask) for the flask application. Give it a tag -t called asmi-flask.

docker run -p 5000:5000 asmi-flask
In this command we tell docker to run an image called asmi-flask. -p Publish a container's port(s) to the host. Since Flask is running on port 5000 by default, the second part of this flag needs to be 5000.

After this, we can run docker ps command to check if the image has been built or not.

Output:

ubntu@asmijafar20:~/Flask-App$ docker images
REPOSITORY              TAG          IMAGE ID       CREATED       SIZE
asmijafar20/dockerhub   asmi-flask   2e155e2820a0   3 days ago    922MB
Enter fullscreen mode Exit fullscreen mode

In the next blog, we can build CI/CD pipelines and configure them using GitHub Actions.

Till then Happy Coding!

Top comments (0)