Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communication between docker containers on different servers

Tags:

docker

vps

I have one server running a website along with nginx which are in their own docker containers. I have a completely separate server running postgres inside a container.

How would I go about communicating between the two servers so that the website on the first server can use the database on the second server?

This works fine locally because I can just use docker compose and start everything up on the same docker network. Alternatively I can also start the containers separately and specify an external network for them to use which again works well. However im not sure how to get this all working in production, on different servers (vps).

I am aware of things like Kubernetes which I think could help with this but I have no experience with it and would prefer to avoid that while keeping this all as simple / vanilla as possible. This is just for personal projects and my skills lie in development, not devops.

like image 906
darkpool Avatar asked May 13 '26 19:05

darkpool


1 Answers

You'd set up your containers in the exact same way as if the database was running on the remote host but not in Docker: configure them with the other host's name as the database host and the appropriate database name, login credentials, &c. You need to make sure the database container is configured to allow external connections (docker run -p 3306:3306 or -p 5432:5432 options as appropriate); it wouldn't necessarily be in a single-host Docker Compose setup.

As a more general question, Docker on its own doesn't give much specific help if a given client and server are on separate hosts, even if both are in Docker. You can configure things to use only physical host names, ignoring Docker space, and redeploy if you move workloads around; you can deploy a service registry like Consul that knows where services are running and can tell the clients; or set up an overlay network (also buying into Docker's Swarm container orchestrator). This also is related to how you deploy the workloads, and your choice here might be different if you're using Ansible, or can tolerate installing a full-scale cluster manager, or insist on only using docker run or docker-compose commands.

like image 195
David Maze Avatar answered May 15 '26 11:05

David Maze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!