Skip to main content

Dockerizing a python project

Dockerizing a Python Project

So this is a basic example of a docker file

# Set Base image to python 3.9 image

FROM python:3.9

WORKDIR /app

COPY . /app

RUN pip install -r requirements.txt

EXPOSE 8080

ENV token sdajkadsjkslnladsnkln

CMD \["python", "app.py"\]

Assuming this is our folder structure

|-app.py

|-requirements.txt

|-dockerfile

Here is each line in the docker file:

  1. Set the base image to the python runtime

  2. Set our workDIR to /app

  3. Copy our project, into /app in the image

  4. Run pip install to get dependencies

  5. Tell the image that our app will run on port 8080 and that we need to expose it to the outer world VIA port-fowarding

  6. Create and env called Token

  7. The run command (which will be run when the image is run) is set to python app.py

Finally we can build and run it like so

docker build -t imagename . 

docker run imagename