容器化部署之dockerfile07
一、制作镜像方法
第一种方式:
"""
# 提交镜像仓库
docker commit -a cb -m "backend demo" autotpenv backend:v1# 以新镜像启动容器
docker run -itd -v /root/software/autotpsite:/opt -p 8081:8081 --name autotpenv backend:v1 sh /opt/auto_deploy.sh# 加入自定义网络
docker network disconnect bridge autotpenv
docker network connect autotonet autotpenv# 编辑nginx配置文件修改静态地址重启nginx容器:
vim /root/conf/conf.d/default.conf
location /api {proxy_pass http://autotpenv:8081;}
docker restart mynginx
"""
第二种方式:
dockerfile文件
"""
FROM 指定基础镜像
RUN 执行命令,每执行一次镜像就多一层
多条命令时, RUN的正确用法:
RUN command1 && command2 && command3
"""
例:
# 创建Dockerfile文件
FROM nginx
RUN echo '<h1>Hello,Docker!</h1>' > /usr/share/nginx/html/index.html
# 制作镜像
docker build -t mynginxdemo:v1 . # . 找环境里的Dockerfile文件
特殊的镜像scratch
所有镜像的根镜像,相当于object类
如果你以 scratch 为基础镜像的话,意味着你不以任何镜像为基础,即创建一个空白的镜像常用于GO语言开发的应用,因为静态编译的程序自带运行环境(即一段二进制代码可以直接运行)
二、dockerfile部署
"""
# 执行auto_deloy.sh文件(需去掉&& uwsgi uwsgi.ini && tail -f > /dev/null,否则命令会终止)
pip install -r requirements.txt -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple --trusted-host mirrors.tuna.tsinghua.edu.cn && pip install uwsgi -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple --trusted-host mirrors.tuna.tsinghua.edu.cn# 制作镜像文件
FROM python:3.8
COPY . /opt
RUN cd /opt && sh auto_deploy.sh
CMD cd /opt && uwsgi uwsgi.ini && tail -f uwsgi_server.log# 生成镜像
docker build -t autotp:v1 .
CMD后面命令不会执行,启动容器才会执行# 启动容器
docker run -itd --name autotpenv3 autotp:v1
"""