1. 构建django的docker镜像

2022-01-17 13:46:14 浏览数 (1)

构建大致步骤

采用centos镜像作为地板镜像,然后安装python3、pip3,再安装django即可。

但是在构建的过程中也出现了不少问题。

  • 问题一:在Django 2.2中启动开发服务器时处理SQLite3错误
  • 问题二:Django配置runserver实现远程访问

解决的方式已经详细写在了上面两个链接答案中。

构建镜像的文件目录

代码语言:javascript复制
[root@server01 django]# tree
.
├── Dockerfile   # 镜像构建Dockerfile
├── restart_service.sh  # 服务启动脚本
├── run_DockerFile.sh  # 镜像构建脚本
└── work   # django项目
    ├── db.sqlite3
    ├── manage.py
    ├── polls
...

Dockerfile

代码语言:javascript复制
[root@server01 django]# cat Dockerfile
# Using Centos for base image
FROM centos:latest
# author lijw
MAINTAINER lijw
# start run
    # step1: install python3 pip3
RUN yum install -y wget && 
    cd /opt && 
    wget "https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tgz" && 
    yum install libffi-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make -y && 
    cd /opt && 
    tar -zxvf Python-3.7.1.tgz && 
    cd /opt/Python-3.7.1 && 
    ./configure --prefix=/usr/local/python3 && 
    make && make install && 
    ln -s /usr/local/python3/bin/python3 /usr/bin/python3 && 
    ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3 && 
    pip3 install --upgrade pip && 
    pip3 install ipython && 
    ln -s /usr/local/python3/bin/ipython3 /usr/bin/ipython3 && 
    # step2: install Django
    pip3 install Django && 
    # step3: install sqlite3
    cd ~ && 
    wget https://www.sqlite.org/2019/sqlite-autoconf-3270200.tar.gz && 
    tar -zxvf sqlite-autoconf-3270200.tar.gz && 
    cd sqlite-autoconf-3270200 && 
    ./configure --prefix=/usr/local && 
    make && make install && 
    cd ~ && 
    rm -rf sqlite-autoconf-3270200.tar.gz && 
    rm -rf sqlite-autoconf-3270200 && 
    mv /usr/bin/sqlite3  /usr/bin/sqlite3_old && 
    ln -s /usr/local/bin/sqlite3 /usr/bin/sqlite3 && 
    echo 'export LD_LIBRARY_PATH="/usr/local/lib"' >> /root/.bashrc && 
    source /root/.bashrc && 
    # step4: clear yum
    yum clean all
# Copy Django code to image
COPY work /work
# Start Django Service
CMD export LD_LIBRARY_PATH="/usr/local/lib" && 
    cd /work && python3 manage.py runserver 0.0.0.0:8000
[root@server01 django]#

编译生成镜像

代码语言:javascript复制
[root@server01 django]# cat run_DockerFile.sh
docker build -t django .
[root@server01 django]#

启动服务脚本

代码语言:javascript复制
[root@server01 django]# cat restart_service.sh
docker stop django
docker rm django
docker run --name django --restart always 
       -h djangoServer 
       -d -p 8000:8000 
       django
[root@server01 django]#

运行中的docker进程

代码语言:javascript复制
[root@server01 django]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                            NAMES
1ef06015ff9b        django              "/bin/sh -c 'expor..."   2 minutes ago       Up 2 minutes        0.0.0.0:8000->8000/tcp                           django
093c3282ed6e        gogs/gogs           "/app/gogs/docker/..."   22 hours ago        Up 22 hours         0.0.0.0:10022->22/tcp, 0.0.0.0:10080->3000/tcp   gogs
[root@server01 django]#

测试访问

代码语言:javascript复制
[root@server01 ~]# curl http://172.16.0.4:8000/polls/


