I want to set up a development environment for WordPress which supports IDE debugging through Xdebug and is contained in a docker container.
I'm working from WSL2 / Ubuntu 18.04
I'm using a MySQL official image
I'm using a WordPress image built on top of the WordPress official image with Xdebug installed through PECL
I'm copying configuration files for php.ini and xdebug.ini from a local folder
After launching my container I'm attaching a VSCode instance to the container by using ms Remote Containers extensions
I'm installing the official PHP debug VSCode extension from the Xdebug team in the attached instance of VSCode
I'm automatically generating a launch.json file for PHP
I'm setting a breakpoint and launching the debugger by pressing F5
What happens is that the debugger starts, offering me to open a browser at the port I set for Xdebug (9003), but the page doesn't load, the breakpoint isn't reached and the debugger tab in VSCode shows several errors such as: Failed initializing connection 1: connection closed (on close)
I tried other slightly different methods such as having the dockerfile build Xdebug from source as well as other setups for the ini files from tutorials found on the internet, but the end result is always the same.
If I try to run xdebug_info() through the browser I get a normal output, if I try to run the file from console I get this error:
Xdebug: [Step Debug] Could not connect to debugging client. Tried: host.docker.internal:9003 (through xdebug.client_host/xdebug.client_port) :-(
which is in line with similar errors I get in the docker-compile output.
I tried forwarding the 9003 port with docker-compile, but the behavior didn't change.
These are my docker and config files:
docker-compose.yml
version: "3.6"
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
ports:
- "3306:3306"
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
build:
context: ./wordpress
dockerfile: Dockerfile
volumes:
- wordpress_data:/var/www/html
ports:
- "8080:80"
# - "9003:9003"
restart: always
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
wordpress_data: {}
wordpress/Dockerfile
FROM wordpress:latest
RUN apt-get update && \
apt-get -y install git
# RUN git config --global url."https://github".insteadOf git://github
RUN pecl install xdebug
COPY ../config/ /
RUN docker-php-ext-enable xdebug
wordpress/config/usr/local/etc/php/php.ini (edited from php.ini-development)
...
[Xdebug]
xdebug.remote_autostart = 1
xdebug.scream = 1
xdebug.remote_enable = 1
xdebug.show_local_vars = 1
xdebug.remote_autostart = 1
xdebug.remote_connect_back = 1
(Since it seems this isn't being tracked I also tried making a symbolic link from php.ini-development to php.ini from the container console, then adding the variables there.)
wordpress/config/usr/local/etc/php/conf.d/xdebug.ini
zend_extension=xdebug.so
[xdebug]
xdebug.mode=develop,debug,trace,profile,coverage
xdebug.start_with_request = yes
xdebug.discover_client_host = 0
xdebug.remote_connect_back = 1
xdebug.client_port = 9003
xdebug.client_host='host.docker.internal'
xdebug.idekey=VSCODE
Related
Everything is fine until I reboot my Ubuntu host.
After reboot, the WordPress page shows the fresh installation page.
There are volumes properly mounted on the host's local directory.
I only have set docker.service to restart the Docker service when reboot.
There must be some mistake I am not aware of.
At least, what shall I do if this thing happens again?
I see all the files mounted on my host shows the latest modification time,
so it looks like the data is persistent...
(edited)
I also tried the external voume as #bilal said in the comment, but it didn't make any difference.
So, now I am thinking this may be related to the process while booting up. like, instead of stop&start, it somehow down/up. But I may be wrong.
version: '3.8'
services:
db:
container_name: $DB_CONTAINER
image: mariadb:latest
restart: always
volumes:
- wordpress_db_data:/var/lib/mysql:rw
environment:
MYSQL_RANDOM_ROOT_PASSWORD: 1
MYSQL_DATABASE: $DB_NAME
MYSQL_USER: $DB_USER
MYSQL_PASSWORD: $DB_PASSWORD
wp:
container_name: $WP_CONTAINER
image: wordpress:latest
depends_on:
- db
- cp
restart: always
volumes:
- wordpress_wp_data:/var/www/html:rw
environment:
WORDPRESS_DB_HOST: $DB_CONTAINER
WORDPRESS_DB_NAME: $DB_NAME
WORDPRESS_DB_USER: $DB_USER
WORDPRESS_DB_PASSWORD: $DB_PASSWORD
WORDPRESS_TABLE_PREFIX: $WP_TABLE_PREFIX
VIRTUAL_HOST: $VIRTUAL_HOST
VIRTUAL_PORT: $VIRTUAL_PORT
LETSENCRYPT_HOST: $VIRTUAL_HOST
LETSENCRYPT_EMAIL: $LETSENCRYPT_EMAIL
#LETSENCRYPT_TEST: 'true'
cp:
build: composer
container_name: ${COMPOSER_CONTAINER}
volumes:
- wordpress_wp_data:/app/wp-content:rw
command: composer install
networks:
default:
external:
name: nginx_proxy
volumes:
wordpress_wp_data:
name: wordpress_wp_data
wordpress_db_data:
name: wordpress_db_data
Here's my volume list
> docker volume ls
DRIVER VOLUME NAME
local wordpress_db_data
local wordpress_wp_data
Here's my docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket containerd.service
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3
# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
OOMScoreAdjust=-500
[Install]
WantedBy=multi-user.target
you should use docker volumes for persistent storage what I understand is you mounted dir. See docker volumes for more information.
so your volume section should look like this.
volumes:
- ./wp_data:/var/www/html:rw
- wp_data:/wp_data{volume you want to persist}
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.
Intro:
I am trying to run a few WP-CLI commands for maintenance as a part of my release process on my production sites. I can execute the following commands against the docker-compose file below successfully.
docker-compose run wp-cli_collinmbarrett-com core update
docker-compose run wp-cli_collinmbarrett-com plugin update --all
docker-compose run wp-cli_collinmbarrett-com theme update --all
docker-compose run wp-cli_collinmbarrett-com db optimize
I have a plugin (WP-Sweep) installed on the site that adds its own WP-CLI command. When I try to run this command, it fails.
docker-compose run wp-cli_collinmbarrett-com sweep --all
/usr/local/bin/docker-entrypoint.sh: exec: line 15: sweep: not found
In a non-dockerized setup, I have verified that the WP-Sweep command for WP-CLI works successfully.
Question:
How can I run plugin-installed WP-CLI commands when running in a containerized environment with Docker Compose? Do I need to somehow make the WP-CLI container aware of the installed plugins other than having a shared volume?
My docker-compose.yml:
version: "3.7"
services:
wp_collinmbarrett-com:
image: wordpress:fpm-alpine
restart: always
networks:
- reverse-proxy
- collinmbarrett-com
depends_on:
- mariadb_collinmbarrett-com
volumes:
- collinmbarrett-com_files:/var/www/html
mariadb_collinmbarrett-com:
image: mariadb:latest
restart: always
networks:
- collinmbarrett-com
volumes:
- collinmbarrett-com_data:/var/lib/mysql
wp-cli_collinmbarrett-com:
image: wordpress:cli
networks:
- collinmbarrett-com
volumes:
- collinmbarrett-com_files:/var/www/html
networks:
reverse-proxy:
external:
name: wp-host_reverse-proxy
collinmbarrett-com:
volumes:
collinmbarrett-com_files:
collinmbarrett-com_data:
Full config on GitHub.
Not answering directly to your command needs (I didn't tried yet), but I wanted to share with you all the configurations I'm using in hope it helps you.
My docker-compose.yml has:
services:
...
# Mysql container
db:
...
# Wordpress container
wp:
...
wpcli:
image: wordpress:cli
user: "33:33"
volumes:
# necessary to write to the filesys
- ./php-config/phar.ini:/usr/local/etc/php/conf.d/phar.ini
- wp_app:/var/www/html
- /tmp/wp-temp:/tmp/wp-temp
environment:
HOME: /tmp/wp-temp
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: $WORDPRESS_DB_USER
WORDPRESS_DB_PASSWORD: $WORDPRESS_DB_PASSWORD
WORDPRESS_DB_NAME: $WORDPRESS_DB_NAME
depends_on:
- db
- wp
volumes:
wp_app: {}
...
Please note that as mentioned on Running as an arbitrary user section in https://hub.docker.com/_/wordpress:
When running WP-CLI via the cli variants of this image, it is
important to note that they're based on Alpine, and have a default
USER of Alpine's www-data, whose UID is 82 (compared to the
Debian-based WordPress variants whose default effective UID is 33), so
when running wordpress:cli against an existing Debian-based WordPress
install, something like --user 33:33 is likely going to be necessary
(possibly also something like -e HOME=/tmp depending on the wp command
invoked and whether it tries to use ~/.wp-cli)
You will need to define WP-CLI user as www-data with user id and group id = 33. This is why I defined user: "33:33". Also, the command might need to download temporary content, so I defined a HOME environment setting. Please also note that HOME mapped in your Host should also be assigned with user 33:33 ownership ids, otherwise WP CLI can't write to the filesys.
Also, PHP.ini in the WPCLI image has the setting phar.readonly as On, so you need to override it. I've add a specific ./php-config/phar.ini file that has that override:
phar.readonly = Off
To execute a plugin installation I do, on my docker-compose.yml folder, the following command:
docker-compose run --rm wpcli plugin install wp-mail-smtp --force --allow-root
Please note that --force --allow-root are optional.
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?
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