01 logo

Building Docker images for your Python apps

How to use docker to build custom containers for your apps

By Jordan RaychevPublished 3 years ago 2 min read
Like
Building Docker images for your Python apps
Photo by Dominik Lückmann on Unsplash

Recently, I have been working on a web scraper, which downloads files from user provided URLs, processes them and finally uploads them to a FTP server. When I started the project I was exploring several programming languages to build the project on and I ended up choosing Python.

Every python project requires a set of libraries to be installed and mine was no different. The catch here is that when you install additional libraries or different library versions under your main environment you may end up breaking other project's dependencies. Because of that I wanted to build my app inside a Docker container and don't worry about breaking other projects.

I want to focus on building the Docker container from the group up and I am not going to discuss what Docker is, how to install it and configure your environment.

The first step is to create a file named "Dockerfile" in your root app directory and open it with your favorite editor (I prefer vscode). The structure of your Dockerfile should look like this:

FROM ubuntu:20.04 #Installs ubuntu 20.04 from Docker Hub

RUN apt-get update

RUN apt-get install python3 -y

RUN apt-get install python3-pip -y

RUN apt-get install vim -y

WORKDIR /home/{your-username}/apps/{your-app-name} #Declaring working directory in our container

COPY requirements.txt . #Copy requirements.txt to $WORKDIR

RUN pip3 install -r requirements.txt

COPY . . #Copy source files to $WORKDIR

Firstly, we are download and installing Ubuntu 20.04 from docker hub as a base image for our container. Secondly we are issuing several apt commands to update our package list and install python3, python3-pip and vim on our container. After that we specify the working directory in our container (you have to change {your-username} and {your-app-name} with your actual username and app name that you want to use). The next step is to copy our requirements.txt file to the working directory (do not forget the trailing dot) and install all the required packages for the app. And lastly, we are copying all the source files from to our container by issuing COPY . ..

We are pretty much at the end of our docker container build. Once we are happy with our Dockerfiles settings we have to build our actual container. In order to do that we have to issue the following commands (do not forget to change your {container-name} with your actual container name):

docker build - pull - rm -f "Dockerfile" -t {container-name}:latest "."

If you want to run the container interactively you can use the following command:

docker run - it {container-name}

Once the execution finishes you will be granted a shell access of your newly build container.

With that we conclude this article. I hope that this has been informative to you and I would like to thank you for reading!

how to
Like

About the Creator

Jordan Raychev

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.