docker-compose: send only some traffic through another container (vpn) - docker

I have an app that consists of a crawler and a MySQL db. I want to go through a VPN when I do my crawler, but then not use the VPN when I connect to my DB. I've managed to get my app container to send all traffic through my VPN container, but now I can't connect to my DB that is located at localhost. I've tried using host addresses localhost, 127.0.0.1, and the db's container name but none of them work.
How can I route some traffic through my VPN container, and some traffic just normally?
Here is my docker compose file:
version: '3.7'
services:
db:
image: mysql:8
restart: always
environment:
MYSQL_DATABASE: my_db
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: pw
MYSQL_PASSWORD: pw
MYSQL_PORT: 3308
ports:
- "3308:3306"
command: --default-authentication-plugin=mysql_native_password
vpn:
build:
context: ./
dockerfile: Dockerfile-openvpn-dev
restart: always
cap_add:
- NET_ADMIN
devices:
- /dev/net/tun
volumes:
- ./openvpn:/vpn
command: openvpn --config /vpn/config.ovpn --auth-user-pass /vpn/client.pwd --auth-nocache
app:
build:
context: ./
dockerfile: Dockerfile-crawler-dev
environment:
MYSQL_DATABASE: my_db
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: pw
MYSQL_PASSWORD: pw
MYSQL_HOST: db
MYSQL_PORT: 3306
network_mode: service:vpn

You have to put db and vpn on the same network. Local container traffic isn't routed through the vpn. If you don't want to route external traffic through the vpn, you would have to use iptables on the vpn service.
Also make sure to include redirect-gateway def1 in your ovpn config.
version: '3.7'
services:
db:
image: mysql:8
restart: always
networks:
- default
environment:
MYSQL_DATABASE: my_db
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: pw
MYSQL_PASSWORD: pw
MYSQL_PORT: 3308
ports:
- "3308:3306"
command: --default-authentication-plugin=mysql_native_password
vpn:
build:
context: ./
dockerfile: Dockerfile-openvpn-dev
restart: always
networks:
- default
#might be necessary:
#links:
# - db
cap_add:
- NET_ADMIN
devices:
- /dev/net/tun
volumes:
- ./openvpn:/vpn
command: openvpn --config /vpn/config.ovpn --auth-user-pass /vpn/client.pwd --auth-nocache
app:
build:
context: ./
dockerfile: Dockerfile-crawler-dev
environment:
MYSQL_DATABASE: my_db
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: pw
MYSQL_PASSWORD: pw
MYSQL_HOST: db
MYSQL_PORT: 3306
network_mode: service:vpn
depends_on:
- vpn
networks:
default:

Related

Connection refused: Docker and Symfony

I'm trying to connect my Symfony project with a database in a Docker environment but unfortunately, the message is always: Connection refused.
My .env configuration (Symfony file):
DB_USER=root
DB_PASSWORD=root
DB_HOST=127.0.0.1
DB_PORT=3306
DB_NAME=db
DATABASE_URL=mysql://${DB_USER}:${DB_PASSWORD}#${DB_HOST}:${DB_PORT}/${DB_NAME}
and my Docker-compose configuration:
version: '3.8'
services:
db:
image: mariadb
container_name: db-docker-env
restart: always
volumes:
- db-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: "root"
MYSQL_DATABASE: "db"
MYSQL_USER: "root"
MYSQL_PASSWORD: "root"
networks:
- dev
command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']
ports:
- "3306:3306"
phpmyadmin:
image: phpmyadmin
container_name: phpmyadmin-docker-env
restart: always
depends_on:
- db
ports:
- 8080:80
environment:
PMA_HOST: db
networks:
- dev
I searched for a long time on the Internet, but unfortunately to no avail. Is there a solution to that?

mysql in docker compose

hi i'm new in docker and for practice created this project but when it's run with docker compose shows me below error:
wordpress | MySQL Connection Error: (1045) Access denied for user 'user1'#'172.19.0.4' (using password: NO)
docker-compose.yml:
version: '3.9'
services:
db:
image: mysql:latest
container_name: mysql
restart: always
command: "--default-authentication-plugin=mysql_native_password"
environment:
MYSQL_ROOT_PASSWORD: mjs
MYSQL_DATABASE: wpdb
MYSQL_USER: user1
MYSQL_PASSWORD: mjs
wordpress:
image: wordpress
container_name: wordpress
restart: always
volumes:
- ./wp-content:/var/www/html/wp-content
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_NAME: wpdb
WORDPRESS_DB_USER: user1
WORDPRESS_DB_PASSWROD: mjs
ports:
- 8080:80
- 443:443
depends_on:
- db
phpmyadmin:
image: phpmyadmin:latest
restart: always
ports:
- 3333:80
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: mjs
I searched for stackoverflow but found nothing.
Thank you for your help
If you are not defining your db volume data is not persisting on restart. Just btw.
Furthermore it's a good practice to pin your image to a version like mysql:5.7.
Did you try that one here?
https://docs.docker.com/compose/wordpress/

Can't access docker container over public ip

