How to create simple Docker container with Python code?

Category: Machine Learning :: Published at: 04.03.2023

This is how i usually create a simple Docker container for Python code:

  • Create some simple Python project
  • Create a Dockerfile file in the Python project directory
  • This is the simple code you can include inside Dockerfile:
FROM ubuntu:20.04

RUN apt-get update
RUN apt-get install python3 -y
RUN apt-get install python3-pip -y
RUN apt-get install vim -y


COPY requirements.txt . 
RUN pip3 install -r requirements.txt
COPY . .
  • The command below in Python will generate requirements.txt file which will include package requirements for docker
pip freeze > requirements.txt
  • When you have this, you can just run two simple commands to create a container (remember that Docker must be installed on your computer)
# Create a container

docker build --pull --rm -f Dockerfile --tag [CONTAINER_NAME]:latest "."

# Run your container

docker run --it [CONTAINER_NAME]


Remember that after creating changes in your code, you need to rebuild the container with the first command again.

- Click if you liked this article

Views: 89