Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between 'image' and 'build' within docker compose

Please help me understand the difference between 'image' and 'build' within docker compose

like image 692
meallhour Avatar asked Dec 16 '15 15:12

meallhour


People also ask

What is the difference between Docker build and Docker compose?

The three primary differences between the Dockerfile and docker-compose are: The Dockerfile is used to build images while the docker-compose. yaml file is used to run images. The Dockerfile uses the docker build command, while the docker-compose.

Does Docker compose build images?

docker-compose build : This command builds images in the docker-compose. yml file. The job of the build command is to get the images ready to create containers, so if a service is using the prebuilt image, it will skip this service.

What is image build in Docker?

Description. The docker build command builds Docker images from a Dockerfile and a “context”. A build's context is the set of files located in the specified PATH or URL . The build process can refer to any of the files in the context. For example, your build can use a COPY instruction to reference a file in the context ...

What is build an image?

A build is the process of transforming input parameters into a resulting object. Most often, the process is used to transform input parameters or source code into a runnable image. A BuildConfig object is the definition of the entire build process.


2 Answers

  • image means docker compose will run a container based on that image
  • build means docker compose will first build an image based on the Dockerfile found in the path associated with build (and then run a container based on that image).

PR 2458 was eventually merged to allow both (and use image as the image name when building, if it exists).

therobyouknow mentions in the comments:

dockerfile: as a sub-statement beneath build: can be used to specify the filename/path of the Dockerfile.

version: '3' services:   webapp:     build:       context: ./dir       dockerfile: Dockerfile-alternate       args:         buildno: 1 
like image 87
VonC Avatar answered Sep 20 '22 18:09

VonC


build: expects dockerfile path as an argument, it will build an image first and then use the image to create a container.

image: expects existing image name as argument , it will launch container using this image.

Example:docker-compose.yaml

version: '3' services:   service1:     build: .     ports:       - "5000:5000"   service2:     image: "redis:alpine" 

service1 will build an image first based on Dockerfile of current path and run container based on this image.

service2 will download "redis:alpine" image from docker hub and run container on downloaded image.

like image 22
Shahid Hussain Avatar answered Sep 23 '22 18:09

Shahid Hussain