For this project, you need to create a Dockerfile, a Python dependencies file, and a docker-compose.yml file. (You can use either a .yml or .yaml extension for this file.)
-
The Dockerfile defines an application’s image content via one or more build commands that configure that image. Once built, you can run the image in a container. For more information on Dockerfile, see the Docker user guide and the Dockerfile reference. eg: mysite/dockerfile/Dockerfile
FROM python:3.9-alpine3.15
WORKDIR /mysite
COPY ./dockerfiles/requirements.pip /requirements.pip
RUN apk update \
&& apk upgrade busybox zlib \
&& apk add libpq libpq-dev gcc musl-dev make expat-dev\
&& pip3 install -r /requirements.pip\
&& apk del gcc musl-dev make libpq-dev expat-dev\
&& rm -rf /var/cache/apk/* /root/.cache/*
COPY /mysite /mysite
This Dockerfile starts with a Python 3 parent image. The parent image is modified by adding a new code directory. The parent image is further modified by installing the Python requirements defined in the requirements.pip file.
-
This file is used by the RUN pip install -r requirements.pip command in your Dockerfile.
Django>=3.0,<4.0
psycopg2>=2.8
- The docker-compose.yml file describes the services that make your app. In this example those services are a web server and database. The compose file also describes which Docker images these services use, how they link together, any volumes they might need to be mounted inside the containers. Finally, the docker-compose.yml file describes which ports these services expose. See the docker-compose.yml reference for more information on how this file works.
- mysite/docker-compose.yaml
version: "3.3"
services:
db:
container_name: 'database-postgres'
image: postgres:14-alpine
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
container_name: 'mysite-web'
build: .
command: /bin/sh -c -- "while true; do sleep 300; done;"
volumes:
- .:/mysite
ports:
- "8000:8000"
environment:
- POSTGRES_NAME=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
This file defines two services: The db service and the web service.
$ docker-compose up