skip to Main Content

I am having trouble understanding exactly the usage of the “image” option in the following docker-compose file.

  1. In the api service, the image tag looks like its being used to name the built api image as server
  2. However in the redis service, the image tag looks like its telling docker which image to pull from dockerhub.
  3. 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 no app 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


  1. 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 and build is not, then compose attempts to pull the image either from local or from the repository.

    2) If both image and build 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 your worker service, unless app:latest image is available either in local or remote repository, it is going throw error.

    More on the image option here.

    Login or Signup to reply.
  2. 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 the image tag value.

    To answer your questions directly:

    1. The api service will build an image called server if one doesn’t exist
    2. The redis 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.
    3. The worker service uses an image called app.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search