Build context 上下文 增量更新 Docker Compose dockerignore file

 dockerignore file

You can use .dockerignore file to exclude files and directories from the build context. For more information, see .dockerignore file.

You can use a .dockerignore file to exclude files or directories from the build context.

# .dockerignore
node_modules
bar

This helps avoid sending unwanted files and directories to the builder, improving build speed, especially when using a remote builder.

Filename and location

When you run a build command, the build client looks for a file named .dockerignore in the root directory of the context. If this file exists, the files and directories that match patterns in the files are removed from the build context before it's sent to the builder.

 

 

 

docker-compose up -d
 
 

Docker Compose | 菜鸟教程 https://www.runoob.com/docker/docker-compose.html

Compose 使用的三个步骤:

  • 使用 Dockerfile 定义应用程序的环境。

  • 使用 docker-compose.yml 定义构成应用程序的服务,这样它们可以在隔离环境中一起运行。

  • 最后,执行 docker-compose up 命令来启动并运行整个应用程序。

 

实践:

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.

Compose file

Defines a multi-container application.

定义一个实列、多个实例。

 

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

posted @ 2023-06-01 08:55  papering  阅读(175)  评论(0)    收藏  举报