Problems when migrating a Wordpress-Site into Docker - wordpress

Solved: I found the solution to my problem here:
https://serverfault.com/a/955471
This is the docker entry in my wp-config.php:
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false) {
$_SERVER['HTTPS'] = 'on';}
but it seems, in my case it was not recognized, that my docker installation is behind a reverse proxy. I needed to write the "$_SERVER['HTTPS'] = 'on';" entry without the if query.
Edit: The Problem is not the migration, but it seems a wrong apache2 configuration
I am trying to migrate my wordpress site into docker. I have successfully inserted my database with the command:
docker exec -i wordpress_db_1 mysql -u exampleuser -pexamplepass exampledb < /home/dockerNutzer/datenbanken/wpdatabase.sql
and i have copied my old wp-content into the new Docker volume:
rsync -avc /home/dockerNutzer/wordpressSeite/wp-content/ /var/lib/docker/volumes/wordpress_wordpress/_data/wp-content/
The site is up and running, but it doesn't look like expected:
I can browse through the site, but when I try to log in, I get a redirect error from firefox with the message, that an error occurred during the connection with my address. A connection to a page like
/my-wordpress.site/?m=202210
is working, but not
/my-wordpress.site/wp-login.php
I am using the standard docker-compose file for installation:
version: '3.1'
services:
wordpress:
image: wordpress
restart: always
ports:
- 8050:80
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
volumes:
- wordpress:/var/www/html
db:
image: mariadb:10.5
restart: always
ports:
- 3307:3306
environment:
MYSQL_DATABASE: exampledb
MYSQL_USER: exampleuser
MYSQL_PASSWORD: examplepass
MYSQL_RANDOM_ROOT_PASSWORD: '1'
volumes:
- db:/var/lib/mysql
volumes:
wordpress:
db:
I tried to clear the rewrite rules in the apache config file and the .htaccess file.
I tried to insert my whole old site into the docker volume and not only the wp-content folder.
Edit:
I tried with this site
https://ithemes.com/blog/wordpress-login-redirect-loop/
to fix the redirect loop. But it doesn't worked.
I also checked the database with db check, like it is described here:
https://ithemes.com/blog/error-establishing-database-connection/
It showed no error:
docker exec -i wordpressaltepw_wpcli_1 wp db check
wpdatabase.wp_commentmeta OK
wpdatabase.wp_comments OK
...
Success: Database checked.

Related

Cannot complete wordpress setup aftern installing it with Docker Desktop + WSL2

I am trying to install wordpress using Docker Desktop and WSL2.
I was able to compose an image creating the following docker-compose.yml file and then running docker-compose up
version: '3.1'
services:
wordpress:
image: wordpress
restart: always
ports:
- 8080:80
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
volumes:
- wordpress:/var/www/html
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: exampledb
MYSQL_USER: exampleuser
MYSQL_PASSWORD: examplepass
MYSQL_RANDOM_ROOT_PASSWORD: '1'
volumes:
- db:/var/lib/mysql
volumes:
wordpress:
db:
(This is taken from the official wordpress image's page on docker hub. I have change the default environment variables for DB_USER, DB_PASSWORD and DB_NAME but they were set equal to MYSQL_USER, MYSQL_PASSWORD and MYSQL_DATABASE respectively.)
Back to Docker Desktop, I ran the container, which seems to work correctly because when I got to localhost:8080 I find Wordpress' first setup process. However, when I plug-in the database name, username, password and host that I used in the file I get the error "Error establishing a database connection".
I tried the following for as Database host, but none worked: localhost, localhost:8080, localhost:80 and db.
Any idea?

wp-cli in Docker fetches wrong database login credentials from wp-config

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

Is restarting a website with "docker-compose up" after a host server reboot a standard practice?

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.

Docker - WordPress - Localhost ERR_EMPTY_RESPONSE

I started working with Docker for WordPress. I followed the docker documentation to get it up and running:
https://docs.docker.com/compose/wordpress/
I added volumes for the plugin & theme directory.
When ran the command docker-compose up -d the first time and went to http://localhost:8000/ i saw the installation of WordPress. When i rebooted my PC and started the services again with: docker-compose up -d or docker-compose start i got the error message: ERR_EMPTY_RESPONSE.
I tried:
Removing the containers, services and volumes
Killing the netstat port
Currently I have no idea why it isn't working anymore. I am working on macOS
This is my current docker-compose.yml
version: '3.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
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- ./plugins/my-plugin:/var/www/html/wp-content/plugins/my-plugin
- ./themes/my-theme:/var/www/html/wp-content/themes/my-theme
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpres
volumes:
db_data: {}
The status of containers after running it:
Follow these steps
docker-compose down - twice
Edit the yml file and replace all instances of db_data with db_datax
Run docker-compose up -d
Alternately,
docker-compose down - twice {removes the network as well}
docker system prune --volumes
docker-compose up -d

How to share volumes between wordpress and wp-cli in docker-compose

In trying to setup a reusable, semi-automated docker-compose.yml file for WordPress theme development, I found a very helpful post on StackOverflow and implemented the docker-compose.yml that vstm provided.
The example works very well, and I had been using it to add wp-cli commands for adding plugins, changing options, and deleting pre-loaded themes and plugins.
But now, I'm getting nothing but permission errors and I have tried manually changing wp-cli to user: '33:33' as well as the original user:xfs
I started over from the example and the first problem I run into is adding an additional volume shared between the two for the theme I'm developing. My theme name is default so that volume name is correct.
Here's what I have so far:
version: "2.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
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- 8000:80
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_TABLE_PREFIX: "wp_"
WORDPRESS_DEBUG: 1
# vstm: add shared volume
volumes:
- wp_data:/var/www/html
- ./default/:/var/www/html/wp-content/themes/default
wordpress-cli:
depends_on:
- db
- wordpress
image: wordpress:cli
# vstm: This is required to run wordpress-cli with the same
# user-id as wordpress. This way there are no permission problems
# when running the cli
user: xfs
# vstm: The sleep 10 is required so that the command is run after
# mysql is initialized. Depending on your machine this might take
# longer or it can go faster.
command: >
/bin/bash -c '
sleep 10;
wp core install --path="/var/www/html" --url="http://localhost:8000" --title="Local Wordpress By Docker" --admin_user=admin --admin_password=secret --admin_email=foo#bar.com;
wp plugin install classic-editor;
'
# vstm: add shared volume
volumes:
- wp_data:/var/www/html
- ./default/:/var/www/html/wp-content/themes/default
volumes:
db_data:
# vstm: add shared volume
wp_data:
Here's the output log from the wp-cli console in kitematic:
Warning: Unable to create directory wp-content/uploads/2019/01. Is its parent directory writable by the server?
Success: WordPress installed successfully.
Installing Classic Editor (1.3)
Warning: Failed to create directory '/etc/X11/fs/.wp-cli/cache/': mkdir(): Permission denied.
Downloading installation package from https://downloads.wordpress.org/plugin/classic-editor.1.3.zip...
Unpacking the package...
Warning: Could not create directory.
Error: No plugins installed.
Everything was working perfectly on at least 5 sites and then I assume there was a change in docker recently and I updated and broke all of them. Any ideas how this can be remedied in the latest Docker?

Resources