Skip to main content

Creating a Dockerfile

note

Before getting started, you'll need:

Files​

Create two files in the application's directory:

  • Dockerfile: the steps that build the image and the command that runs the application
  • .dockerignore: files and directories to leave out of the build context

Example Dockerfile​

A basic Python Dockerfile:

dockerfile
FROM python:3.8-slim-buster

WORKDIR /app

COPY requirements.txt requirements.txt

RUN pip3 install -r requirements.txt

COPY . .

EXPOSE 8080

CMD ["python3", "-m" , "flask", "run", "--host=0.0.0.0"]
  • FROM python:3.8-slim-buster: the base image, with Python and its tooling.
  • WORKDIR /app: the working directory for the following instructions.
  • COPY requirements.txt requirements.txt: copies requirements.txt to /app/requirements.txt.
  • RUN pip3 install -r requirements.txt: installs the dependencies.
  • COPY . .: copies the source code into /app, except what .dockerignore lists.
  • EXPOSE 8080: documents the port the application listens on.
  • CMD [...]: the command the container runs. --host=0.0.0.0 makes Flask listen outside the container.

The directory now looks like:

shell
python-app
|____ app.py
|____ requirements.txt
|____ Dockerfile
|____ .dockerignore

For other languages, see templates.