需求描述
簡單說,就是建立一個服務型的鏡像,即運作基于該鏡像建立的容器時,基于該容器自動開啟一個服務。具體來說,是建立一個部署了
nginx
,
uwsgi
python
django
項目代碼的鏡像,運作基于該鏡像建立的容器時,自動開啟
nginx
,
uwsgi
等服務。簡單了解就是在容器内部,通過
nginx
+
uwsgi
部署Django項目
實踐環境
CentOS Linux release 7.9.2009
實踐過程
實踐過程1
Django
項目代碼組織結構
Django
env_setup.sh
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
start.sh
#!/bin/bash
`uwsgi --ini uwsgi.ini &` && nginx -c /usr/local/nginx/conf/nginx.conf -g "daemon off;"
nginx.conf
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
requirements.txt
Django==3.0.6
uwsgi==2.0.8
前置準備工作
如下,建立一個幹淨的目錄(例中為
myworkspace
),用于建立鏡像,并将解壓後的
Django
項目代碼存放到該目錄,然後在該目錄下建立并編寫
dockerfile
.dockerignore
檔案。
# pwd
/root/myworkspace
# ls -a
. .. DjServer dockerfile .dockerignore
dockerfile
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
.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
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
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
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
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
.dockerignore
*/__pycache__
docker/start.sh
docker/start.sh
#!/bin/bash
`uwsgi --ini uwsgi.ini &` && nginx -c /usr/local/nginx/conf/nginx.conf -g "daemon off;"
docker/nginx.conf
docker/nginx.conf
同“實踐過程1”
# docker build -t djserver:v1.0 .
...略
Successfully tagged djserver:v1.0
# docker run --name dj9003 -p 9003:80 -e hostPort=10.1.66.163:9003 -d -it djserver:v1.0