Skip to main content

Dockerfile Guide

Building a minimal Dockerfile

Often, applications are containerized and work out-of-the-box when being moved to Pergola. If not, this article shows a set of Dockerfile examples that are easy to customize and help you start hosting your application on Pergola.

Dockerfiles can be highly standardized and do not require Pergola specific dependencies. They all follow the same structure and can be reused when creating multiple applications in the same programming language:

  • Start with an image specialized for your programming language, e.g. Java or Python
  • Make source code and configuration available via COPY in WORKDIR
  • Download all dependencies, execute build
  • Start application

It can be as short and focused as here:

FROM <PROGRAMMING-LANGUAGE>:<VERSION-TAG>

WORKDIR /app
COPY build-config/ build-config/
COPY source-code/ source-code/

RUN <package-manager> resolve-dependencies build

CMD ["app", "argument_1", "argument_2"]

Python Dockerfiles

Python pip Dockerfile example

A Dockerfile for running a Python application, using pip as package manager:

FROM python:3.13-slim

WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

COPY hello.py ./

# final configuration
ENV FLASK_APP=hello
EXPOSE 8017
CMD ["flask", "run", "--host", "0.0.0.0", "--port", "8017"]

Note, that a standard Python application, not using flask, could be started like this:

CMD ["python", "hello.py"]
tip

For Python, it is beneficial to resolve dependencies before copying the source code. This allows the resolved dependencies to be cached by Docker as image layer, speeding up future Docker builds.

Python conda example

A Dockerfile for running a Python application, using conda as package manager:

FROM continuumio/miniconda3

WORKDIR /app

COPY environment.yml environment.yml
RUN conda env create -f /app/environment.yml
RUN echo "source activate myenv" > ~/.bashrc
ENV PATH=/opt/conda/envs/myenv/bin:$PATH

COPY hello.py ./

# final configuration
ENV FLASK_APP=hello
EXPOSE 8017
CMD ["flask", "run", "--host", "0.0.0.0", "--port", "8017"]
note

The conda environment needs to be named 'myenv' in environment.yml

Java Dockerfiles

Java gradle wrapper Dockerfile example

A Dockerfile for running a Java application, using gradle wrapper as package manager and build tool:

FROM eclipse-temurin:21-jdk

WORKDIR /app
COPY . .
RUN ./gradlew clean build

RUN cp /app/build/libs/*SNAPSHOT.jar /app/hello.jar

CMD ["java", "-jar", "hello.jar" ]

Further Reading