Docker 基於Dockerfile創建鏡像實踐

授客 2021-08-15 22:30:11 阅读数:972

本文一共[544]字,预计阅读时长:1分钟~
docker dockerfile

需求描述

簡單說,就是創建一個服務型的鏡像,即運行基於該鏡像創建的容器時,基於該容器自動開啟一個服務。具體來說,是創建一個部署了nginxuwsgipythondjango項目代碼的鏡像,運行基於該鏡像創建的容器時,自動開啟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
版权声明:本文为[授客]所创,转载请带上原文链接,感谢。 https://gsmany.com/2021/08/20210815222959456k.html