What we want to do:
We want to use docker-compose to link one already running container (A) to another container (B) by container name. We use "external-link" as both containers are started from different docker-compose.yml files.
Problem:
Container B fails to start with the error although a container with that name is running.
ERROR: for container_b Cannot start service container_b: Cannot link to a non running container: /PREVIOUSLY_LINKED_ID_container_a_1 AS /container_b_1/container_a_1
output of "docker ps":
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
RUNNING_ID container_a "/docker-entrypoint.s" 15 minutes ago Up 15 minutes 5432/tcp container_a_1
Sample code:
docker-compose.yml of Container B:
container_b:
external_links:
- container_a_1
What differs this question from the other "how to fix"-questions:
- we can't use "sudo service docker restart" (which works) as this is a production environment
- We don't want to fix this every time manually but find the reason so that we can
- understand what we are doing wrong
- understand how to avoid this
Assumptions:
- It seems like two instances of the container_a exist (RUNNING_ID and PREVIOUSLY_LINKED_ID)
- This might happen because we
- rebuilt the container via docker-compose build and
- changed the forwarded external port of the container (808
01:8080)
Comment
- Do not use
docker-compose down
as suggested in the comments, this removes volumnes!