Here is my docker-compose.yml file:
version: '3.9'
services:
db:
image: mysql:5.7
restart: always
command: [
'--disable-partition-engine-check',
'--default_authentication_plugin=mysql_native_password',
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_unicode_ci',
'--max_allowed_packet=100M'
]
volumes:
- ./db_data:/var/lib/mysql
environment:
MYSQL_DATABASE: wordpress
MYSQL_ROOT_PASSWORD: pswd4!
pma:
image: phpmyadmin/phpmyadmin
environment:
PMA_HOST: db
PMA_PORT: 3306
MYSQL_ROOT_PASSWORD: pswd4!
UPLOAD_LIMIT: 1G
ports:
- 8080:80
depends_on:
- db
wp:
image: wordpress:php8.0
ports:
- 80:80
volumes:
- ./config/php.conf.ini:/usr/local/etc/php/conf.d/conf.ini
- ./:/var/www/html
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: pswd4!
WORDPRESS_DEBUG: 1
depends_on:
- db
links:
- db:mysql
hostname: test.localhost
wpcli:
image: wordpress:cli-php8.0
volumes_from:
- wp
depends_on:
- db
- wp
links:
- db:mysql
entrypoint: wp
command: "--info"
volumes:
db_data:
When I try to use wp-cli in Docker (e.g. docker-compose run --rm wpcli plugin list), it gets an error that it cannot connect to the database:
Error: `Access denied for user 'username_here'#'192.168.32.5' (using password: YES)`
Error establishing a database connection
This either means that the username and password information in your `wp-config.php` file is incorrect or we can’t contact the database server at `mysql`. This could mean your host’s database server is down.
Are you sure you have the correct username and password?
Are you sure you have typed the correct hostname?
Are you sure the database server is running?
If you’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums. `Access denied for user 'username_here'#'192.168.32.5' (using password: YES)
`
It looks like wp-cli is seeing bad database credentials (username_here instead of root)
Result of executing docker-compose run --rm wpcli config list command:
What could be wrong? I've searched all over the internet, lost several hours and still haven't found the cause of the problem.
You should specify the same set of environment variables as in your wp container for your wpcli container. If not, default variables from a php file in wordpress are used.
Do be careful : volumes_from and link options are deprecated in compose v3. For the link option, docker-compose creates a network automatically (or you can specify one if you prefer) and the embed docker dns automatically attributes aliases to your containers based on their names (or in compose the service name). More info on that here
For volumes, you can find more info here
I've a question how exactly docker-compose handles environment variables.
services:
wp:
image: wordpress:latest
container_name: "wp"
restart: unless-stopped
links:
- wpdb
environment:
- TZ=Europe/Berlin
- WORDPRESS_DB_HOST=wpdb:3306
- WORDPRESS_DB_USER=wordpress
- WORDPRESS_DB_PASSWORD=password
- WORDPRESS_DB_NAME=wp
volumes:
- ./data:/var/www/html
labels:
- "traefik.enable=true"
- "traefik.backend=wp"
- "traefik.frontend.rule=Host:MASKED"
- "traefik.port=80"
- "traefik.docker.network=web"
networks:
- internal
- web
wpdb:
image: mariadb:latest
restart: unless-stopped
container_name: "wpdb"
environment:
- MYSQL_ROOT_PASSWORD=1234
- MYSQL_USER=wordpress
- MYSQL_PASSWORD=password
- MYSQL_DATABASE=wp
networks:
- internal
labels:
- "traefik.enable=false"
volumes:
- ./sql:/var/lib/mysql
volumes:
data:
sql:
networks:
web:
external: true
internal:
The compose file works great. The containers will be created and work perfectly.
But when I change the defaults at: WORDPRESS_DB_PASSWORD=password and MYSQL_PASSWORD=password.
The Wordpress container throws access denied for user. I also tried to kill the container and volumes.
Hopefully someone has a hint for me.
You should be doing a docker-compose down -v which would delete the named volumes declared in the volumes section. The only downside is that you would be losing all the data created by the service for the first time.
Here is how I could reproduce it -
Used your compose file as reference and on first time used the default password mentioned by you. The services come up fine, I install it and do a Ctrl+C to bring down the service. So all the MYSQL data is written into sql named volume.
When you do a Ctrl+C OR docker-compose down it only removes the containers and networks defined in the service. Not the volumes. Read more about it here
Now when you change password and bring the service back up it still uses the old volumes which has your old password.
So use a docker-compose down -v to remove the volumes too and give it a try.
Here are the steps how I reproduced it
Ctrl+C to stop all the services and then update the docker-compose.yml to update the password and do a docker-compose up again to get access denied error.
Do a docker-compose down -v to clean all the volume too and then do a docker-compose up
On doing a docker-compose down -v you will be losing all the data created by the prior service. Use it cautiously.
I'm new to docker and I'm just trying to create a simple Symfony API. I ran docker-compose up -d which created these containers:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
071de3320346 willdurand/elk "/usr/bin/supervisor…" About a minute ago Up About a minute 0.0.0.0:81->80/tcp simple-api_elk_1
c24132f645be simple-api_nginx "nginx" About a minute ago Up About a minute 0.0.0.0:80->80/tcp, 443/tcp simple-api_nginx_1
37128a03b667 simple-api_php "php-fpm7 -F" About a minute ago Up About a minute 0.0.0.0:9000->9000/tcp simple-api_php_1
aa7738c59891 mysql "docker-entrypoint.s…" About a minute ago Up About a minute 3306/tcp, 33060/tcp, 0.0.0.0:3307->3307/tcp simple-api_db_1
I then took the id from the simple-api_php and ran
docker exec 37128a03b667 composer create-project symfony/website-skeleton symfony-api
It returns:
OCI runtime exec failed: open /tmp/runc-process126262263: permission denied: unknown
here is my docker-compose.travis.yml:
version: '2'
services:
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: symfony
MYSQL_USER: symfony
MYSQL_PASSWORD: symfony
php:
build: ./php-fpm
expose:
- "9000"
volumes:
- ./symfony:/var/www/symfony
- ./logs/symfony:/var/www/symfony/var/logs
links:
- db
nginx:
build: ./nginx
ports:
- "80:80"
links:
- php
volumes_from:
- php
volumes:
- ./logs/nginx/:/var/log/nginx
elk:
image: willdurand/elk
ports:
- "81:80"
volumes:
- ./elk/logstash:/etc/logstash
- ./elk/logstash/patterns:/opt/logstash/patterns
volumes_from:
- php
- nginx
Just don't have enough knowledge to troubleshoot this.
Thanks!!
You should edit your elk service.
elk:
image: willdurand/elk
ports:
- "81:80"
volumes:
- ./elk/logstash:/etc/logstash
- ./elk/logstash/patterns:/opt/logstash/patterns <-------- remove this line
volumes_from:
- php
- nginx <-------- remove this line
Note: comments are marked with arrow <------
Also, if you still have the same issue - try to remove images and containers and then rebuild them.
// This command will remove all containers and images in your docker-compose.travis.yml file
$: docker-compose down
// Or you can remove container and images manually
$: docker rm container_name
$: docker rmi image_name
I am doing something very simple puting drupal in a docker container
volumes:
- /c/Users/mark/drupalb/sites/all/modules:/var/www/html/sites/all/modules
this directive which should mount home directory of my modules into the containers module directory doesnt work this seems like thing that people do everyday but how to resolves this
Not clear what your problem is. In the meantime here's a working drupal example
Example
The folowing compose file
└── docker-compose.yml
Runs two containers, one for drupal the other for the db:
$ docker volume create --name drupal_sites
$ docker-compose up -d
$ docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------
dockercompose_db_1 docker-entrypoint.sh mysqld Up 3306/tcp
dockercompose_web_1 apache2-foreground Up 0.0.0.0:8080->80/tcp
Note how the volume used to store the drupal sites is created separately, defaults to local storage but can be something more exotic to fit your needs
docker-compose.yml
version: '2'
services:
db:
image: mysql
environment:
- MYSQL_ROOT_PASSWORD=letmein
- MYSQL_DATABASE=drupal
- MYSQL_USER=drupal
- MYSQL_PASSWORD=drupal
volumes:
- /var/lib/mysql
web:
image: drupal
depends_on:
- db
ports:
- "8080:80"
volumes:
- drupal_sites:/var/www/html/sites
- /var/www/private
volumes:
drupal_sites:
external: true
I've created a small docker-compose.yml which used to work like a charm to deploy small WordPress instances. It looks like this:
wordpress:
image: wordpress:latest
links:
- mysql
ports:
- "1234:80"
environment:
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_PASSWORD: "password"
WORDPRESS_DB_HOST: mariadb
MYSQL_PORT_3306_TCP: 3306
volumes:
- /srv/wordpress/:/var/www/html/
mysql:
image: mariadb:latest
mem_limit: 256m
container_name: mariadb
environment:
MYSQL_ROOT_PASSWORD: "password"
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: "password"
volumes:
- /srv/mariadb:/var/lib/mysql
But when I start it now (maybe since docker update to Docker version 1.9.1, build a34a1d5), it fails
wordpress_1 | Warning: mysqli::mysqli(): (HY000/2002): Connection refused in - on line 10
wordpress_1 |
wordpress_1 | MySQL Connection Error: (2002) Connection refused
When I cat /etc/hosts of the wordpress_1 there are entries for MySQL:
172.17.0.10 mysql 12a564fdbc56 mariadb
and I am able to ping the MariaDB server.
When I docker-compose up, WordPress gets installed and after several restarts the MariaDB container prints:
Version: '10.0.22-MariaDB-1~jessie' socket: '/var/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution
Which schould indicate it to be running, isn't it?
How do I get the WordPress to be able to connect to the MariaDB container?
To fix this issue the first thing to do is:
Add the following code to wordpress & database containers (in the docker-compose file):
restart: unless-stopped
This will make sure you Database is started and intialized before wordpress container trying to connect to it. Then restart docker engine
sudo restart docker
or (for ubuntu 15+)
sudo service docker restart
Here the full configuration that worked for me, to setup wordpress with MariaDB:
version: '2'
services:
wordpress:
image: wordpress:latest
links:
- database:mariadb
environment:
- WORDPRESS_DB_USER=wordpress
- WORDPRESS_DB_NAME=mydbname
- WORDPRESS_TABLE_PREFIX=ab_
- WORDPRESS_DB_PASSWORD=password
- WORDPRESS_DB_HOST=mariadb
- MYSQL_PORT_3306_TCP=3306
restart: unless-stopped
ports:
- "test.dev:80:80"
working_dir: /var/www/html
volumes:
- ./wordpress/:/var/www/html/
database:
image: mariadb:latest
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=mydbname
- MYSQL_USER=wordpress
- MYSQL_PASSWORD=password
restart: unless-stopped
ports:
- "3306:3306"
The reason for this behaviour probably was related to a recent kernel and docker update. I recognized several other connection issues in other docker-compose setups. Therefore I restarted the server (not just the docker service) and didn't have had any issues like this ever since.
I had almost same problem, but just restarting the Wordpress container saved me:
$ docker restart wordpress
I hope this help many people.
I too had troubles here. I was using docker-compose to set up multiple wordpress websites on a single (micro) Virtual Private Server, including phpmyadmin and jwilder/nginx-proxy as a controller.
$ docker logs XXXX will help indicate areas of concern. In my case, the MariaDB databases would keep restarting all the time.
It turns out that all that stuff just wouldn't fit on a micro 512M Single CPU service. I never received error messages that told me directly that size was an issue, but after adding things up, I realized that when all the databases were starting up, I was running out of memory. An upgrade to 1Gb, 1 CPU service worked just fine.
I was using your docker-compose.yml, had the same problem. Just restarting didn't fix. After nearly an hour of researching the logs, I found the problem was: wordpress service started connecting mysql service before it had fully started. Simply adding depends_on won't help.Docker Compose wait for container X before starting Y
the work around could be start the db server before Up. When it has fully started, run docker-compose up. Or just use external service.
This simply means you are trying to connect to the wrong host. In order to use this in localhost just use the name of your service as the database host example in your case, it would be mysql you can fix this by specifying the name of the localhost with a default variable like this MYSQL_ROOT_HOST: localhost
In my case, I'm using Mysql (not MariaDb) but I had the same problem.
After upgrading the MySQL version, it's works fine.
You can see my open source docker-compose configuration: https://github.com/rimiti/wordpress-dockerized-environment