mysql docker cannot connect from outside container - mysql

I've created a docker-compose file for PHP dev but I cannot connect to the DB with sequel pro neither from the APP.
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: digitalocean.com/php
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
ports:
- "80:80"
- "443:443"
networks:
- app-network
#MySQL Service
db:
image: mysql:latest
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
expose:
# Opens port 3306 on the container
- '3306'
environment:
MYSQL_DATABASE: laravel
MYSQL_USER: laraveluser
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./mysql/data:/var/lib/mysql
- ./mysql/my.cnf:/etc/my.cnf
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
everything works OK and no errors are displayed but when I'm trying to connect to the DB in any way it doesn't work docker-compose exec app PHP artisan migrate nor with sequel Pro, what's wrong with my config?
The only way to access the DB is with docker-compose exec db bash
here the full repo

straight from the GitHub repo I have the answer, though I haven't tried yet as I think I'm going to stick with 5.7 for now.
You might need to add --default-auth=mysql_native_password to your command
Mysql:8 uses caching_sha2_password by default https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password
#454 (comment)

After digging into your code, I found that you need to update .env.example file
Replace DB_HOST=db on line 10.
You must have missed it, but it is already mentioned in the post that you had referred, under the heading "Step 8 — Modifying Environment Settings and Running the Containers".

Related

Docker / phpmyadmin: Unable to connecto

I have the following docker-compose file.
version: "3.7"
services:
main:
container_name: buspack_main
build:
context: .
target: development
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- ${API_PORT}:${API_PORT}
command: bash -c "npm install --save && npm run start:dev"
env_file:
- .env
networks:
- webnet
depends_on:
- db
links:
- db
restart: always
db:
container_name: buspack_db
image: mysql:5.7.33
networks:
- webnet
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: "backendnest"
ports:
- 23306:3306
restart: always
volumes:
- ./db:/var/lib/mysql:rw
phpmyadmin:
container_name: buspack_phpmyadmin
image: phpmyadmin/phpmyadmin
depends_on:
- db
restart: always
ports:
- '8030:80'
environment:
PMA_HOST: buspack_db
MYSQL_ROOT_PASSWORD: 123456
networks:
webnet:
volumes:
db: {}
My problem Is that when I try to login with phpmyadmin I get the following error:
mysqli::real_connect(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution
And also
mysqli::real_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution
Is there something I'm missing?
networks:
- webnet
add webnet network to phpmyadmin service
There are more options to solve the connection.
Add phpmyadmin container to webnet network as Def Soudani suggests. The YML example provided overrides the default network. Since the phpMyAdmin is lacking the network config, it won't be on the webnet docker network by default.
Remove all networks related config from the YML. Since compose already creates a default network (docs) for the containers within one YML file.
Use the PMA_ARBITRARY=1 (docs )which allows you to enter a database server hostname on login form.
Example of my YML setup with custom bridge networking:
version: '3.8'
services:
mysql:
image: mysql:5.7
container_name: myapp-mysql-db
restart: unless-stopped
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
volumes:
- ./database/dbdata:/var/lib/mysql
networks:
- myapp-network
phpmyadmin:
image: phpmyadmin:latest
container_name: phpmyadmin
ports:
# 8080 is the host port and 80 is the docker port
- 8080:80
environment:
PMA_HOST: mysql
UPLOAD_LIMIT: 20M
MYSQL_USERNAME: ${DB_USERNAME}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
restart: unless-stopped
networks:
# define your network where all containers are connected to each other
- myapp-network
networks:
myapp-network:
driver: bridge

Docker: Can't login in my mariadb using phpmyadmin

I am trying to build up a docker-compose file to run all components I need for my app. The app, mysql (mariadb) and phpmyadmin is running but I can't login to my database.
Following docker-compose.yml:
version: '3.7'
networks:
laravel:
services:
php:
image: php:7.4-fpm
build: .conf/php/
restart: always
ports:
- '9000:9000'
working_dir: /var/www
volumes:
- ./src:/var/www
- .conf/php/conf.d/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini
- .conf/php/conf.d/custom.ini:/usr/local/etc/php/conf.d/custom.ini
networks:
- laravel
fpm:
image: php:7.4-fpm
restart: always
volumes:
- ./src:/var/www
networks:
- laravel
nginx:
image: nginx:latest
ports:
- 8000:80
volumes:
- ./src:/var/www
- ./var/log/nginx:/var/log/nginx
- .conf/nginx/default.conf:/etc/nginx/conf.d/default.conf
networks:
- laravel
mariadb:
image: mariadb:latest
ports:
- 3306:3306
volumes:
- mariadb-volume:/var/lib/mysql
restart: always
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "no"
MYSQL_DATABASE: app
MYSQL_USER: admin
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
networks:
- laravel
phpmyadmin:
image: phpmyadmin
container_name: pma
environment:
MYSQL_DATABASE: app
PMA_HOST: mariadb
depends_on:
- mariadb
ports:
- '8081:80'
networks:
- laravel
volumes:
mariadb-volume:
When I try to login, I get the following error:
mysqli::real_connect(): (HY000/1045): Access denied for user 'admin'#'172.31.0.6' (using password: YES)
I tried to find a solution.. something like set up the right env vars for mysql or set up a bridge between mysql and phpmyadmin. I did the network bridge and I can't really see the problem with my env vars.
Any clue?
System: Windows 10
Edit:
Found the problem. I just did docker-compose down -v ( -v = Remove named volumes declared in the volumes section of the Compose file and anonymous volumes )
and it worked :)
May be you can set for phpmyadmin by this command:
phpmyadmin:
image: phpmyadmin:latest
container_name: pma
restart: always
environment:
MYSQL_DATABASE: app
PMA_HOST: mariadb
depends_on:
- mariadb
ports:
- '8081:80'
networks:
- laravel

