简体   繁体   中英

How to add a docker container to an existing docker network

I have the following challenge with docker: I have a postgres service up and running which is managed by a docker-compose and works just fine.

Now, I want to build additional but isolated docker services that connect to this postgres database (an do an analytical task for instance) and can read and write data from there.

I understand that I could just grow my docker-compose file, however I want to keep this slim and tidy. So, my idea is to put each additional service in its own docker image and connect this to the postgres database. This allows me to use the additional docker services as I need them.

The challenge I have with this, is: how do I connect from docker container X to the postgres database that's running in the other docker network?

Thanks for any help!

Placing containers in the same network is trivial:

docker run --name my_new_container --network my_docker_network_name my_image_name

Figuring out the right network name:

Now, docker compose creates a network based on the app folder it was run in. Run docker network ls to list all networks.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM