WRITELOOP

LOCAL DOCKER CONTAINERS COMMUNICATION EVEN UNDER A VPN

2021 November 25

If the VPN you are using redirect all your network traffic through it, you may be in a situation where you cannot communicate with your local docker containers while you are connected to this VPN. To circumvent that, we can run the docker containers with network_mode: host. Then, if you have mapped container ports to your local host, you can still communicate with the local containers under the IP 127.0.0.1. Here is a docker-compose.yml example:

version: "3"
services:
database:
image: mysql:5.6
container_name: mysql-local
network_mode: host
volumes:
- "./shared:/shared"
expose:
- 3306
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=12345678
- MYSQL_DATABASE=sample_db

In this example, even under a VPN, you can still connect to this containerized MySQL through e.g. mysql default client:

$ mysql -u root -h 127.0.0.1 -p12345678 sample_db
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.5.40 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>