创建Django容器,并访问默认的网站

你好。
我是Class Act基础设施事业部的大塚先生。

我打算创建Django的容器映像,并在docker上运行它。另外,我还想将该映像推送到Harbor上。然后,在接下来的几次中,我将使用这个创建的Django容器映像作为pod部署到Kubernetes环境中。
由于本次只是准备阶段,我将简略地记录下来。

建立形象

k8s_3-ページ14.drawio.png

在Harbor上准备一个专用项目。

2023071201.png

创建基于Django的容器镜像

基本上,我將以我之前創建的以下內容作為基礎。

 

选择了基于Ubuntu 22.04的容器映像作为基础。

root@k8s-master:~/yaml/ins-env# docker images
REPOSITORY                             TAG       IMAGE ID       CREATED       SIZE
ubuntu                                 latest    5a81c4b8502e   10 days ago   77.8MB

root@k8s-master:~/yaml/ins-env# docker run -itd --name django ubuntu:latest
2df08d54e76e569363db2374d83bf079ba6d1f264a9cbc6aa0c4bd8670c3fb4f
root@k8s-master:~/yaml/ins-env# docker ps
CONTAINER ID   IMAGE           COMMAND       CREATED         STATUS         PORTS     NAMES
2df08d54e76e   ubuntu:latest   "/bin/bash"   3 seconds ago   Up 2 seconds             django
root@k8s-master:~/yaml/ins-env# docker exec -it django /bin/bash
root@2df08d54e76e:/# uname -a
Linux 2df08d54e76e 5.15.0-76-generic #83-Ubuntu SMP Thu Jun 15 19:16:32 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
root@2df08d54e76e:/# cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04.2 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.2 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy

在容器内执行以下命令。

apt update
apt upgrade
apt install -y python3-pip vim 
pip --version
apt install -y software-properties-common
add-apt-repository ppa:deadsnakes/ppa
apt update
apt list python3.*
apt install -y python3.12 python3.12-venv
pip install django

从容器中注销并创建一个映像。

root@k8s-master:~/yaml/ins-env# docker commit django django-image
sha256:bc37f587743f23a4e350879c4a2cca5631f97b4ee8d6859e9178e4a1e59b3e07
root@k8s-master:~/yaml/ins-env# docker images
REPOSITORY                             TAG       IMAGE ID       CREATED         SIZE
django-image                           latest    bc37f587743f   6 seconds ago   639MB
ubuntu                                 latest    5a81c4b8502e   10 days ago     77.8MB

我会进行标签打上。

root@k8s-master:~/yaml/ins-env# docker tag django-image 192.168.2.37:30003/ins-env/django:1.0
root@k8s-master:~/yaml/ins-env# docker images
REPOSITORY                             TAG       IMAGE ID       CREATED         SIZE
192.168.2.37:30003/ins-env/django      1.0       bc37f587743f   4 minutes ago   639MB
django-image                           latest    bc37f587743f   4 minutes ago   639MB
192.168.2.37:30003/k8s-images/ubuntu   1.0       5a81c4b8502e   10 days ago     77.8MB
ubuntu                                 latest    5a81c4b8502e   10 days ago     77.8MB

我会在Harbor登录后进行推送。

root@k8s-master:~/yaml/ins-env# docker login 192.168.2.37:30003
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
root@k8s-master:~/yaml/ins-env# docker push 192.168.2.37:30003/ins-env/django:1.0
The push refers to repository [192.168.2.37:30003/ins-env/django]
8a75255d3619: Pushed
59c56aee1fb4: Pushed
1.0: digest: sha256:44c98246ef58d3c14ef807d379774c976ea52bddce4e721bbb6bda6fad038e80 size: 742
2023071202.png

在Django容器中创建并运行项目。

我们将基于之前创建的Django镜像:1.0再次部署Django容器。为了在Django运行时打开8000端口以进行访问,需要留意在容器运行时执行的命令。

root@k8s-master:~/yaml/ins-env# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
root@k8s-master:~/yaml/ins-env# docker images
REPOSITORY                             TAG       IMAGE ID       CREATED          SIZE
192.168.2.37:30003/ins-env/django      1.0       bc37f587743f   30 minutes ago   639MB
django-image                           latest    bc37f587743f   30 minutes ago   639MB
ubuntu                                 latest    5a81c4b8502e   10 days ago      77.8MB
192.168.2.37:30003/k8s-images/ubuntu   1.0       5a81c4b8502e   10 days ago      77.8MB
root@k8s-master:~/yaml/ins-env# docker run -itd --name django -p 8000:8000 192.168.2.37:30003/ins-env/django:1.0
5db09c0dd3d967758acb11991752413a0c27465c0ef1476a8045d272418f90ad
root@k8s-master:~/yaml/ins-env# docker ps
CONTAINER ID   IMAGE                                   COMMAND       CREATED         STATUS         PORTS                                       NAMES
5db09c0dd3d9   192.168.2.37:30003/ins-env/django:1.0   "/bin/bash"   5 seconds ago   Up 4 seconds   0.0.0.0:8000->8000/tcp, :::8000->8000/tcp   django

暂时随意地运行项目,为了允许所有IP地址的访问,还需要修改settings.py文件。

root@k8s-master:~/yaml/ins-env# docker exec -it django /bin/bash
root@5db09c0dd3d9:/# cd /home
root@5db09c0dd3d9:/home# django-admin startproject testProject
root@5db09c0dd3d9:/home# cd testProject/
root@5db09c0dd3d9:/home/testProject# python3 manage.py startapp testApplication
root@5db09c0dd3d9:/home/testProject# ls
manage.py  testApplication  testProject
root@5db09c0dd3d9:/home/testProject# cd testProject
root@5db09c0dd3d9:/home/testProject/testProject# ls -ltr
total 20
-rw-r--r-- 1 root root  399 Jul  9 16:18 wsgi.py
-rw-r--r-- 1 root root  767 Jul  9 16:18 urls.py
-rw-r--r-- 1 root root 3236 Jul  9 16:18 settings.py
-rw-r--r-- 1 root root  399 Jul  9 16:18 asgi.py
-rw-r--r-- 1 root root    0 Jul  9 16:18 __init__.py
drwxr-xr-x 2 root root 4096 Jul  9 16:20 __pycache__
root@5db09c0dd3d9:/home/testProject/testProject# vi settings.py
root@5db09c0dd3d9:/home/testProject/testProject# cp -p settings.py settings.py.org
root@5db09c0dd3d9:/home/testProject/testProject# vi settings.py
root@5db09c0dd3d9:/home/testProject/testProject# diff settings.py settings.py.org
28c28
< ALLOWED_HOSTS = ["*"]
---
> ALLOWED_HOSTS = []
root@5db09c0dd3d9:/home/testProject/testProject# cd ..
root@5db09c0dd3d9:/home/testProject# python3 manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
July 09, 2023 - 07:22:01
Django version 4.2.3, using settings 'testProject.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

使用Web浏览器访问运行Docker的服务器的IP地址:8000。看起来没有问题,输出了Django的默认页面。

2023071203.png

将此上传到Harbor作为Django映像:1.1。

root@k8s-master:~/yaml/ins-env# docker commit django django
sha256:af76143a23d157a2b8601cc58f238a475118251bb2b5f219a01207447e235b2d
root@k8s-master:~/yaml/ins-env# docker images
REPOSITORY                             TAG       IMAGE ID       CREATED          SIZE
django                                 latest    af76143a23d1   8 seconds ago    678MB
192.168.2.37:30003/ins-env/django      1.0       bc37f587743f   48 minutes ago   639MB
django-image                           latest    bc37f587743f   48 minutes ago   639MB
ubuntu                                 latest    5a81c4b8502e   10 days ago      77.8MB
root@k8s-master:~/yaml/ins-env# docker tag django 192.168.2.37:30003/ins-env/django:1.1
root@k8s-master:~/yaml/ins-env# docker images
REPOSITORY                             TAG       IMAGE ID       CREATED              SIZE
192.168.2.37:30003/ins-env/django      1.1       af76143a23d1   About a minute ago   678MB
django                                 latest    af76143a23d1   About a minute ago   678MB
192.168.2.37:30003/ins-env/django      1.0       bc37f587743f   49 minutes ago       639MB
django-image                           latest    bc37f587743f   49 minutes ago       639MB
ubuntu                                 latest    5a81c4b8502e   10 days ago          77.8MB
192.168.2.37:30003/k8s-images/ubuntu   1.0       5a81c4b8502e   10 days ago          77.8MB
root@k8s-master:~/yaml/ins-env# docker push 192.168.2.37:30003/ins-env/django:1.1
The push refers to repository [192.168.2.37:30003/ins-env/django]
f036152a0332: Pushed
8a75255d3619: Layer already exists
59c56aee1fb4: Layer already exists
1.1: digest: sha256:3aa7bc46c2c77e97d803f0a90060c73ffaed6ef04b602665bc40288e1135ca79 size: 954
2023071204.png
广告
将在 10 秒后关闭
bannerAds