简体   繁体   English

Dockerfile FROM vs Docker-compose IMAGE

[英]Dockerfile FROM vs Docker-compose IMAGE

I am currently in the process of learning Docker.我目前正在学习 Docker。 After reading the docs and a few articles I obviously have more questions than answers.阅读文档和几篇文章后,我显然有更多的问题而不是答案。 Most intriguing one for me at the moment is: what is the difference between目前对我来说最有趣的是:两者之间有什么区别?

FROM some:docker-image

In Dockerfile and在 Dockerfile 和

image: digitalocean.com/php 

In docker-compose.yml在 docker-compose.yml

I do understand that they should grab the image and create a container from it.我明白他们应该抓取图像并从中创建一个容器。 What I don't understand is what happens if we will specify both at the same time, for example:我不明白的是,如果我们同时指定两者会发生什么,例如:

version: '3'
services:
  #PHP Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: digitalocean.com/php

Both docker-compose.yml and Dockerfile have images specified in them. docker-compose.yml 和 Dockerfile 都指定了图像。 What happens when those images are different?当这些图像不同时会发生什么? Will docker-compose.yml always win and for that one service? docker-compose.yml 会永远赢吗? Will it use only this 'top' image?它会只使用这个“顶部”图像吗? Will they overlap somehow?它们会以某种方式重叠吗? Or maybe I got it all wrong?或者,也许我都弄错了?

I did see this but I am still not sure if I understand what is going on.我确实看到了这一点,但我仍然不确定我是否理解发生了什么。

The difference is build vs. run区别在于构建运行

Think of images as apps and containers as a process running an app.将图像视为应用程序,将容器视为运行应用程序的进程。 Running an app does not change the app.运行应用程序不会改变应用程序。 Running a container likewise does not change the image.运行容器同样不会改变镜像。 Images are built from Dockerfiles using docker build and are persistent.图像是使用Dockerfiles docker buildDockerfiles docker build并且是持久的。 Containers are created as needed by docker run , docker-compose , kubernetes, or similar tools from images and are intended to be temporary.容器由 docker docker run 、 docker docker-compose 、 kubernetes 或类似工具根据需要创建,并且是临时的。

The Dockerfile is used by the docker build command to build a new image. Dockerfile docker build命令使用Dockerfile来构建新映像。 In the Dockerfile the first line usually specifies the base image with FROM , ie FROM nginx .Dockerfile ,第一行通常用FROM指定基础镜像,即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. Dockerfile中的后续RUN行提供了Dockerfile docker build将在FROM镜像的上下文中的 shell 中执行以创建新镜像的附加步骤。 Note that the Dockerfile does not specify the name of the new image.请注意, Dockerfile未指定新映像的名称。 Instead, the new image is named in the -t some/name option to docker build相反,新图像在docker build-t some/name选项中命名

The docker-compose.yml file specifies a group of images to download and run together as part of a combined service. docker-compose.yml文件指定一组图像作为组合服务的一部分一起下载和运行。 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.例如,博客docker-compose.yml可能包含一个 Web 服务器映像、一个应用程序映像和一个数据库映像,并且不仅会指定图像,还可能会指定它们的通信方式。

Since docker builds and docker compose are separate operations, there is no conflict or detection of differences.由于 docker builds 和 docker compose 是单独的操作,因此不存在冲突或差异检测。 The docker-compose.yml controls what is going to be download and run, and you can also build whatever you like. docker-compose.yml控制将要下载和运行的内容,您还可以构建任何您喜欢的内容。

Also, as @David Maze mentioned in comments:另外,正如@David Maze 在评论中提到的:

If you use both options then Docker Compose will build the image as specified and then tag it using the image: name;如果您同时使用这两个选项,则 Docker Compose 将按照指定构建镜像,然后使用镜像:名称; this can be confusing if you're putting a "standard" image name there.如果您在那里放置“标准”图像名称,这可能会令人困惑。

My guess is if you do that, you might end up with an image, say nginx on your own machine that does not match the Dockerhub image.我的猜测是,如果你这样做,你最终可能会得到一个图像,比如你自己机器上的nginx与 Dockerhub 图像不匹配。 Don't do that.不要那样做。 Instead, use unique names for any images that you build.相反,为您构建的任何图像使用唯一名称。

what happens if we will specify both (image and Dockerfile) at the same time:如果我们同时指定(图像和 Dockerfile)会发生什么:

If the image does not exist, docker-compose attempts to pull it, unless you have also specified build, in which case it builds it using the specified options and tags it with the specified tag.如果映像不存在,docker-compose 会尝试拉取它,除非您还指定了 build,在这种情况下,它会使用指定的选项构建它并使用指定的标签对其进行标记。 https://docs.docker.com/compose/compose-file/compose-file-v3/#image https://docs.docker.com/compose/compose-file/compose-file-v3/#image

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

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