If I use this inside docker-compose, it works fine:
db_node_domain:
image: mysql:5.7
volumes:
- ./db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: mystrongpassword
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
container_name: wp_db
networks:
- "proxy-tier"
wordpress:
depends_on:
- db_node_domain
image: wordpress:latest
ports:
- "8080:80"
expose:
- "8080"
restart: always
environment:
VIRTUAL_HOST: blog.mydomain.com
LETSENCRYPT_HOST: blog.mydomain.com
LETSENCRYPT_EMAIL: foo#mydomain.com
WORDPRESS_DB_HOST: db_node_domain:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
container_name: wordpress
networks:
- "proxy-tier"
I want to use different username and stronger passwords for MYSQL_USER and WORDPRESS_DB_USER but if I make any changes at all in them I get 502 Bad Gateway from nginx. Can I not change these?
it is because you have already built the database.
When you have run the docker-compose up for the fist time, these values have been set:
environment:
MYSQL_ROOT_PASSWORD: mystrongpassword
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
and this part:
volumes:
- ./db_data:/var/lib/mysql
means it is a persistence data and after stopping the container, for the next run, the data will be available and new ones will not be set. So you have two options:
new build
remove the data (or maybe image)
modify docker-compose.yml file with new password
running docker-compose again
manually change the password, etc
run the database container
login to the container e.g docker exec -it <CONTAINER-ID> /bin/bash
change the password, etc
logout and commit this new change
and it is obvious that new build is simpler and better option to do.
Related
I'm new to docker and databases but my first project on local host doesn't load as expected. The side appears but the content is scattered. Here is the .yml file
version: "3.9"
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- ./www:/var/www/html
ports:
- "80:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
Every time you run
$ docker-compose up
Docker create a new network to which all the containers in your compose file will be connected to.
In the first run of this compose file, you do the installation of Wordpress,
and Wordpress insert the IP address of your web site in the database.
The problem occur when you stop this compose file with:
$ docker-compose down
the network created with "docker-compose up" is removed
and when you run the compose file again, docker will create a new network with different IP addresses, so links become broken because your webpages still point to the old IP address.
A solution can be to create a network outside compose file with:
$ docker network create my_network
and empty the volume "dbdata" and the folder "./www" (and lose all your data) then update your compose file with:
version: "3.9"
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- ./www:/var/www/html
ports:
- "80:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
networks:
default:
external:
name: my_network
I think there is solution to this problem without losing your data, for more information you can read this article.
I made the wp site on my local mac using localhost. Then I git commited the entire thing and transferred to my hosting using http://ip:port instead of localhost. Then I edited wp-config define('DOMAIN_CURRENT_SITE', 'localhost'); and mySQL tables wp_blogs, wp_site, wp_sitemeta.
Now all the assets still reference localhost instead of http://ip:port so I see the text but no css/js/images.
I'm guessing it's some apache setting problem?
This is my docker compose:
version: '3.3'
services:
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- ./files:/var/www/html
# basic http auth
- ./apache/000-default.conf:/etc/apache2/sites-enabled/000-default.conf
- ./apache/.htpasswd:/etc/apache2/.htpasswd
ports:
- "80:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: username
WORDPRESS_DB_PASSWORD: password
db:
image: mysql:5.7
volumes:
- ./db:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: username
MYSQL_USER: username
MYSQL_PASSWORD: password
I tried finding what is referencing localhost in apache config but
cat `grep -rl localhost /etc/apache2` | grep localhost
finds nothing useful.
I'm trying to create a local WordPress environment using Docker and existing WordPress db dump. PHPMyAdmin is loading correctly and the db is in that but the localhost:8000 where I am trying to load the site says it can't be reached. Is there something I am missing in this docker-compose.yml file?
version: '3'
services:
db:
image: mysql:8
volumes:
- ./data:/docker-entrypoint-initdb.d
restart: always
command: "--default-authentication-plugin=mysql_native_password"
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wpdb
MYSQL_USER: user
MYSQL_PASSWORD: password
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_NAME: wpdb
WORDPRESS_DB_USER: user
WORDPRESS_DB_PASSWORD: password
WORDPRESS_DB_PREFIX: unti54
volumes:
- ./wp-content:/var/www/html/wp-content
phpmyadmin:
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 3333:80
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORT: password
If you are sure that both the db and the wordpress volume are correct and the logs aren't showing any errors about them, check the definition of the wordpress ports. You use quotation marks, but in the db service you don't. Try to execute the script without those quotation marks.
Quickstart: Compose and WordPress proposes the following docker-compose.yml
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- dbdata:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
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
volumes:
dbdata:
For persisting database data, a volume is created:
The docker volume db_data persists any updates made by Wordpress to the database.
but nothing is mentioned about the wordpress container...
Questions:
should I follow the same approach and create volumes for the wordpress container, in order to persist the data that are going to be added (by posts, uploads, themes)?
If yes, which paths / directories should I point to?
Maybe I've found something...
volumes:
- wp-content:/var/www/html/wp-content
According to this article:
...wp-content contains all user-supplied content. Basically anything you can upload to your site ends up here. That doesn’t include anything you write, mind you. Those things are stored in the WordPress database.
However, as long as you have both the database and your wp-content folder, you can always get your site back, even if everything else was lost.
This is also applied here: Setting up WordPress with Docker
To try it out:
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- dbdata:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- wp-content:/var/www/html/wp-content
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
dbdata:
wp-content:
I am trying to host multiple sites on a single Digital Ocean VPS. I am using docker to achieve this. Each Wordpress site has its own database.
Using the docker-compose file of any of the two sites works fine and the site goes live. Adding a second does not work. (Navigating to the domain gives an nginx error of 'service temporarily unavailable')
I tried launching a static website from a container based on an apache image, and it does indeed work. So the nginx reverse proxy does successfully route traffic.
I am guessing that there is something more that I need to change between the two docker-compose files. Every tutorial or sample I've found stops after creating the first, and never actually shows a second Wordpress site being created.
Wordpress site 1:
docker-compose.yml
version: "3"
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
container_name: site1_db
wordpress:
depends_on:
- db
image: wordpress:latest
expose:
- 80
restart: always
environment:
VIRTUAL_HOST: www.site1.com
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
container_name: site1_wp
volumes:
db_data:
networks:
default:
external:
name: nginx-proxy
Wordpress site 2:
docker-compose.yml
version: "3"
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
container_name: site2_db
wordpress:
depends_on:
- db
image: wordpress:latest
expose:
- 80
restart: always
environment:
VIRTUAL_HOST: www.site2.com
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
container_name: site2_wp
volumes:
db_data:
networks:
default:
external:
name: nginx-proxy
A network named nginx-proxy was created beforehand to link all the containers. I left out the details about the nginx instance as it uses the well known jwilder image and like I said, it does seem to work just fine.
I finally found the answer. It is two-fold:
The WORDPRESS_DB_HOST environmental variable should not be the service, but rather the container that it is referencing
Thus change:
WORDPRESS_DB_HOST: db:3306
to:
WORDPRESS_DB_HOST: site1_db:3306
The same obviously goes for site2
The fix above still does not result in a working setup. When changing the database container to build from mariadb instead of mysql, it starts working! I have no idea in the slightest why this is true. Especially since the mysql instances can work on their own, but when running simultaneously, only mariadb works