Dockerfile FROM vs Docker-compose IMAGE

Rob*_*ert 7 php docker dockerfile docker-compose docker-image

我目前正在学习 Docker。阅读文档和几篇文章后,我显然有更多的问题而不是答案。目前对我来说最有趣的是:两者之间有什么区别?

FROM some:docker-image
Run Code Online (Sandbox Code Playgroud)

在 Dockerfile 和

image: digitalocean.com/php 
Run Code Online (Sandbox Code Playgroud)

在 docker-compose.yml

我明白他们应该抓取图像并从中创建一个容器。我不明白的是如果我们同时指定两者会发生什么,例如:

version: '3'
services:
  #PHP Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: digitalocean.com/php
Run Code Online (Sandbox Code Playgroud)

docker-compose.yml 和 Dockerfile 都指定了图像。当这些图像不同时会发生什么?docker-compose.yml 会永远赢吗?它会只使用这个“顶部”图像吗?它们会以某种方式重叠吗?或者,也许我都弄错了?

我确实看到了这一点,但我仍然不确定我是否理解发生了什么。

Pau*_*aul 7

区别在于构建运行

将图像视为应用程序,将容器视为运行应用程序的进程。运行应用程序不会更改应用程序。同样,运行容器不会更改图像。图像是通过Dockerfiles使用构建的docker build并且是持久的。容器是根据需要由 、kubernetes 或类似工具从镜像创建的docker rundocker-compose并且是临时的。

The Dockerfile is used by the docker build command to build a new image. In the Dockerfile the first line usually specifies the base image with FROM, i.e. FROM nginx. Subsequent RUN lines in the Dockerfile provide the additional steps that docker build will execute in a shell, within the context of the FROM image, to create the new image. Note that the Dockerfile does not specify the name of the new image. Instead, the new image is named in the -t some/name option to docker build

The docker-compose.yml file specifies a group of images to download and run together as part of a combined service. For example, the docker-compose.yml for a blog could consist of a web server image, an application image, and a database image and would specify not only the images but also possibly how they communicate.

Since docker builds and docker compose are separate operations, there is no conflict or detection of differences. The docker-compose.yml controls what is going to be download and run, and you can also build whatever you like.

Also, as @David Maze mentioned in comments:

If you use both options then Docker Compose will build the image as specified and then tag it using the image: name; this can be confusing if you're putting a "standard" image name there.

我的猜测是,如果您这样做,您最终可能会得到一个与nginxDockerhub 镜像不匹配的镜像,比如在您自己的机器上。不要那样做。相反,请为您构建的任何映像使用唯一的名称。

  • 如果您[使用这两个选项](https://docs.docker.com/compose/compose-file/#build),那么 Docker Compose 将按指定构建映像,然后使用“image:”名称对其进行标记;如果您在那里放置“标准”图像名称,这可能会令人困惑。 (3认同)