How Can I Connect to MySQL in Docker Without Exposing Ports?
Asked by
77cmRainfall
I have a simple web app running in Docker on a cloud server, consisting of an Nginx container and a MySQL container, both on the same 'webapp' network. While Nginx has ports 80 and 443 exposed, MySQL does not have any ports exposed. I'm looking for a way to connect to the MySQL database from my local machine without opening up any ports. Is it possible to access the webapp network from the outside?
Answers
Since you want to keep MySQL off the public internet, it's smart that you've set it up on the internal Docker network. To connect from your local machine without opening the MySQL port to everyone, you can bind MySQL to localhost on the server and create an SSH tunnel. It's a safe way to access your database without exposing it.
Reply to this answer
One option is to set your Docker network to 'host'. This way, it can directly use the network of the host system, which simplifies access. Just check the Docker documentation for more details on the host network driver.
Reply to this answer
If you have the option to SSH into your server, you can create a tunnel to access MySQL that way. Make sure you have SSH key authentication set up for security. Additionally, you might want to restrict the SSH access to your IP for added safety. Using a firewall to limit access to the MySQL port is also a good move.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions