Docker compose mysql reachable on port 3306 - mysql

How can i make the mysql database in this docker-compose reachable from "outside" - meaning that i can connect with a database administration tool for example on port 3306:
wordpress:
build: /Users/FabianL/wp-docker/
container_name: "kr-wp-container"
links:
- db:mysql
ports:
- 8080:80
- 3306:3306
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: biersaufen

You have opened port 8080 and 3306 from container "wordpress" and none from container "db"
Change your compose file to:
wordpress:
build: /Users/FabianL/wp-docker/
container_name: "kr-wp-container"
links:
- db:mysql
ports:
- 8080:80
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: biersaufen
ports:
- 3306:3306

Related

Ubuntu docker with multiple mysql container

I hope you could help me.
I'm trying to setup a docker with multiple MySQL container in ubuntu server.
docker-compose.yml
version: '3.1'
services:
mysql-db_1:
image: mysql:8.0.28-debian
command: --sql_mode="" --default-authentication-plugin=mysql_native_password
restart: always
ports:
- '3306:3306'
volumes:
- ./data_1/files:/var/lib/mysql-files
- ./data_1/data:/var/lib/mysql
mysql-db_2:
image: mysql
command: --sql_mode="" --default-authentication-plugin=mysql_native_password
restart: always
ports:
- '3307:3307'
volumes:
- ./data_2/files:/var/lib/mysql-files
- ./data_2/data:/var/lib/mysql
Everything is successfully built and I was able to connect to port 3306 however I can't connect to port 3307.
Your mysql-db_2 container port is invalid.
you can change it to 3307:3306
so it will be exposed at port 3307, but the container itself will be listening on port 3306
mysql-db_2:
image: mysql
command: --sql_mode="" --default-authentication-plugin=mysql_native_password
restart: always
ports:
- '3307:3306'
volumes:
- ./data_2/files:/var/lib/mysql-files
- ./data_2/data:/var/lib/mysql

How to pass containers ip address to another container?

I am correctly trying to pass a container ip address to another
Here is my docker-compose.yml file
app-phpmyadmin:
image: phpmyadmin/phpmyadmin
depends_on:
- app-mysql
ports:
- '80'
container_name: app-phpmyadmin
environment:
PMA_HOST: app-mysql
PMA_PORT: 3306
app-mysql:
image: mysql:8
container_name: app-mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_ROOT_HOST: app-phpmyadmin <<<< doesnt work see below why
ports:
- 3306
When passing app-phpmyadmin instead of passing the ip it actually writes "app-phpmyadmin" as a string. On selecting use, host from mysql.user the hostname is "app-phpmyadmin" instead of the ip.
Is there a way I can do this in the docker-compose file or would i have to come up with a bash script that does it?
Actually, you can have try to use static network address
version: '3.4'
services:
app-phpmyadmin:
image: phpmyadmin/phpmyadmin
depends_on:
- app-mysql
ports:
- '81'
container_name: app-phpmyadmin
environment:
PMA_HOST: app-mysql
PMA_PORT: 3306
networks:
app-net:
ipv4_address: 10.5.0.5
app-mysql:
container_name: app-mysql
image: mysql:8
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_ROOT_HOST: 10.5.0.5
ports:
- 3306
networks:
app-net:
ipv4_address: 10.5.0.6
networks:
app-net:
driver: bridge
ipam:
config:
- subnet: 10.5.0.0/16
try this
version: '3'
services:
app-phpmyadmin:
image: phpmyadmin/phpmyadmin
depends_on:
- app-mysql
ports:
- '8081:80'
container_name: app-phpmyadmin #name expose at list: docker ps
hostname: app-phpmyadmin #hostname in network docker
environment:
PMA_HOST: app-mysql
PMA_PORT: 3306
PMA_USER: "root"
PMA_PASSWORD: "root"
app-mysql:
image: mysql:5.7 #downgrade beacause mysql 8 generate error with phpmyadmin. error: caching_sha2_password
container_name: app-mysql #name expose at list: docker ps
hostname: app-mysql #hostname in network docker
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- 3306