<link rel="stylesheet" type="text/css" href="/static/polls/style.css">


    <ul>
    



        <li><a href="/polls/3/">What&#39;s up3?</a></li>
    



        <li><a href="/polls/2/">What&#39;s up2?</a></li>
    



        <li><a href="/polls/1/">What&#39;s up?</a></li>
    
    </ul>
[root@server01 ~]#

缺点

本次编译镜像安装了比较多的服务,所以镜像尽管采用了串联的方式去编写,清除了不需要的yum缓存以及安装文件,镜像依然较大。

代码语言:javascript复制
[root@server01 django]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
django                latest              1aca5ef73690        9 minutes ago       816 MB
<none>                <none>              a7c9243edeb0        10 minutes ago      816 MB
<none>                <none>              12567dcc7855        12 minutes ago      816 MB
<none>                <none>              a52e87c281be        15 minutes ago      816 MB
<none>                <none>              14a3e40c1302        17 minutes ago      816 MB
<none>                <none>              12c2262cbed5        34 minutes ago      816 MB
<none>                <none>              db9c18fbcbc7        4 hours ago         841 MB
docker.io/gogs/gogs   latest              08fa8ee955da        5 weeks ago         99.1 MB
docker.io/centos      latest              9f38484d220f        7 weeks ago         202 MB

在编译镜像的过程会产生很多none镜像,都应该删除掉。

代码语言:javascript复制
[root@server01 django]# docker images | grep none | awk '{print $3}' | xargs docker rmi
Deleted: sha256:a7c9243edeb050e337818ea0ac9ece2b33ffc87febba62f033b068a1d2be9121
Deleted: sha256:8ef3d81763b3c50d19891d5a1bda860bbe442021cd0fb4f0ecb3f6e2dae4a74d
Deleted: sha256:df6f1865b5dfaa1950e055de25c88740dc447b571c8dbe25c2d9edf04fbea51f
Deleted: sha256:12567dcc7855d17fa93d22a97ae7091279d8b9fe88f65329c421604e751d5841
Deleted: sha256:b5ba6bf6bae1549c3c0a40745b33946096066c6cb7a64cd6847ae22230ecffeb
Deleted: sha256:1aaa5122053eba7e6ac9f3f9d46cd5068549b809866b8cc2352c33df5a38e66b
Deleted: sha256:a52e87c281be0935093821c1e80e3f12c006ba2ad7ad72e8c745efa7f5e9b18e
Deleted: sha256:d9b5547926368d40749fe641dbe06e25b7c78e0cf4ddb36ae67b87afbdabc07e
Deleted: sha256:4bf3ff836e1f6ef5c5caeb750a62ca61f24737e8c756de4424773feb7d04dc6b
Deleted: sha256:14a3e40c1302dffc4298685647dfb7e91cbf5e33496492dc64de6e67cca9626d
Deleted: sha256:3a5a02f60e80b29e9a096d30fcdf664ae52dfa658461bdf092dfd2d42eb04391
Deleted: sha256:d51b11a568529fb4a720f3b5bd0769c266db5af7b363f24d01064f35150cca1c
Deleted: sha256:12c2262cbed51e3cc46b19472f63570a9e9cbf748e56d4581f83e11388e141c9
Deleted: sha256:31b5244e8ce7d1109815023e11641ed09b9171f5611068d83775747570fb36c4
Deleted: sha256:916305e485efd3185c9aa8866fbfc27c3953490f11b7d8d2756d2cb9d5f433e5
Deleted: sha256:db9c18fbcbc71f4a9526a68585c35be31f65730b891955f0b08abc376fe4a89e
Deleted: sha256:bd24b4c744ed1fa90f5b2896c8b6c96c714fb3ba19693f139bb5bb65030f12ce
[root@server01 django]#
[root@server01 django]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
django                latest              1aca5ef73690        11 minutes ago      816 MB
docker.io/gogs/gogs   latest              08fa8ee955da        5 weeks ago         99.1 MB
docker.io/centos      latest              9f38484d220f        7 weeks ago         202 MB
[root@server01 django]#

0 人点赞