Connect to docker-compose network using docker run

I’m not sure if the --net option ever existed but it’s now --network.

From docker run --help:

      --network string   Connect a container to a network (default "default")

As @maxm notes you can find the network name, with the DIR prefix of the compose project directory, then simply run it as you were trying:

$ docker run --network=DIR_compose_network <image for the job>

I wanted to connect on run as my container is transient (running tests) so I can’t use a second docker network command in time before it quits.

e.g. for my docker composition in a “dev” folder with no network name specified so uses the docker-compose “default” name, therefore I get the name dev_default.

docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
2c660d9ed0ba        bridge              bridge              local
b81db348e773        dev_default         bridge              local
ecb0eb6e93a5        host                host                local

docker run -it --network dev_default myimage

This connects the new docker container to the existing docker-compose network.

Leave a Comment