flask can not connect mysql in docker compose

flask can not connect mysql in docker compose
in connect string, I already use db service name as host, still can't connect db
My docker-compose.yml:
services:
api:
build:
context: ./api
dockerfile: Dockerfile
volumes:
- './api:/usr/src/app'
ports:
- 5002:5000
environment:
- FLASK_CONFIG=development
- FLASK_ENV=development
- APP_SETTINGS=project.config.DevelopmentConfig
- DATABASE_URL=mysql+pymysql://root:xxxxx#mysql-db:3307/gaojiesi
- SECRET_KEY=ZQbn05PDeA7v11
depends_on:
- mysql-db
links:
- mysql-db
restart: unless-stopped
mysql-db:
image: mysql:8.0
container_name: mysql-db
command: --default-authentication-plugin=mysql_native_password
environment:
- MYSQL_ROOT_PASSWORD=frikyalong
ports:
- "3307:3306"
volumes:
- ./db/mysql/conf/my.cnf:/etc/my.cnf.d/
- ./db/mysql/logs:/logs
- ./db/mysql/data:/var/lib/mysql
you need to change :
DATABASE_URL=mysql+pymysql://root:xxxxx#mysql-db:3307/gaojiesi
to :
DATABASE_URL=mysql+pymysql://root:xxxxx#mysql-db:3306/gaojiesi
port 3307 in not reachable from inside the container

Docker can't connect to MySQL running docker-compose up

I am using this Docker config: https://github.com/romaricp/kit-starter-symfony-4-docker
I start the environment by using:
docker-compose build
followed by:
docker-compose up -d
Everything is running fine but there is a problem with MySQL service. I get an Symfony error:
"An exception occurred in driver: could not find driver"
as well as PMA error when I try to login to DB:
mysqli_real_connect(): php_network_getaddresses: getaddrinfo failed:
Name does not resolve
Any idea how could I make it work?
docker-compose.yml:
version: '3'
services:
apache:
build: .docker/apache
container_name: sf4_apache
ports:
- 80:80
volumes:
- .docker/config/vhosts:/etc/apache2/sites-enabled
- .:/home/wwwroot/sf4
depends_on:
- php
mysql:
image: mysql
container_name: sf4_mysql
volumes:
- .docker/data/db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sf4
MYSQL_USER: sf4
MYSQL_PASSWORD: sf4
php:
build: .docker/php
container_name: sf4_php
volumes:
- .:/home/wwwroot/sf4
environment:
- maildev_host=sf4_maildev
depends_on:
- maildev
- mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: sf4_phpmyadmin
ports:
- 8080:80
links:
- mysql
maildev:
image: djfarrelly/maildev
container_name: sf4_maildev
ports:
- 8001:80
Also I opened the mysql logs and I see this:
2019-04-07T12:00:30.943414Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.15' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
Maybe the port is wrong and that's why I can't connect?
For phpmyadmin service, i think you should set the PMA_HOST and PMA_PORT environment variables like this:
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: sf4_phpmyadmin
environment:
PMA_HOST: mysql
PMA_PORT: 3306
ports:
- 8080:80
links:
- mysql
Your mysql container should have the command instruction at the start to set the authentication plugin (there is an issue with the connectors with mysql 8), more details here
mysql:
image: mysql
container_name: sf4_mysql
command: "--default-authentication-plugin=mysql_native_password"
volumes:
- .docker/data/db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sf4
MYSQL_USER: sf4
MYSQL_PASSWORD: sf4
in your Symfony app, the connection string is located in .env file, and it should have the following format :
DATABASE_URL=mysql://mysql_user:mysql_user_password#mysql_host:mysql_port/db_name
mysql_user: your mysql user (ex: root)
mysql_user_password: the user's password (ex: root)
mysql_host: is should contain your mysql container service name
located in your docker-compose.yml file (mysql in this case)
mysql_port: mysql container internal port (3306, in this case)
db_name: the database you want to connect to.
the DATABASE_URL can look like this :
DATABASE_URL=mysql://root:root#mysql:3306/sf4
stop your containers after these changes and start them up again.
Hope this will help.
You have to expose the mysql port 3306:
mysql:
image: mysql
container_name: sf4_mysql
volumes:
- .docker/data/db:/var/lib/mysql
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sf4
MYSQL_USER: sf4
MYSQL_PASSWORD: sf4
networks:
- default
I think you're having trouble linking the containers. I suggest that you use a custom network instead of linking. That all containers can see each other