Cannot resolve hostname in docker desktop windows

I have setup docker-compose to run phpmyadmin and mysql. Here is my docker-compose.yml
version: "3.9"
networks:
local_web_network:
driver: bridge
volumes:
mysql_data:
driver: local
services:
mysql:
image: mysql:8.0
container_name: local_mysql
restart: always
tty: true
ports:
- "3306:3306"
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_DATABASE: local_master
MYSQL_ROOT_PASSWORD: secret
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
volumes:
- mysql_data:/var/lib/mysql
networks:
- local_web_network
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: local_phpmyadmin
restart: always
tty: true
ports:
- "8083:80"
environment:
PMA_HOST: local_mysql
PMA_ABSOLUTE_URI: http://db.test
MYSQL_ROOT_PASSWORD: secret
networks:
- local_web_network
extra_hosts:
- 'db.test:127.0.0.1'
I have configured the windows hosts file with the below settings
127.0.0.1 db.test
I have successfully run the docker containers without any errors. I have tried accessing the http://127.0.0.1:8083 or http://db.test:8083 it works and displays the phpmyadmin login page.
But the problem here is, i am unable to access the page without specifying the port i.e http://db.test. How do i get this working without specifying the port?

mysql database name and credentials inside docker container not updating

I have a LAMP stack with laravel setup in docker-compose containers,everything seems to be working fine so far. I initially had a test database with the name of homestead setup in docker-compose.yml that was just there for testing now i want to change it to something more meaningful like my project name. let me show you my docker-compose.yml
version: '3'
networks:
laravel:
services:
site:
build:
context: .
dockerfile: nginx.dockerfile
container_name: nginx
ports:
- 80:80
volumes:
- ./src:/var/www/html:delegated
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql:5.7.29
container_name: mysql
restart: unless-stopped
tty: true
ports:
- 3306:3306
environment:
MYSQL_DATABASE: alis
MYSQL_USER: alis
MYSQL_PASSWORD: pass
MYSQL_ROOT_PASSWORD: pass
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
volumes:
- ./mysql:/var/lib/mysql
php:
build:
context: .
dockerfile: php.dockerfile
container_name: php
volumes:
- ./src:/var/www/html:delegated
networks:
- laravel
composer:
build:
context: .
dockerfile: composer.dockerfile
container_name: composer
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
depends_on:
- php
user: laravel
entrypoint: ['composer', '--ignore-platform-reqs']
networks:
- laravel
npm:
image: node:latest
container_name: npm
volumes:
- ./src:/var/www/html
ports:
- 3000:3000
- 3001:3001
working_dir: /var/www/html
entrypoint: ['npm']
networks:
- laravel
artisan:
build:
context: .
dockerfile: php.dockerfile
container_name: artisan
volumes:
- ./src:/var/www/html:delegated
depends_on:
- mysql
working_dir: /var/www/html
user: laravel
entrypoint: ['php', '/var/www/html/artisan']
networks:
- laravel
phpmyadmin:
image: phpmyadmin/phpmyadmin
restart: always
container_name: phpmyadmin
depends_on:
- mysql
ports:
- "8081:80"
environment:
PMA_HOST: mysql
MYSQL_ROOT_PASSWORD: secret
networks:
- laravel
the problem is phpmyadmin is still using the old database and credentials , it tried the following solutions
docker-compose down
docker-compose down -v
then
docker-compose up -d
this didnt work.Out of frustration even thought this is probably not the best way to do it, I removed all containers, all images and then ran
docker-compose up -d
to re-install everything from scratch to my surprise phpmyadmin still picks up the old database config.Does anyone know the reason for this behaviour?
Your phpmyadmin service tries to connect as root:secret (MYSQL_ROOT_PASSWORD) while your mysql service defines root:pass (MYSQL_ROOT_PASSWORD) as admin user.
See the available configuration here:
https://hub.docker.com/_/phpmyadmin
https://hub.docker.com/_/mysql