So I am creating an application that is running in a docker container on port 3000 of my local machine. I can open the app at localhost:3000, I can also open it on another device but on the same network by using 192.168.68.100:3000. However, I can't access the app by visiting my_public_ip:3000. I added port forwarding on my router for port 3000. I am running the app using docker-compose up and this is my docker-compose file:
version: '3.7'
services:
main:
container_name: main
build:
context: .
target: development
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- 3000:3000
command: npm run start:dev
env_file:
- .env
networks:
- webnet
depends_on:
- db
db:
container_name: db
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_DATABASE: "..."
MYSQL_USER: "..."
MYSQL_PASSWORD: "..."
MYSQL_ROOT_PASSWORD: "..."
adminer:
container_name: adminer
image: adminer
restart: always
ports:
- 8080:8080
networks:
webnet:
Any suggestions what am I missing?

Connect to networked mysql docker container with local client

I have just followed a tutorial on using Docker. I started with installing different containers and when I got to mysql, i installed it by running
docker container run -d -p 3306:3306 --name mysql --env MYSQL_ROOT_PASSWORD=123456 mysql
After the container has been created, I have been able to connect to MySQL with Heidi, by using 127.0.0.1, root and 123456 as password.
I eventually moved forward with another tutorial on installing WordPress with docker.
https://www.youtube.com/watch?v=pYhLEV-sRpY
The yaml for setting up the containers is bellow. Everything works just fine, but when it comes to connecting to the database using Heidi, I just do not get what to do - what connection data to use or what to change in the yaml to be able to connect from local machine.
version: '3'
services:
# Database
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- wpsite
# phpmyadmin
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '8080:80'
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: password
networks:
- wpsite
# Wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- '8000:80'
restart: always
volumes: ['./:/var/www/html']
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
networks:
- wpsite
networks:
wpsite:
volumes:
db_data:
You should expose 3306 port to local machine. Then, you can connect to MySQL by using host=127.0.0.1.
Update the yaml file like this:
version: '3'
services:
# Database
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- wpsite
...

How Can Run Phpmyadmin, Mysql and Apache Tomcat with Docker-Compose File?

I am trying to run java web app. Thus, I must setup MySQL, phpmyadmin and Apache Tomcat for accessing website. I want to use the docker and its docker-compose application to make installation easier (If I may learn).
I could not set mysql and phpmyadmin applications to work together. If I can get past this first step, I will try to install tomcat.
This is my codes which is used(docker-compose.yml)
version: '2'
services:
db:
image: mysql
restart: always
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: myDb
MYSQL_USER: admin
MYSQL_PASSWORD: 12345
MYSQL_ROOT_PASSWORD: root
phpmyadmin:
image: phpmyadmin/phpmyadmin
restart: always
ports:
- "8080:80"
environment:
MYSQL_USER: admin
MYSQL_PASSWORD: 12345
MYSQL_ROOT_PASSWORD: root
depends_on:
- db
links:
- db
I open the above codes with docker-compose up in the command line application.
I'm going to http://localhost:8080/ page, I'm typing admin for Username, 12345 for password.
The errors I get are:
Cannot log in to MySQL server
mysqli_real_connect (): The server requested authentication method
mysqli_real_connect (): (HY000 / 2054): The server requested authentication method
How can I install the applications (mysql, phpmyadmin, tomcat) with docker-compose?
Currently phpmyadmin and MySQL can work together. It is solved.
PLAN A:
This is my docker-compose.yml file:
version: '2'
services:
db:
container_name: db
image: mysql
restart: always
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: myDb
MYSQL_USER: admin
MYSQL_PASSWORD: 12345
MYSQL_ROOT_PASSWORD: root
command: --default-authentication-plugin=mysql_native_password
phpmyadmin:
depends_on:
- db
container_name: phpmyadmin
image: phpmyadmin/phpmyadmin
restart: always
ports:
- "8080:80"
environment:
MYSQL_USER: admin
MYSQL_PASSWORD: 12345
MYSQL_ROOT_PASSWORD: root
PMA_HOST: db
Now, you can go into MySQL via phpmyadmin at http://localhost:8080
It can also be solved by this method.
PLAN B: This is my docker-compose.yml file:
version: '2'
services:
db:
container_name: db
image: mysql
restart: always
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: myDb
MYSQL_USER: admin
MYSQL_PASSWORD: 12345
MYSQL_ROOT_PASSWORD: root
phpmyadmin:
depends_on:
- db
container_name: phpmyadmin
image: phpmyadmin/phpmyadmin
restart: always
ports:
- "8080:80"
environment:
MYSQL_USER: admin
MYSQL_PASSWORD: 12345
MYSQL_ROOT_PASSWORD: root
PMA_HOST: db
Command line's commands:
$ docker stop phpmyadmin
$ docker exec -it db bash
$ mysql -u root -proot
$ ALTER USER root IDENTIFIED WITH mysql_native_password BY 'root';
$ exit
$ exit
$ docker start phpmyadmin
Now, you can go into MySQL via phpmyadmin at http://localhost:8080
Reference: phpMyAdmin on MySQL 8.0
version: '3.1'
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- 3307:3306
environment:
MYSQL_ROOT_PASSWORD: 1234
phpmyadmin:
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 8080:80
environment:
MYSQL_ROOT_PASSWORD: 1234
links:
- db

Resources