简体   繁体   English

“docker-compose build”和“docker build”有什么区别?

[英]What is the difference between `docker-compose build` and `docker build`?

What is the difference between docker-compose build and docker build ? docker-compose builddocker build什么区别?

Suppose in a dockerized project path there is a docker-compose.yml file:假设在 dockerized 项目路径中有一个 docker docker-compose.yml文件:

docker-compose build

And

docker build

docker-compose can be considered a wrapper around the docker CLI (in fact it is another implementation in python as said in the comments ) in order to gain time and avoid 500 characters-long lines (and also start multiple containers at the same time). docker-compose可以被认为是 docker CLI 的包装器(实际上,正如评论中所说,它是 python 中的另一个实现),以便获得时间并避免 500 个字符长的行(并且还可以同时启动多个容器) . It uses a file called docker-compose.yml in order to retrieve parameters.它使用一个名为docker-compose.yml的文件来检索参数。

You can find the reference for the docker-compose file format here .您可以在此处找到 docker-compose 文件格式的参考。

So basically docker-compose build will read your docker-compose.yml , look for all services containing the build: statement and run a docker build for each one.所以基本上docker-compose.yml docker-compose build将读取您docker-compose.yml ,查找包含build:语句的所有服务并为每个服务运行一个docker build

Each build: can specify a Dockerfile , a context and args to pass to docker.每个build:可以指定一个Dockerfile ,一个上下文和要传递给Dockerfile参数。

To conclude with an example docker-compose.yml file :以一个示例docker-compose.yml文件结束:

version: '3.2'

services:
  database:
    image: mariadb
    restart: always
    volumes:
      - ./.data/sql:/var/lib/mysql

  web:
    build:
      dockerfile: Dockerfile-alpine
      context: ./web
    ports:
      - 8099:80
    depends_on:
      - database 

When calling docker-compose build , only the web target will need an image to be built.调用docker-compose build ,只有web目标需要构建图像。 The docker build command would look like : docker build命令看起来像:

docker build -t web_myproject -f Dockerfile-alpine ./web

docker-compose build will build the services in the docker-compose.yml file. docker-compose build将在docker-compose.yml文件中构建服务。

https://docs.docker.com/compose/reference/build/ https://docs.docker.com/compose/reference/build/

docker build will build the image defined by Dockerfile. docker docker build将构建由 Dockerfile 定义的镜像。

https://docs.docker.com/engine/reference/commandline/build/ https://docs.docker.com/engine/reference/commandline/build/

Basically, docker-compose is a better way to use docker than just a docker command.基本上,docker-compose 是一种比 docker 命令更好的使用 docker 的方式。

If the question here is if docker-compose build command, will build a zip kind of thing containing multiple images, which otherwise would have been built separately with usual Dockerfile, then the thinking is wrong.如果这里的问题是 docker-compose build 命令是否会构建一个包含多个图像的 zip 类型的东西,否则这些图像会用通常的 Dockerfile 单独构建,那么这种想法是错误的。

Docker-compose build, will build individual images, by going into individual service entry in docker-compose.yml. Docker-compose build,将通过进入 docker-compose.yml 中的单个服务条目来构建单个图像。

With docker images, command, we can see all the individual images being saved as well.使用 docker images 命令,我们还可以看到所有正在保存的单个图像。

The real magic is docker-compose up.真正的魔法是 docker-compose up。

This one will basically create a network of interconnected containers, that can talk to each other with name of container similar to a hostname.这将基本上创建一个相互连接的容器网络,它们可以使用类似于主机名的容器名称相互通信。

Adding to the first answer...添加到第一个答案...

You can give the image name and container name under the service definition.您可以在服务定义下提供图像名称和容器名称。

eg for the service called 'web' in the below docker-compose example, you can give the image name and container name explicitly, so that docker does not have to use the defaults.例如,对于下面 docker-compose 示例中名为“web”的服务,您可以明确给出图像名称和容器名称,这样 docker 就不必使用默认值。

Otherwise the image name that docker will use will be the concatenation of the folder (Directory) and the service name.否则 docker 将使用的图像名称将是文件夹(目录)和服务名称的串联。 eg myprojectdir_web例如 myprojectdir_web

So it is better to explicitly put the desired image name that will be generated when docker build command is executed.所以最好显式地放置在执行 docker build 命令时将生成的所需图像名称。

eg image: mywebserviceImage container_name: my-webServiceImage-Container例如图像:mywebserviceImage container_name:my-webServiceImage-Container

example docker-compose.yml file :示例 docker-compose.yml 文件:

version: '3.2'
services:
  web:
    build:
      dockerfile: Dockerfile-alpine
      context: ./web
    ports:
      - 8099:80
    image: mywebserviceImage
    container_name: my-webServiceImage-Container
    depends_on:
      - database

Few additional words about the difference between docker build and docker-compose build .关于docker builddocker-compose build之间的区别的附加说明很少。 Both have an option for building images using an existing image as a cache of layers.两者都可以选择使用现有图像作为图层缓存来构建图像。

Unfortunately, up until now, at this level, images made by one are not compatible with the other as a cache of layers ( Ids are not compatible ).不幸的是,到目前为止,在这个级别上,一个图像作为图层缓存与另一个不兼容Ids 不兼容)。 However, docker-compose v1.25.0 (2019-11-18) , introduces an experimental feature COMPOSE_DOCKER_CLI_BUILD so that docker-compose uses native docker builder (therefore, images made by docker build can be used as a cache of layers for docker-compose build )然而, docker-compose v1.25.0(2019年11月18日) ,介绍了一种实验性的功能COMPOSE_DOCKER_CLI_BUILD使得docker-compose用途天然搬运工生成器(因此,通过由图像docker build可以用作层的高速缓存docker-compose build

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM