Build context 上下文 增量更新
实践:
1、拉取代码、静态扫描、单元测试,最后打包成制品,上传;
2、在目标机器上下载1中的制品;
3、目标机器上已经有docker 运行环境;
4、构建镜像、运行实例(强制删除、新建,待优化为更新镜像、平滑启动服务)
2、目标机器代码下载路径
mkdir -p /home/project/myapp
tar zxvf /home/admin/app/myapp_python.tgz -C /home/project/myapp
3、强制删除、新建,待优化为更新镜像、平滑启动服务
https://docs.docker.com/engine/reference/commandline/run
基于同样的镜像构建不同的实例
docker run -e TZ=Asia/Shanghai --name app0 -d myimg;
docker run -e TZ=Asia/Shanghai --name app1 -d myimg;
基于Dockerfile(包含ADD myappcode /app)构建,目录关系
Dockerfile
myappcode
2者同一级目录下
# 新建Docker镜像myapp
mkdir -p /home/project/my_docker_files_for_myapp
tar zxvf /home/admin/app/my_docker_files_for_myapp.tgz -C /home/project/my_docker_files_for_myapp
cd /home/project/my_docker_files_for_myapp/Dockerfile_myapp
echo "清理 myapp 文件"
rm -rf /home/project/my_docker_files_for_myapp/Dockerfile_myapp/myapp
echo "准备docker build src文件"
cp -r /home/project/myapp /home/project/my_docker_files_for_myapp/Dockerfile_myapp/
docker ps -a
docker images
echo "执行 docker image rm --force myapp"
docker image rm --force myapp
echo "执行 docker build -t myapp ."
docker build -t myapp .
echo "查看 docker images -2"
docker images
echo "执行 docker container rm --force myapp"
docker container rm --force myapp
echo "执行 docker run --name myapp -d myapp"
docker run --name myapp -d myapp
【注意】在Dockerfile_myapp文件夹下有Dockerfile文件,同时myapp代码文件也在这个目录下,以便docker 上下文能够找到,在该目录下执行下边的Dockerfile文件,创建镜像,命令: docker build -t myapp .
----》# 使用Python 3.7作为基础镜像
FROM python:3.7
# 将代码到容器中/app目录下
ADD myapp /app
# 将工作目录切换到/app
WORKDIR /app
# 安装依赖包
RUN pip install --no-cache-dir -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
RUN echo world-in-from-Dockerfile
# 启动命令
CMD ["python", "main.py"]
《---
查看命令执行结果,docker images
docker container rm --force myapp;docker image rm --force myapp;docker build -t myapp . ;docker run -e TZ=Asia/Shanghai -i -t myapp;
概念:
Reference documentation | Docker Documentation https://docs.docker.com/reference/
Defines the contents and startup behavior of a single container.
Build context | Docker Documentation https://docs.docker.com/build/building/context/
Multiple <src>
resources may be specified but the paths of files and directories will be interpreted as relative to the source of the context of the build.
Dockerfile reference | Docker Documentation https://docs.docker.com/engine/reference/builder/#copy