Django packaged as a docker image

I’ve thought about packaging a django project for docker deployment before, but since the previous project was too large and used various system services such as system timers, I didn’t know if the related services would still be able to start after packaging it for docker, so I didn’t implement it.

With the My Footprint Map I made the other day, the project is relatively self-contained, with no other dependencies, and it was just the right time to try it out.

First create a Dockerfile under the project and write the following:

# 使用官方Python运行时作为父镜像
 
FROM python:3.8.18-slim
 

 
# 设置工作目录
 
WORKDIR /app
 

 
# 将当前目录内容复制到位于/app中的容器中
 
COPY . /app
 

 

 
# 安装项目依赖
 
RUN pip install --no-cache-dir -r requirements.pip -i https://pypi.tuna.tsinghua.edu.cn/simple
 

 
# 暴露端口8000,与Django的默认运行端口一致
 
EXPOSE 10086
 

 
# 定义环境变量
 
ENV NAME=Django
 

 
# 在容器启动时运行Django的manage.py命令
 
CMD ["python", "manage.py", "runserver", "0.0.0.0:10086"]

The online code is copied back and forth, and the second line is all FROM python:3.8-slim If you write it that way it will result in the following error:

PS E:\Pycharm_Projects\BabyFootprintV2> docker build -t baby-footprint:1.0 .
 
[+] Building 21.2s (2/2) FINISHED docker:desktop-linux
 
 => [internal] load build definition from Dockerfile 0.0s
 
 => => transferring dockerfile: 568B 0.0s
 
 => ERROR [internal] load metadata for docker.io/library/python:3.8-slim 21.1s
 
------
 
 > [internal] load metadata for docker.io/library/python:3.8-slim:
 
------
 
Dockerfile:2
 
--------------------
 
   1 | # 使用官方Python运行时作为父镜像
 
   2 | >>> FROM python:3.8-slim
 
   3 |
 
   4 | # 设置工作目录
 
--------------------
 
ERROR: failed to solve: python:3.8-slim: failed to resolve source metadata for docker.io/library/python:3.8-slim: failed to do request: Head "https://registry-1.docker.io/v2/library/python/manifests/3.8-slim": dialing registry-1.docker.io:443 container via direct connection because has no HTTPS proxy: connecting to registry-1.docker.io:443: dial tcp 69.63.186.31:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

Directly accessing the above URL docker.io/library/python:3.8-slim will reveal that there is no such thing, so change it to FROM python:3.8.18-slim

Do a search and there will be tutorials prompting to download the python 3.8 docker first:

PS E:\Pycharm_Projects\BabyFootprintV2> docker pull python:3.8.18-slim
 
3.8.18-slim: Pulling from library/python
 
8a1e25ce7c4f: Pull complete
 
1103112ebfc4: Pull complete
 
b7d41b19b655: Pull complete
 
6a1ad0671ce8: Pull complete
 
de92c59aadaa: Pull complete
 
Digest: sha256:e796941013b10bb53a0924d8705485a1afe654bbbc6fe71d32509101e44b6414
 
Status: Downloaded newer image for python:3.8.18-slim
 
docker.io/library/python:3.8.18-slim

3.8.18 is ok, just rebuild it now:

PS E:\Pycharm_Projects\BabyFootprintV2> docker build -t baby-footprint:1.0 .
 
[+] Building 214.6s (9/9) FINISHED docker:desktop-linux
 
 => [internal] load build definition from Dockerfile 0.0s
 
 => => transferring dockerfile: 571B 0.0s
 
 => [internal] load metadata for docker.io/library/python:3.8.18-slim 0.0s
 
 => [internal] load .dockerignore 0.0s
 
 => => transferring context: 2B 0.0s
 
 => [1/4] FROM docker.io/library/python:3.8.18-slim 0.1s
 
 => [internal] load build context 0.9s
 
 => => transferring context: 43.30MB 0.8s
 
 => [2/4] WORKDIR /app 0.1s
 
 => [3/4] COPY . /app 0.2s
 
 => [4/4] RUN pip install --no-cache-dir -r requirements.pip -i https://pypi.tuna.tsinghua.edu.cn/simple 212.0s
 
 => exporting to image 1.4s
 
 => => exporting layers 1.4s
 
 => => writing image sha256:cba073b574f88f19be7487b29612e19b9826ab99e7b54ea748bd5df22e83e1a0 0.0s
 
 => => naming to docker.io/library/baby-footprint:1.0 0.0s

After compiling, you can push the image to the docker hub, but first you need to set the tag, if you push it directly, you will get the following error:

PS E:\Pycharm_Projects\BabyFootprintV2> docker push baby-footprint:1.0
 
The push refers to repository [docker.io/library/baby-footprint]
 
04013169f44d: Preparing
 
f7c443286fad: Retrying in 5 seconds
 
fd749af069d5: Retrying in 5 seconds
 
3482d4cd60de: Retrying in 5 seconds
 
370c0e78e3ea: Retrying in 5 seconds
 
a74bee0a48a5: Waiting
 
c8f253aef560: Waiting
 
a483da8ab3e9: Waiting
 
denied: requested access to the resource is denied

This prompt was also a bit tricky, as docker was blocked, I thought it was a network connection problem until I realized later that it was a path problem.

Set the tag after push with the following command:

docker tag baby-footprint:1.0 obaby/baby-footprint:1.0
PS E:\Pycharm_Projects\BabyFootprintV2> docker push obaby/baby-footprint:1.0
 
The push refers to repository [docker.io/obaby/baby-footprint]
 
04013169f44d: Pushed
 
f7c443286fad: Pushed
 
fd749af069d5: Pushed
 
3482d4cd60de: Pushed
 
370c0e78e3ea: Layer already exists
 
a74bee0a48a5: Pushed
 
c8f253aef560: Pushed
 
a483da8ab3e9: Layer already exists
 
1.0: digest: sha256:0d0c0989a64cc3f3e192e5c8e7bc4931676d49ab66d810061a1daec6b1a6af58 size: 2000

Due to network issues, push may fail, just try it a few more times and you’ll be fine.

Finally, you can install and run it directly from the docker:

docker push obaby/baby-footprint:tagname