Docker Compose phpmyadmin not connecting to MySQL

I have a docker compose file which is basically trying to build a WAMP/LAMP style environment.
Basically, PHPMyAdmin can't seem to connect to MySQL. Looking through answers here, it appeared that it was an issue with legacy auth using MySQL Image 8 so I added:
command: --default-authentication-plugin=mysql_native_password
but that didn't work, so I dropped down to mysql image5.7 and the issue is still present. For some reason, I can't connect to MySQL and I get the error:
mysqli_real_connect(): php_network_getaddresses: getaddrinfo failed: Try again
and
mysqli_real_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Try again
Here is a copy of my Docker Compose which I don't think is doing anything weird.
Hoping someone can clarify that for me.
version: "3.1"
services:
site:
build: .
ports :
- "80:80"
volumes:
- ./www:/var/www/html/
links:
- database
networks:
- php-network
#######################################
# PHP MY ADMIN
#######################################
phpmyadmin:
build:
context: .
dockerfile: PHPMYADMIN.Dockerfile
restart: always
links:
- database:mysql
environment:
PMA_HOST: mysql
PMA_PORT: 3306
ports:
- "8080:80"
environment:
- MYSQL_USERNAME=admin
- MYSQL_PASSWORD=root
networks:
- php-network
#######################################
# MySQL server
#######################################
database:
image: mysql:5.7.25
ports:
- "3306:3306"
container_name: db-mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=test_db
- MYSQL_USER=admin
- MYSQL_PASSWORD=root
networks:
- php-network
networks:
php-network:
driver: bridge
I also saw mention of a "depends on" flag which I tried too but that also failed to allow me to connect. I have had the same error throughout.
It's about the container name.
database:
image: mysql:5.7.25
ports:
- "3306:3306"
- **container_name: db-mysql**
and in your phpcontainer:
phpmyadmin:
build:
context: .
dockerfile: PHPMYADMIN.Dockerfile
restart: always
links:
- database:mysql
environment:
**PMA_HOST: mysql**
PMA_PORT: 3306
you define host as mysql, which in docker network will be unrecognizable.
try switching PMA_HOST to db-mysql.
Sorry for bad formatting.
Also use docker ps to see docker container names and to figure out which hosts do you need to connect.
I've got this working now with the following:
version: "3.1"
services:
www:
build: .
ports:
- "8081:80"
volumes:
- ./www:/var/www/html/
links:
- db
networks:
- default
#######################################
# MySQL server
#######################################
db:
image: mysql:5.7.25
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: test_db
MYSQL_ROOT_PASSWORD: test
volumes:
- ./dump:/docker-entrypoint-initdb.d
- persistent:/var/lib/mysql
networks:
- default
#######################################
# PHP MY ADMIN
#######################################
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8080:80
environment:
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
persistent:
According to your phpmyadmin service configuration, you are trying to reach mysql container by mysql address:
environment:
PMA_HOST: mysql
PMA_PORT: 3306
But container with mysql server is accessible by database or db-mysql addresses.
So, you need to change phpmyadmin service configuration to:
environment:
PMA_HOST: database
PMA_PORT: 3306