I have an issue with a docker image.
I have just followed this tutorial to get custom local domains: https://medium.com/#francoisromain/set-a-local-web-development-environment-with-custom-urls-and-https-3fbe91d2eaf0
This seem to work good. But now I can not seem to connect to the Mysql database.
I'm using the WordPress image and my docker-compose.yml file looks like this:
version: '3.3'
services:
db:
image: mysql:5.7
restart: unless-stopped
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: p4ssw0rd!
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
expose:
- 80
environment:
VIRTUAL_HOST: phpmyadmin.local
VIRTUAL_PORT: 80
PMA_HOST: db
MYSQL_ROOT_PASSWORD: p4ssw0rd!
wordpress:
depends_on:
- db
image: wordpress:latest
restart: unless-stopped
working_dir: /var/www/html
volumes:
- ./wp-content:/var/www/html/wp-content
restart: always
expose:
- 80
environment:
VIRTUAL_HOST: domain.local
VIRTUAL_PORT: 80
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_NAME: wp_name
WORDPRESS_DB_USER: wp_user
WORDPRESS_DB_PASSWORD: p4ssw0rd!
WORDPRESS_TABLE_PREFIX: wp_
WORDPRESS_DEBUG: 1
volumes:
db_data: {}
networks:
default:
external:
name: nginx-proxy
When I start running the container, the error I'm getting is this:
So now I know my WordPress container and proxy are working (domain.local), but the only thing missing is the connection to the database.
I think this has to do with the 'network' and that he simply can't find the database. But I have no idea how to connect it.
Hope one of you guys know what I'm doing wrong.
Thanks in advance.
Okay, so I finally figured it out.
The WORDPRESS_DB_USER should be set to: root
Then everything will work.
Related
I’m trying to set up a local dev environment for WordPress with MySQL.
After starting the container, the WordPress element works fine and is accessible.
But MySQL throws connection refused despite being able to connect using the same database details in MySQL Workbench.
I have tried changing the host from dB:3306 to 0.0.0.0:3306 but to no avail.
Using Docker for Mac OS.
Dockerfile:
---
version: '3.3'
services:
db:
image: mysql:5.7.30
volumes:
- ./database:/var/lib/mysql
- ./mysqldumps/production-backup.sql:/docker-entrypoint-initdb.d/production.sql
- ./init/migrate.sh:/docker-entrypoint-initdb.d/migrate.sh
restart: unless-stopped
networks:
- wp
ports:
- '3306:3306'
environment:
MYSQL_ROOT_PASSWORD: $db_root_password
MYSQL_DATABASE: $db_name
MYSQL_USER: $db_user
MYSQL_PASSWORD: $db_password
WORDPRESS_TABLE_PREFIX: $db_table_prefix
PRODUCTION_URL: $production_url
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- ./wordpress/wp-content:/var/www/html/wp-content
- ./init/prep.sh:/usr/local/bin/prep.sh
ports:
- '80:80'
restart: unless-stopped
networks:
- wp
environment:
WORDPRESS_DB_HOST: $db_host
WORDPRESS_DB_USER: "root"
WORDPRESS_DB_PASSWORD: "password"
WORDPRESS_TABLE_PREFIX: $db_table_prefix
WORDPRESS_DEBUG: $wp_debug_mode
DISABLED_PLUGINS: $wp_plugins_to_disable
networks:
wp:
volumes:
database:
Does anybody have any ideas what’s going on here?
I have a similar issue to Run MySQL on Port 3307 Using Docker Compose but either I can't see the wood for the trees or the solution here is not working.
I have the following docker-compose.yml file:
version: '3'
services:
db:
image: mysql:5.7
container_name: squirrels_db
volumes:
- db_data:/var/lib/docker/volumes/squirrels_db_data/_data
restart: always
ports:
# <Port exposed> : <MySQL Port running inside container>
- 3310:3306
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: mydb_user
MYSQL_PASSWORD: password
volumes:
- ./var/lib/docker/volumes/squirrels_db_data/_data
networks:
internal-net:
ipv4_address: 172.29.0.11
wordpress:
image: wordpress:latest
container_name: squirrels_web
depends_on:
- db
ports:
- 8000:80
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: mydb_user
WORDPRESS_DB_NAME: mydb_name
WORDPRESS_DB_PASSWORD: password
volumes:
- ./data/wp_content:/var/www/html/wp-content
- ./config/wordpress/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
networks:
nginx-proxy:
internal-net:
ipv4_address: 172.29.0.12
# Names our volume
volumes:
db:
networks:
nginx-proxy:
external:
name: nginx-proxy
internal-net:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.29.0.0/16
Note: I have changed usernames and passwords here and will eventually have them in a .env file
When I hit http://localhost:8000 I am seeing a WordPress delivered "Error establishing a database connection" message and the following in the log (from docker-compose logs):
PHP Warning: mysqli::__construct(): (HY000/2002): Connection refused
in Standard input code on line 22 MySQL Connection Error: (2002)
Connection refused
This now officially driving me nuts, not helped at all by just knowing I am missing something obvious! So, any observations or suggestions gratefully received
Thanks
I managed to get it working. Here's the working file – I'll explain my changes below:
version: "3"
services:
db:
image: mysql:5.7
container_name: squirrels_db
volumes:
# You prepended with db_data: but volume was called db – scroll down to volumes to see my fix
- db_data:/var/lib/docker/volumes/squirrels_db_data/_data
restart: always
ports:
# <Port exposed> : <MySQL Port running inside container>
- 3310:3306
environment:
MYSQL_ROOT_PASSWORD: password
# This is likely the isse - you called it wordpress but tried to connect to mydb_name in the wordpress container
MYSQL_DATABASE: mydb_name
MYSQL_USER: mydb_user
MYSQL_PASSWORD: password
# REMOVE BELOW 2 LINES - you declared above
# volumes:
# - ./var/lib/docker/volumes/squirrels_db_data/_data
networks:
internal-net:
ipv4_address: 172.29.0.11
wordpress:
image: wordpress:latest
container_name: squirrels_web
depends_on:
- db
ports:
- 8000:80
restart: always
environment:
WORDPRESS_DB_HOST: db # db:3306 is fine but that's default so removed
WORDPRESS_DB_USER: mydb_user
WORDPRESS_DB_NAME: mydb_name
WORDPRESS_DB_PASSWORD: password
volumes:
- ./data/wp_content:/var/www/html/wp-content
- ./config/wordpress/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
networks:
nginx-proxy:
internal-net:
ipv4_address: 172.29.0.12
# Names our volume
volumes:
# FIX: renamed from db to db_data. Added {} to declare an empty volume
db_data: {}
networks:
nginx-proxy:
external:
name: nginx-proxy
internal-net:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.29.0.0/16
Changes
I think your indentation was fine - but I changed this as a first test
You had 2 volumes keys declared in your db container. the first was seemingly correct, the second was missing the db_data: volume prepending the path.
Your volume was named db but was being used (as in point 2) as db_data
Your database names didn't match. You called the db wordpress on setup at MYSQL_DATABASE: wordpress then tried to connect to mydb_name at WORDPRESS_DB_NAME: mydb_name
Simpler solution
Although the above works, do you definitely need the networking? The below will also work and is much simpler:
version: "3"
services:
db:
image: mysql:5.7
container_name: squirrels_db
volumes:
- db_data:/var/lib/docker/volumes/squirrels_db_data/_data
restart: always
ports:
- 3310:3306
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: mydb_name
MYSQL_USER: mydb_user
MYSQL_PASSWORD: password
wordpress:
image: wordpress:latest
container_name: squirrels_web
depends_on:
- db
ports:
- 8000:80
restart: always
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: mydb_user
WORDPRESS_DB_NAME: mydb_name
WORDPRESS_DB_PASSWORD: password
volumes:
- ./data/wp_content:/var/www/html/wp-content
- ./config/wordpress/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
volumes:
db_data: {}
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
I'm learning to use docker to make my development easier but I'm still failing access mysql.
Here is my docker-compose.yaml:
version: '3.3'
services:
# Database
db:
image: mysql:latest
ports:
- '3306:3306'
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: dev1
MYSQL_USER: root
MYSQL_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: root
WORDPRESS_DB_PASSWORD: password
WORDPRESS_DB_NAME: dev1
networks:
- wpsite
networks:
wpsite:
volumes:
db_data:
Wordpress is running without difficulties which means that mysql must be alright too. I'm on Linux and trying to connect database via mysql workbench. It appears that connection is also ok expect for, there is no schema and so no wordpress tables.
I tried to add also phpmyadmin into docker-compose.yaml:
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '8080:80'
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: password
networks:
- wpsite
but here I get following error after attempt to access db:
What I miss?
EDIT:
here is overview of running containers:
So i modified your docker-compose , with 2 small changes , and i dont have a issue .
I created a user for wordpress ( userdev1 ) in mysql .
The root is already here and can have some restrictions for remote access .
Via phpmyadmin i can login with userdev1 or root
You want a network access with the root account you must set this variable
MYSQL_ROOT_HOST .
You can find more information on this page (
https://dev.mysql.com/doc/mysql-installation-excerpt/5.7/en/docker-mysql-more-topics.html#docker_var_mysql-root-host )
version: '3.3'
services:
# Database
db:
image: mysql:5.7
ports:
- '3306:3306'
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: password4root
MYSQL_DATABASE: dev1
MYSQL_USER: userdev1
MYSQL_PASSWORD: password4dev1
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: userdev1
WORDPRESS_DB_PASSWORD: password4dev1
WORDPRESS_DB_NAME: dev1
networks:
- wpsite
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '8080:80'
environment:
PMA_HOST: db
networks:
- wpsite
networks:
wpsite:
volumes:
db_data:
** UPDATED **
With the very last version of mysql docker image ( aka mysql 8.0 ),
you must change the default-authentification to mysql_native_password to be comptatible with legacy mysql client
source :
https://dev.mysql.com/doc/mysql-installation-excerpt/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
I'm new to docker all together - but am trying to setup a local test environment to play with some wordpress things.
So I went to the docker site and pulled up a default docker .yml file on how to get it going easily.
I've made just a couple changes, but mostly this is a straight forward document.
version: '3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql2
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: somerootwordpresspw
MYSQL_DATABASE: testdatabase
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
volumes:
- ./WP-TEST/:/var/www/html/
depends_on:
- db
image: wordpress:latest
ports:
- "80:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
db_data:
When I run docker-compose up with the above .yml file, I see this error:
MySQL "CREATE DATABASE" Error: Access denied for user 'wordpress'#'%' to database 'wordpress'
Which I find odd, because I'm naming the database testdatabase, so why is it trying to create a database named wordpress?
When I connected with SQL Pro, I could see testdatabase, but according to the console it's trying to create wordpress db.
How do I get it to connect to my named DB, instead of constantly failing to create wordpress?
So I think I got it.
It was really simple. In my wordpress portion of my .yml file I needed to include WP_DB_NAME: testdatabase
By doing that, it used my named testdatabase to install wordpress to.
Hope this helps people who might stumble across this.
Now the .yml file looks like this:
version: '3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql2
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: somerootwordpresspw
MYSQL_DATABASE: testdatabase
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
volumes:
- ./WP-TEST/:/var/www/html/
depends_on:
- db
image: wordpress:latest
ports:
- "80:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_NAME: testdatabase
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
db_data: