2

New to Docker

Background: I have written a docker-compose.yml which when run with docker-compose up will build and run nicely on my box. Note: my docker-compose.yml downloads the Postgres image.

version: '3'

services:
  api:
     image: conference_api 
     container_name: conference_api
     build:
       context: .
     ports:
       - 5000:80
     environment:
       ASPNETCORE_ENVIRONMENT: Production
     depends_on:
       - postgres
  postgres:
    image: postgres:9.6.3
    container_name: conference_db
    environment:
      POSTGRES_DB: conference
      POSTGRES_USER: conf_app
      POSTGRES_PASSWORD: docker
    ports:
      - 5432:5432
    volumes:
      - ./db:/docker-entrypoint-initdb.d

I then publish my docker image to docker hub.

On a fresh machine I use docker pull to pull my image and then I run it.

I get errors saying bascially "I can't find the database". The Postgres Image was not also downloaded.

My Question: When I pull my image, how can I get the Postgres image to also download as it is a dependency of my Image.

Ian Vink
  • 66,960
  • 104
  • 341
  • 555
  • 1
    On the "fresh machine," why aren't you using `docker-compose up`? That should pull all the needed images when you run that. – Joshua Schlichting May 21 '20 at 19:17
  • I'm trying to get the big picture here. So when I publish an Image to Docker Hub, if it depends on 10 other images to run, I would need to give clients a docker-compose.yaml file to run my image? – Ian Vink May 21 '20 at 19:19
  • @IanVink Yes, in-line a `docker-compose.yml` into the repo's (and thus, DockerHub's) `README.md`. In fact, your `conference_api` service requires *exactly this docker-compose* because it depends on a service called exactly `postgres` running on the default port. This is why I would expect to get a compose file and run `docker-compose up` instead of `docker pull`, which will just pull required base images anyway. – msanford May 22 '20 at 18:43
  • More big picture: we do this at my company, and we have many base images that are built upon to make different applications. The only use I have encountered for anyone to run `docker pull` was to update a base image whose version tag didn't change, or to pull the newest `:latest`. – msanford May 22 '20 at 18:45

1 Answers1

2

Use docker-compose pull --include-deps [SERVICE...].

Per the documentation:

--include-deps Also pull services declared as dependencies

This would require the users of your image to have your docker-compose.yml file.

Another option would be to use docker in docker, so docker-compose.yml would be inside your image where it will execute. However, this appears to be discouraged, even by the developer who made this feature possible.

Joshua Schlichting
  • 3,110
  • 6
  • 28
  • 54