Search code examples
dockerdocker-network

mix link docker containers with network


I have the following setup:

CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS              PORTS                NAMES
eab42051ca26        web-www:20180804             "node run.js"            8 minutes ago       Up 8 minutes        3000/tcp             web-www
63ec48e93a77        jwilder/nginx-proxy:latest   "/app/docker-entrypo…"   9 hours ago         Up 9 hours          0.0.0.0:80->80/tcp   nginx-proxy-server
463ffd55260b        fiorix/freegeoip             "/go/bin/freegeoip"      9 hours ago         Up 9 hours          8080/tcp             freegeoip
bdc702c370ec        euvat                        "/usr/local/bin/euva…"   9 hours ago         Up 9 hours          3000/tcp             euvat
40c07de732fa        redis:4.0.10                 "docker-entrypoint.s…"   9 hours ago         Up 9 hours          6379/tcp             redis-www
76831834f59d        mongo:4.0                    "docker-entrypoint.s…"   9 hours ago         Up 9 hours          27017/tcp            mongo-www

where my web-www node.js app connects to redis and mongo via the

NETWORK ID          NAME                DRIVER              SCOPE
74d8f38aca38        bridge              bridge              local
1c894a7fa176        host                host                local
ca02c5ccac55        network-www         bridge              local
7226d9cc5360        none                null                local

my run.sh file is like:

OLDAPP="$(docker ps --all --quiet --filter=name="$APP")"
if [ -n "$OLDAPP" ]; then
  docker stop $OLDAPP && docker rm $OLDAPP
fi
docker run --name web-www \
    --network network-www \
   --link euvat:euvat \
   --link freegeoip:freegeoip \
   --env VIRTUAL_HOST=araweelo.local \
   --env-file /env/web-www.env \
   web-www:20180804.182446

so, now i am starting a new development stack dev-www for example, so i will create the network-dev, launch redis-dev and mongo-dev but want to share the euvat and freegeoip containers with the web-www container.

is this the correct way to do this or is there an alternative method?

any advice is much appreciated.


Solution

  • Docker links are deprecated and maybe removed soon.

    It's better to create the networks, containers ahead of time and join the container to the network

    docker network create network-www
    
    docker run --name web-www \
       --env VIRTUAL_HOST=araweelo.local \
       --env-file /env/web-www.env \
       web-www:20180804.182446
    
    docker network connect network-www web-www
    docker network connect network-www euvat
    docker network connect network-www freegeoip
    

    This above commands will create a network-www user-defined bridge network and connect euvat, web-www and freegeoip containers to that network.

    Replace/add containers as required. Might be a better idea to write a compose file which brings up the containers in a single command