简体   繁体   中英

Docker compose:

For example, here is my project structure:

project
     |
     +-- docker-compose.yml
     |    
     +-- hq-backend
     |  +-- Dockerfile
     |  +-- .env.docker-compose

here is my docker compose file:

db:
  container_name: hq-db
  image: postgres:latest
  ports:
    - "5432:5432"
  environment:
    POSTGRES_USER: user_name
    POSTGRES_PASSWORD: password
    POSTGRES_DB: db_name

mongo:
  container_name: hq-mongo
  image: mongo:3.2.9
  tty: true
  stdin_open: true
  ports:
    - "27027:27027"
  volumes:
    - mongo:/data/db

hq-backend:
  env_file:
    - ../hq-backend/.env.docker_compose
  build: ../hq-backend/
  dockerfile: Dockerfile
  ports:
    - "8686:8686"
  volumes:
    - ".:/webapp"
  links:
    - db
    - mongo

When I tried to run docker-compose up . I always meet this exception at the end:

ERROR: for hq-backend Cannot start service hq-backend: Cannot link to a non running container: /hq-mongo AS /project_hq-backend_1/mongo

Here is my environment file:

ENV=dev
DB_HOST=db
DB_ADDRESS=db
DB_PORT=5432
POSTGRES_DB=db_name
POSTGRES_USER=username
POSTGRES_PASSWORD=password

DB_MONGO_ADDRESS=mongo
DB_MONGO_PORT=27027

This problem is only with mongo service, not db service. After that, if I tried to run docker-compose up again, no error found, but my hq-backend will throw exception because cannot connect to mongo db. In other word, I just meet this problem when rebuild hq-backend from scratch.

Please tell me about this problem.

Remove tty and stdin_open options from docker-compose file. i suppose that it's messing with how docker-compose is handling containers. if You want to execute commands inside mongo container then after compose up just login into it using: docker exec -t -i hq-mongo /bin/bash

By the way - You may consider to use docker-compose version 2 or 3 and depends_on instead of links https://docs.docker.com/compose/compose-file/compose-file-v2/

Try adding depends_on to hq-backend at the same level as links. For example:

depends_on:
  - mongo
  - db
links:
  - db
  - mongo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM