I am having trouble understanding exactly the usage of the “image” option in the following docker-compose file.
- In the
api
service, the image tag looks like its being used to name the built api image asserver
- However in the
redis
service, the image tag looks like its telling docker which image to pull from dockerhub. - Finally, what image is the
worker
service pulling? There is no build tag to refer to a dockerfile nor is there an image being pulled. There is noapp
image defined as well so really confused how this is even working?
version: '3'
services:
api:
build: ./server
image: server
ports:
- "5000:5000"
volumes:
- ./server:/usr/src/server
command: "python -m server.app"
restart: always
env_file:
- ./dev.env
redis:
image: redis:latest
expose:
- "6379"
command: ["sh", "-c", "redis-server --appendonly yes"]
restart: always
depends_on:
- api
worker:
image: app
volumes:
- .:/app
command: "python -m server.worker"
restart: always
env_file:
- ./dev.env
depends_on:
- redis
2
Answers
Just like you have observed,
image
option has two different definitions based on how it is specified in the compose file.1) If
image
option is specified andbuild
is not, then compose attempts to pull the image either from local or from the repository.2) If both
image
andbuild
options are specified, then compose tags the built image with this name.If there is no tag present,
latest
is assumed as the tag. In yourworker
service, unlessapp:latest
image is available either in local or remote repository, it is going throw error.More on the
image
option here.I think the documentation for
image
explains this well.The difference is primarily whether the images exists or not. If it doesn’t exist, the image tag will be used by the
build
to create an image and name it using theimage
tag value.To answer your questions directly:
api
service will build an image calledserver
if one doesn’t existredis
service will run an image `redis:latest. If it exists locally (pulled), that image will be used. If not, it will default to dockerhub and pull it.worker
service uses an image calledapp
.