Search code examples
dockerdocker-composecontainers

How to link a container already running to my app using docker-compose


I need to link 2 containers named proxy1 and proxy2 to myapp.

proxy1 and proxy2 are using a docker command to run, and myapp is using a docker-compose.yml

I need to be able to ping proxy1 and proxy2 from the container myapp

I tried to use external_links in my docker-compose.yml like that:

services:
  myapp:
    build: .
    ports:
      - 3000:3000
    external_links:
      - proxy1
      - proxy2

if i make a docker ps i have this:

CONTAINER ID        IMAGE               COMMAND                  STATUS              PORTS                                            NAMES
2f0365826670        myapp         "docker-entrypoint.s…"   Up About an hour    0.0.0.0:3000->3000/tcp, 0.0.0.0:8001->8001/tcp   myapp_myapp_1
bbd1f5340086        proxy         "supervisord -n"         Up 2 hours          127.0.0.1:5002->8080/tcp                         proxy2
6c3cd1eb6530        proxy         "supervisord -n"         Up 2 hours          127.0.0.1:5001->8080/tcp                         proxy1

How can i access to these 2 proxy containers from myapp ?


Solution

  • I suggest you to do the following:

    create a network:

    docker network create mynetwork
    

    connect your standalone container to the network:

    docker network connect mynetwork my_container
    

    update your docker-compose:

    services:
      myapp:
        build: .
        ports:
          - 3000:3000
    networks:
      default:
        external:
          name: mynetwork
    

    Other way is to check what the standalone contaoner network is using:

    docker inspect proxy1
    

    and use that netwok as the external network in compose.

    or you can use the network_mode: "host" in your compose which is not recommended