Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot link to a running container started by docker-compose

I am setting up my local development environment with docker containers. The docker-compose.yml is like following

version: '2'  services:   db:     image: mongo:3   mq:     image: rabbitmq:3   api:     build: .     image: my_app/api     ports:       - "3000:3000"     links:       - db       - mq     environment:       - NODE_ENV=development 

It starts without error. And docker lists 3 running containers

docker-compose up -d docker ps  e90e5a8b5d33        my_app/api    "/usr/local/bin/node "   0.0.0.0:3000->3000/tcp               my_app_api_1 42bfcd971b16        mongo:3       "/entrypoint.sh mongo"   27017/tcp                            my_app_db_1 a0685a816c47        rabbitmq:3    "/docker-entrypoint.s"   4369/tcp, 5671-5672/tcp, 25672/tcp   my_app_mq_1 

However when I try to link to those running containers from another container

docker run --link my_app_mq_1:mq --link my_app_db_1:db -it worker  

I get error

 docker: Error response from daemon: Cannot link to /my_app_mq_1, as it does not belong to the default network. 

I have also tried

 docker run --link my_app_mq_1:mq --link my_app_db_1:db -it --net default worker  

Same error.

So how can I link to a running container started by docker-compose?

like image 613
George Chen Avatar asked Apr 08 '16 00:04

George Chen


People also ask

Can you ssh into a running Docker container?

The SSH method works fine for Docker containers, too. That said, you can SSH into a Docker container using Docker's built-in docker exec . If you do not need an interactive shell, you can also use the docker attach command to connect the host's stdin and stdout to the running container and execute remote commands.

Which Docker command is used to attach to a running?

Use docker attach to attach your terminal's standard input, output, and error (or any combination of the three) to a running container using the container's ID or name. This allows you to view its ongoing output or to control it interactively, as though the commands were running directly in your terminal.


2 Answers

Ok, found the answer to it. In case anyone else comes across the same problem, just do

docker network ls 

This command lists all the docker networks. docker-compose will create a new network when you run docker-compose up. In my case, the network is named as myapp_default.

Note: Your app’s network is given a name based on the “project name”, which is based on the name of the directory it lives in. You can override the project name with either the --project-name flag or the COMPOSE_PROJECT_NAME environment variable. Networking in Compose

So the correct way to link to those containers is

docker run --link my_app_mq_1:mq --link my_app_db_1:db -it --net myapp_default worker  
like image 79
George Chen Avatar answered Sep 25 '22 01:09

George Chen


my command to run links

docker run --network=YOUR_NETWORK --link SERVICE -t -i app command 
like image 42
Gabriel Borges Oliveira Avatar answered Sep 26 '22 01:09

Gabriel Borges Oliveira