first, sorry for my bad english, hope you can understand me.
i have the following task.
I want to run a (maybe more) wordpress installation on my synology nas. Therefore, i installed Docker and run portainer for creating some stuff.
My main idea is to create the following:
Create different container with separated wordpress installations
Create mysql container for hosting the different wordpress databases, each for every wordpress app
for the wordpress container there is a own network called "app_network" (bridge, attachable)
for the mysql container there is another network called "db_backend" (bridge, attachable)
So far, so god. At the moment i created one WP container, the mysql container and the two networks. Everything seems to be fine.
wordpress container is created with docker-compose (stack in portainer)
mysql container is created with docker-compose (stack in portainer)
I created a db for wordpress in the mysql container manually - local login on container works perfect.
the mysql container is in the network db_backend
the woordpress container is in the network app_network and additionally connected to the db_backend network (assigend ip´s looks correct)
But...if i am calling the wordpress page i got "Error establishing a database connection"
My yaml looks like this:
#mysql.yaml
version: '3.9'
services:
db:
image: mysql:latest
restart: on-failure:3
volumes:
- /volume1/docker/databases:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: mysuperstrongpassword
container_name: db_mysql
networks:
- db_backend
networks:
db_backend:
driver: bridge
external: true
#worppess.yaml
version: '3.9'
services:
#frontend
wp_app:
image: wordpress:latest
restart: on-failure:3
ports:
- '49200:80'
- '49201:443'
volumes:
- /volume1/docker/wp_app/wp_t:/var/www/html
environment:
WORDPRESS_DB_HOST: db_mysql:3306 //wrong entry? tried hostname, ip, service
WORDPRESS_DB_NAME: mydb
WORDPRESS_DB_USER: myuser
WORDPRESS_DB_PASSWORD: mypassword
networks:
- db_backend
- app_network
networks:
#172.168.29.1/24
db_backend:
driver: bridge
external: true
#172.168.30.1/24
app_network:
driver: bridge
external: true
After all i was able to read about docker, docker-networking and docker compose i thought my solution should work, all can be deployed without any errors, except the database connection error :( ...
Is the way of connection the container between the networks correct?
May i edit the wp-config.php with the information and add them to the wordpress containe?
Can anyone help?
Replace this WORDPRESS_DB_HOST: db_mysql:3306 with WORDPRESS_DB_HOST: db
Related
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 created a Wordpress website with the docker-compose.yml below.
I started it with docker-compose up -d (Note that the MySQL and Wordpress data are persisted in subdirectories of the local directory: ./mysql and ./wp).
Then let's say we reboot the host server.
How would you restart these containers/this website?
Would you just redo docker-compose up -d?
I tried, and it works indeed, but this actually recreates
new containers! Fortunately, as it reuses the same data directories and as it detects that /var/www/html is non-empty, a new Wordpress installation is not triggered, so "everything is ok", but still, isn't recreating new containers bad practice since the website already exists?
Is there a more "graceful" way to restart containers made with docker-compose after host server reboot, rather than "recreating" the containers?
version: '2'
services:
db:
image: mysql:5.7
volumes: ['./mysql:/var/lib/mysql']
restart: always
environment:
MYSQL_ROOT_PASSWORD: abcdef
MYSQL_DATABASE: wordpress
MYSQL_USER: wp
MYSQL_PASSWORD: abcabc
wordpress:
depends_on: ['db']
image: wordpress:latest
volumes: ['./wp:/var/www/html']
ports: ['8000:80']
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wp
WORDPRESS_DB_PASSWORD: abcabc
There are 2 ways to start containers which are created by docker-compose.
After rebooting the machine:
Run: docker ps -a to show all stopped-container, and you can start container manually, docker start [container_name or container_id]
Run: docker-compose start [service].
Starts existing containers for a service.
In your case:
docker-compose start db
docker-compose start wordpress
or just
docker-compose start
Starting db ... done
Starting wordpress ... done
Before rebooting, docker-compose stop.
I wanted to create a local dev environment for a wordpress site using docker.
So I have the following docker-compose file :
version: '3'
services:
db:
image: mariadb
restart: on-failure
environment:
- MYSQL_DATABASE=${WP_DB_NAME}
- MYSQL_USER=${WP_DB_USER}
- MYSQL_PASSWORD=${WP_DB_USER_PASSWORD}
volumes:
#- ./db_data:/var/lib/mysql
- ./mysql_dump/backup.sql:/docker-entrypoint-initdb.d/backup.sql
- ./mysql_dump/migrate.sql:/docker-entrypoint-initdb.d/migrate.sql
networks:
- local
wordpress:
image: wordpress
depends_on:
- db
restart: always
ports:
- 8080:80
environment:
- WORDPRESS_DB_HOST=db
- WORDPRESS_DB_USER=${WP_DB_USER}
- WORDPRESS_DB_PASSWORD=${WP_DB_USER_PASSWORD}
- WORDPRESS_DB_NAME=${WP_DB_NAME}
volumes:
- ./htdocs:/var/www/html
networks:
local:
ipv4_address: 172.23.0.4
networks:
local:
driver: bridge
ipam:
config:
- subnet: 172.23.0.0/24
I use volume to start mariadb with existing database dump.
Website is loading fine, but some URLs are wrong :
When I try to connect, I am redirected to :
http://172.23.0.4/172.23.0.4/wp-admin/ for example
Some js scripts are also not loaded due to wrong URLs like
http://172.23.0.4172.23.0.4/wp-includes/js/tinymce/tinymce.min.js?ver=4920-20181217
I checked the general settings in the admin panel, and the values siteurl and home in the database and they are correct : 172.23.0.4.
The website in production (no docker but classic LAMP install) is working fine by the way…
Do you have any hints ? (I'm not that familiar with web development, especially with wordpress)
Looks like specifying 172.23.0.4 in wordpress settings for home and siteurl is somehow (don't know why though) causing the issue.
Instead, setting the value to http://172.23.0.4 fixed the problem.
If someone has some explanation, please tell me.
I am trying to connect to a database that has an IP of x.x.x.x from my Docker container
Getting this error
java.net.NoRouteToHostException: No route to host (Host unreachable)
Tried running container using --network=host which has a similar approach to the above attempt
As I mentioned in the comments, here is the sample docker-compose file.
version: '3.7'
services:
entitygraph:
image: entitygraph-by-jar:latest
container_name: entitygraph
restart: always
networks:
- eg-net
ports:
- 9999:8080
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://eg-mysql/customers?useSSL=false
SPRING_PROFILES_ACTIVE: mysql
eg-mysql:
image: mysql:5.7
restart: always
networks:
- eg-net
container_name: eg-mysql
environment:
MYSQL_DATABASE: customers
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
MYSQL_ROOT_PASSWORD:
networks:
eg-net:
name: eg-net
In this file, the application entitygraph is trying to talk to mysql. In my application, the connection string to mysql is as below,
spring.datasource.url=jdbc:mysql://localhost:3306/customers?useSSL=false
So, docker will replace the spring.datasource.url property with the one I specified on my docker-compose file. Note that host:port is eg-mysql, which docker resolves to it's internal IP and will use it to communicate.
I don't know about your application architecture. If I know, I could give you more specific answer to your problem.
I have all set in my local machine for virtual machine shared folders. I have following code in my Docker compose file for Wordpress service. but not sure how the volumes work here. Can you please explain?
version: '2'
services:
database:
image: mysql:5.6
volumes:
- ./mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: password
restart: unless-stopped
wordpress:
image: wordpress:4.9.6
ports:
- 49160:80
links:
- database:mysql
volumes:
- ./wordpress:/var/www/html/wp-content
environment:
WORDPRESS_DB_PASSWORD: password
restart: unless-stopped
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- database:db
ports:
- 8080:80
Does the above volumes line of code mean, does it need to create a WordPress folder in my docker-compsose.yml file that I am currently running?
Or is it anyhow related to my shared folders in virtual machine?
Basically volumes are instruments for Docker so it can retain data. Docker containers are generally designed to be stateless, but if you need to retain state/information between runs, that's where volumes come in.
You can create an unnamed volume in the following way:
volumes:
- /var/www/html/wp-content
This will retain your wp-content folder in the internal volumes storage without a particular name.
A second way would be to give it a name, making it a named volume:
volumes:
- mywp:/var/www/html/wp-content
And the final type, which is also what you are doing, is called a Volume Bind. This basically binds/mounts the content of a folder on your host machine in the container. So if you change a file in either place, it will be saved on the other.
volumes:
- ./wordpress:/var/www/html/wp-content
In order to use your Volume Bind, you will need to create the folder "wordpress" in the folder where you're running the docker-compose.yaml (usually your root folder). Afterwards, when your installation changes within the container, it will also change on the bind and vice-versa.
EDIT: In your particular case the following should work:
version: '3.2'
services:
database:
image: mysql:5.6
volumes:
- ./mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: password
restart: unless-stopped
wordpress:
image: wordpress:4.9.6
ports:
- 49160:80
links:
- database:mysql
volumes:
- type: bind
source: ./wordpress
target: /var/www/html/wp-content
environment:
WORDPRESS_DB_PASSWORD: password
restart: unless-stopped
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- database:db
ports:
- 8080:80
Adding a volume to your docker-compose.yml file will enable you to 'mount' content from your local file system into the running container.
So, about the following line here:
volumes:
- ./wordpress:/var/www/html/wp-content
This means that whatever's in your local wordpress directory will be placed in the /var/www/html/wp-content directory inside your container. This is useful because it allows you to develop themes and plugins locally and automatically inject them into the running container.
To avoid confusion, I'd recommend renaming wordpress to something else, so it's clear that you're mounting only your WordPress content, and not core files themselves.
I have a similar setup here, in case you need another reference:
https://github.com/alexmacarthur/wp-skateboard