需求描述
简单说,就是创建一个服务型的镜像,即运行基于该镜像创建的容器时,基于该容器自动开启一个服务。具体来说,是创建一个部署了nginx
,uwsgi
,python
,django
项目代码的镜像,运行基于该镜像创建的容器时,自动开启nginx
,uwsgi
等服务。简单理解就是在容器内部,通过nginx
+uwsgi
部署Django项目
实践环境
CentOS Linux release 7.9.2009
实践过程
实践过程1
Django
项目代码组织结构
env_setup.sh
#!/bin/bash yum install -y wget gcc make zlib* openssl-devel sqlite-devel wget http://nginx.org/download/nginx-1.10.3.tar.gz tar -xvzf nginx-1.10.3.tar.gz cd nginx-1.10.3 ./configure --prefix=/usr/local/nginx make && make install cp -f /opt/DjServer/docker/nginx.conf /usr/local/nginx/conf/nginx.conf chmod 755 /usr/local/nginx/conf/nginx.conf ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx cd .. && rm -rf nginx-1.10.3* wget http://www.sqlite.org/2019/sqlite-autoconf-3280000.tar.gz tar -xvzf sqlite-autoconf-3280000.tar.gz cd sqlite-autoconf-3280000 ./configure --prefix=/usr/local/sqlite make && make install mv /usr/bin/sqlite3 /usr/bin/sqlite3_old ln -s /usr/local/sqlite/bin/sqlite3 /usr/bin/sqlite3 echo "/usr/local/sqlite/lib" > /etc/ld.so.conf.d/sqlite3.conf ldconfig cd .. && rm -rf sqlite-autoconf-* wget https://www.python.org/ftp/python/3.6.13/Python-3.6.13.tgz tar -xvzf Python-3.6.13.tgz cd Python-3.6.13 ./configure --prefix=/usr/local/python36 make && make install cd .. && rm -rf Python-3.6.13* ln -s /usr/local/python36/bin/python3 /usr/bin/python3 ln -s /usr/local/python36/bin/pip3 /usr/bin/pip3 ls pip3 install -r /opt/DjServer/requirements.txt ln -s /usr/local/python36/bin/uwsgi /usr/bin/uwsgi mkdir -p /var/log/uwsgi
start.sh
#!/bin/bash `uwsgi --ini uwsgi.ini &` && nginx -c /usr/local/nginx/conf/nginx.conf -g "daemon off;"
nginx.conf
worker_processes 16; events { worker_connections 65535; } http { include mime.types; default_type application/octet-stream; keepalive_timeout 65; reset_timedout_connection on; server { listen 80; server_name localhost; charset utf-8; #access_log logs/access.log; access_log off; error_log logs/error.log; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location / { uwsgi_send_timeout 60; uwsgi_connect_timeout 60; uwsgi_read_timeout 600; include uwsgi_params; uwsgi_pass 127.0.0.1:8000; } } }
requirements.txt
Django==3.0.6 uwsgi==2.0.8
前置准备工作
如下,创建一个干净的目录(例中为myworkspace
),用于创建镜像,并将解压后的Django
项目代码存放到该目录,然后在该目录下新建并编写dockerfile
,.dockerignore
文件。
# pwd /root/myworkspace # ls -a . .. DjServer dockerfile .dockerignore
dockerfile
FROM centos:7.9.2009 WORKDIR /opt/DjServer COPY ./DjServer . RUN chmod 755 ./docker/env_setup.sh \ && sh ./docker/env_setup.sh \ && chmod 755 ./docker/start.sh EXPOSE 80 ENTRYPOINT ["./docker/start.sh"]
.dockerignore
*/__pycache__
创建服务镜像
如下,创建djserver:v1.0
镜像
# docker build -t djserver:v1.0 . ...略 Successfully tagged djserver:v1.0
创建容器
基于上述镜像创建名为dj80
的容器,映射宿主主机80
端口到容器的80
端口,并为容器设置环境变量
# docker run --name dj9003 -p 9003:80 -e hostPort=10.1.66.163:9003 -d -it djserver:v1.0
测试验证
# curl http://10.7.0.236:9003/loadapi/api/v1/ip_forward # 10.7.0.236为容器所在宿主机
实践过程2
Django项目代码组织结构
注意:代码结构中的requirements.txt
暂时没用到,对该文件的定位是,存放创建基础镜像时新增requirements.txt文件所包含依赖包除外的其它依赖包。
创建基础镜像前置准备工作
如下,创建一个干净的目录(例中为env_build
),用于创建提供软件运行环境的基础镜像,并在该目录下新建并编写dockerfile
,新建env_conf
目录,然后在env_conf
目录下新建env_setup.sh
, requirements.txt
# pwd /root/env_build # ls -aR .: . .. env_conf env_dockerfile ./env_conf: . .. env_setup.sh requirements.txt
env_dockerfile
FROM centos:7.9.2009 COPY ./env_conf . RUN chmod 755 env_setup.sh \ && sh env_setup.sh \ && rm -f env_setup.sh requirements.txt
env_setup.sh
#!/bin/bash yum install -y wget gcc make zlib* openssl-devel sqlite-devel wget http://nginx.org/download/nginx-1.10.3.tar.gz tar -xvzf nginx-1.10.3.tar.gz cd nginx-1.10.3 ./configure --prefix=/usr/local/nginx make && make install cd .. && rm -rf nginx-1.10.3* ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx wget http://www.sqlite.org/2019/sqlite-autoconf-3280000.tar.gz tar -xvzf sqlite-autoconf-3280000.tar.gz cd sqlite-autoconf-3280000 ./configure --prefix=/usr/local/sqlite make && make install mv /usr/bin/sqlite3 /usr/bin/sqlite3_old ln -s /usr/local/sqlite/bin/sqlite3 /usr/bin/sqlite3 echo "/usr/local/sqlite/lib" > /etc/ld.so.conf.d/sqlite3.conf ldconfig cd .. && rm -rf sqlite-autoconf-* wget https://www.python.org/ftp/python/3.6.13/Python-3.6.13.tgz tar -xvzf Python-3.6.13.tgz cd Python-3.6.13 ./configure --prefix=/usr/local/python36 make && make install cd .. && rm -rf Python-3.6.13* ln -s /usr/local/python36/bin/python3 /usr/bin/python3 ln -s /usr/local/python36/bin/pip3 /usr/bin/pip3 pip3 install -r requirements.txt ln -s /usr/local/python36/bin/uwsgi /usr/bin/uwsgi mkdir -p /var/log/uwsgi
requirements.txt
Django==3.0.6 uwsgi==2.0.8
注意:这里对requirements.txt
的定位是,用于存放一定需要安装,并且极少改动(比如更换版本)的软件依赖包
创建基础镜像
如下,创建djbase:v1.0
基础镜像,为运行Django
项目提供所需的软件运行环境
# docker build --no-cache -t djbase:v1.0 -f env_dockerfile . ...略docker build --no-cache -t djbase:v1.0 -f env_dockerfile . Successfully tagged djbase:v1.0
创建服务镜像前置准备工作
创建一个干净的目录(例中为server_build
),用于创建最终服务镜像,并将解压后的Django
项目代码存放到该目录,然后在该目录下新建并编写dockerfile
,.dockerignore
文件。
# pwd /root/server_build # ls -a . .. DjServer dockerfile .dockerignore
dockerfile
FROM djbase:v1.0 WORKDIR /opt/DjServer RUN mkdir -p /var/log/uwsgi COPY ./DjServer/docker/nginx.conf . RUN mv -f ./nginx.conf /usr/local/nginx/conf/nginx.conf && chmod 755 /usr/local/nginx/conf/nginx.conf COPY ./DjServer . RUN chmod 755 ./docker/start.sh EXPOSE 80 ENTRYPOINT ["./docker/start.sh"]
.dockerignore
*/__pycache__
docker/start.sh
#!/bin/bash `uwsgi --ini uwsgi.ini &` && nginx -c /usr/local/nginx/conf/nginx.conf -g "daemon off;"
docker/nginx.conf
同“实践过程1”
创建服务镜像
如下,创建djserver:v1.0
服务镜像
# docker build -t djserver:v1.0 . ...略 Successfully tagged djserver:v1.0
创建容器
基于上述镜像创建名为djserver
的容器,映射宿主主机80
端口到容器的80
端口
# docker run --name dj9003 -p 9003:80 -e hostPort=10.1.66.163:9003 -d -it djserver:v1.0