13

I am currently in the process of learning 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

image: digitalocean.com/php 

In 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. What happens when those images are different? Will docker-compose.yml always win and for that one service? 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.

2 Answers 2

8

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. Containers are created as needed by docker run, docker-compose, kubernetes, or similar tools from images and are intended to be temporary.

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.

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. Don't do that. Instead, use unique names for any images that you build.

Sign up to request clarification or add additional context in comments.

8 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.
Guys, both answers are great thanks. I do remember that part of docs however after I saw different configs it was a bit confusing. So if I use both then the image from compose will just give a name to the image. If only specify image in compose for example nginx and don't use any docker Dockerfike it will grab nginx image anyway, this will then be replacement of FROM ?
@Robert It is not a replacement for FROM because there is no image building step in docker compose when there is no Dockerfile. docker compose will create some containers. Images are not containers. Containers are created from images. Think of Images as apps and Containers as a process running an app. Does this help?
@Paul I guess it's just my poor choice of words. What I meant was: when in compose file image is specified(e.g nginx) and there is no Dockerfile specified for that service compose will try to grab that ready image from remote repository. When dockerfile and image are specified in docker compose, Dockerfile will be used to build image and image tag from docker composer will give it a name. However like you said I will test that thoroughly. The reason I am asking so many questions is I don't want to randomly assume that this is always what's gonna happen I rather understand the process.
@Robert : I see where the confusion comes from and why you posted the original question. The image: tag in the YML file is misleading in that it's actually the Dockerfile that builds the image (it is docker-compose which executes the container which runs on that image). In my test, Dockerfile builds FROM nginx:alpine, but docker ps -a displays an image-name of Apache because that's what the image: tag in the YML file says. Contrary to what people say, the image: tag and Dockerfile FROM clause do (can) conflict, and cannot be considered functionally mutually exclusive.
|
0

what happens if we will specify both (image and Dockerfile) at the same time:

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. https://docs.docker.com/compose/compose-file/compose-file-v3/#image

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.