Reputation: 15591
Please help me understand the difference between 'image' and 'build' within docker compose
Upvotes: 77
Views: 32195
Reputation: 196
Official documentation that goes in depth on the difference between build
and image
might be of some help here. https://docs.docker.com/compose/compose-file/build/
Long story short, build
needs the path to a Dockerfile that it can use to build an image, but image
goes to a repository to grab an existing image. Both can be included for a service, but
When a service definition includes both the image attribute and a build section, the Compose implementation can’t guarantee a pulled image is strictly equivalent to building the same image from source. Without any explicit user directives, the Compose implementation with Build support MUST first try to pull the image, then build from source if the image was not found on registry. The Compose implementation MAY offer options to customize this behaviour by user request.
Upvotes: 7
Reputation: 1789
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.
Upvotes: 9
Reputation: 1325966
image
means docker compose
will run a container based on that imagedocker 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 beneathbuild:
can be used to specify the filename/path of the Dockerfile.
version: '3'
services:
webapp:
build:
context: ./dir
dockerfile: Dockerfile-alternate
args:
buildno: 1
Upvotes: 72