2

How can I implement the below docker-compose code, but using the docker run command? I am specifically interested in the depends_on part.

version: '2'
services:
  db:
    image: postgres
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
Oliver Angelil
  • 1,099
  • 15
  • 31

2 Answers2

4

depends_on: doesn't map to a docker run option. When you have your two docker run commands you need to make sure you put them in the right order.

docker build -t web_image .
docker network create some_network
docker run --name db --net some_network postgres
# because this depends_on: [db] it must be second
docker run --name web --net some_network ... web_image ...
David Maze
  • 130,717
  • 29
  • 175
  • 215
  • Thanks - however it's possible that the DB is not ready to connect to when the web container wants to connect to it. Do you have any suggestion for such a scenario? – Oliver Angelil Jun 27 '22 at 14:08
  • 1
    Compose `depends_on:` doesn't help that case at all. [Docker Compose wait for container X before starting Y](https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y) has some more discussion of this. In this particular context, any of the setups that involve a `wait-for.sh` script could be run from the host to delay creating the second container until the first one is ready. – David Maze Jun 27 '22 at 14:42
  • 1
    (The standard Kubernetes approach is for the database client to just crash and restart; in your context, `docker run --restart=on-failure`.) – David Maze Jun 27 '22 at 14:43
0

depends-on mean :

Compose implementations MUST guarantee dependency services have been started before starting a dependent service. Compose implementations MAY wait for dependency services to be “ready” before starting a dependent service.

Hence the depends on is not only an order of running and you can use docker-compose instead of docker run and every option in docker run can be in the docker-compose file

mohamed saeed
  • 147
  • 1
  • 6