Ameur Baccoucha
Ameur Baccoucha

Reputation: 699

How to make a second container not run until the first one is already started in the docker-compose?

I am trying to run to container through a docker compose using "docker-compose up" but i get a error.

i have to container one for my app nodejs and the other for my database mongo db and they are connected to each other.

version: "2"
services:
  app:
    container_name: sam-node
    restart: always
    image: amazus/sam-apis
    ports:
      - "3000:3000"
    links:
      - mongo
  mongo:
    container_name: sam-mongo
    image: amazus/sam-data
    ports:
      - "27017:27017"

the error that i got is : ailed to connect to server [mongo:27017] on first connect

Upvotes: 0

Views: 90

Answers (1)

Grynets
Grynets

Reputation: 2525

Add depends_on parameter

app:
  container_name: sam-node
  restart: always
  image: amazus/sam-apis
  ports:
    - "3000:3000"
  depends_on: 
    - mongo
  links:
    - mongo

Also, if you'd like to start dependent container after full initialisation of the parent one, you should add command parameter to check if parent container was initialised and built.
Read more here

Why would you ever need this?
Because some services, such as database, could initialise for some time, and if you have some specific logic which requires immediate response, it's better to start dependent container after initialisation completion of the parent one.

Upvotes: 1

Related Questions