1、把容器内需要⽤到的⽂件全部复制到容器中
2、设置⼀个⼯作⽬录,把项⽬代码共享到⼯作⽬录中运⾏
3、把需要执⾏的shell命令写在⼀个.sh⽂件中,统⼀执⾏。因为Dockerfile 的指令每执⾏⼀次都会在 docker 上新建⼀层。所以过多⽆意
义的层,会造成镜像膨胀过⼤;RUN 是在 docker build执⾏脚本
4、配置容器启动⾃动执⾏脚本,CMD 在docker run 时运⾏运⾏脚本
DockerFile脚本
1# 基于镜像基础
FROM python:3.9.5
# 复制⽂件到容器中
ADD ./docker/pip.conf /root/.pip/pip.conf
ADD ./docker/sources.list /etc/apt/sources.list
ADD ./docker/openssl.cnf /etc/ssl/openssl.cnf
ADD ./docker/localtime /etc/localtime
ADD ./docker/requirements.txt /var/requirements.txt
ADD ./docker/startup.sh /var/startup.sh
ADD ./docker/install.sh /var/install.sh
# ⼯作⽬录 /var/app
WORKDIR /var/app
RUN /var/install.sh
CMD /var/startup.sh
环境⽂件解析
pip.conf
配置pip国内源,容器内⽂件路径:/root/.pip/pip.conf
[global]
index-url=https://mirrors.aliyun.com/pypi/simple/
sources.list
配置容器环境包下载的国内源,容器内⽂件路径:/etc/apt/sources.list
deb http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb http://mirrors.aliyun.com/debian-security buster/updates main
deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian-security buster/updates main
deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free cont
openssl.cnf
处理ssl.SSLError: [SSL: DH_KEY_TOO_SMALL] dh key too small问题
容器内⽂件路径:/etc/ssl/openssl.cnf
修改⽂件参数即可
[system_default_sect]
MinProtocol = TLSv1.2
#CipherString = DEFAULT@SECLEVEL=2
CipherString = DEFAULT@SECLEVEL=1
localtime
同步系统时间到容器内,容器内⽂件路径:/etc/localtime
install.sh
构建容器时需要执⾏的shell命令
#!/bin/bash
set -e
apt update
apt install -y cron libsasl2-dev python-dev libldap2-dev libssl-dev nodejs
pip install --upgrade pip
pip install -r /var/requirements.txt
chmod -R 777 /var/startup.sh
startup.sh
容器启动时⾃动执⾏的脚本命令,⼀般是直接启动项⽬
#!/bin/bash
set -e
service cron start
pip install -r requirements.txt
python manage.py runserver 0.0.0.0:8000
docker镜像构建
准备好以上⽂件之后,在Dockerfile⽂件⽬录下执⾏⼀下命令构建镜像:
docker build -t image-nam