Setup Docker containers for existing Wordpress site

I'm trying to setup a dev environment for an existing wordpress website hosted on cPanel.
I've exported the test data from the existing pre-production database to be imported into the mysql running in one of the containers.
version: '3.3'
services:
db:
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: P#ssw0rd
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
- /local/path/to/wordpress/src/:/var/www/html
volumes:
db_data: {}
everything starts up fine. Now I'm inserting the db dump into the mysql db in the container
cat dump.sql | docker exec -i docker_db_1 /usr/bin/mysql -u wordpress --password=wordpress wordpress
which finishes without error. When trying to access the website now on localhost:8000 the Apache Ubuntu default page pops up but I can't see anything from the existing wordpress site.
This setup worked for me
docker-compose.yml
version: '3.6'
services:
wordpress:
image: wordpress:${WORDPRESS_VERSION}
container_name: wordpress
volumes:
- ${WORDPRESS_DATA_DIR}:/var/www/html
environment:
- WORDPRESS_DB_NAME=${WORDPRESS_DB_NAME}
- WORDPRESS_DB_HOST=${WORDPRESS_DB_HOST}
- WORDPRESS_DB_USER=${WORDPRESS_DB_USER}
- WORDPRESS_DB_PASSWORD=${WORDPRESS_DB_PASSWORD}
depends_on:
- mysql
restart: always
mysql:
image: mysql:${MYSQLDB_VERSION}
container_name: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_DATABASE=${MYSQL_DATABASE}
ports:
- 3306:3306
volumes:
- ./mysql:/var/lib/mysql
- ./mysql_config:/tmp/mysql_config
nginx:
image: nginx:${NGINX_VERSION:-latest}
container_name: nginx
ports:
- '80:80'
- '443:443'
volumes:
- ${NGINX_CONF_DIR}:/etc/nginx/conf.d
- ${NGINX_LOG_DIR}:/var/log/nginx
- ${WORDPRESS_DATA_DIR}:/var/www/html
depends_on:
- wordpress
restart: always
All used variables are set as environment variables first.
To connect to any container with use the following command:
docker exec -i -t <wordpress|mysql|nginx> /bin/bash