Build the Docker Image
The production deployment process begins with building the Docker image using the provided Dockerfile
. The Dockerfile specifies the base image and the working directory, along with the necessary dependencies.
# syntax=docker/dockerfile:1
ARG PYTHON_VERSION=3.12
FROM python:${PYTHON_VERSION}
WORKDIR /src
COPY . .
ARG VERSION=0.0.0.dev0
RUN --mount=type=cache,target=/cache/pip \
PIP_CACHE_DIR=/cache/pip \
SETUPTOOLS_SCM_PRETEND_VERSION=${VERSION} \
pip install .[ssh]
The key commands here include:
FROM python:${PYTHON_VERSION}
: This line pulls the specified Python version as the base image.COPY . .
: This command copies the current directory’s contents into the/src
directory in the Docker image.RUN pip install .[ssh]
: This installs the package defined in thesetup.py
file and includes any extras specified, in this case, thessh
extras.
Build Command
To build the Docker image, execute the following command in the terminal:
docker build -t myapp:0.0.0.dev0 .
Replace myapp:0.0.0.dev0
with the desired tag for your Docker image.
Deployment Configuration via Makefile
The deployment process can be managed through a Makefile
which provides a structured way to run various commands essential for deployment.
.PHONY: build clean all
all: build
build:
docker build -t myapp:$(SETUPTOOLS_SCM_PRETEND_VERSION_DOCKER) .
clean:
docker rmi myapp:$(SETUPTOOLS_SCM_PRETEND_VERSION_DOCKER) || true
Makefile Variables
The SETUPTOOLS_SCM_PRETEND_VERSION_DOCKER
variable validates the current version using Git tags, which is useful for semantic versioning. The default value is set to 0.0.0.dev0
if no tags are found.
Building the Image via Makefile
To build the image using the Makefile
, run:
make
The above command will use the build
target and accordingly tag the image with an appropriate version.
Running the Docker Container
Once the image is built, the next step is to run the Docker container. Use the following command:
docker run -d --name myapp_container myapp:0.0.0.dev0
Adjust parameters as necessary for your specific deployment environment, such as network configurations and environment variables.
Cleaning Up
After deployment, it may be necessary to clean up unused Docker images or containers. The Makefile
provides a clean target:
make clean
This command removes the Docker image tagged with the specified version.
Conclusion
Following the steps outlined here, the application can be built, deployed, and managed efficiently in a Dockerized production environment. The structured approach facilitated by the Dockerfile
and Makefile
ensures a consistent deployment process.
Refer to the original source of this configuration for more details and options regarding deployment.