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}
Related
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
I'm on a Debian VPS on OVH cloud provider, running Docker.
Trying to make an apt update on the instance, I noticed that the disk of 40GB was full. What is quite surprising for an instance hosting 2 Wordpress blogs.
I tried to run:
sudo du -h /var/lib/docker/containers
One of the containers weight 27GB !
27G /var/lib/docker/containers/1618df0(...)d6cc61e
However when I run:
docker container ls --size
The same container only weight 500MB
1618df0(...) 782c(...) "docker-entrypoint.s…" 10 months ago Up 10 months 80/tcp blog_wordpress_1 2B (virtual 545MB)
The Docker Compose is pretty simple:
wordpress:
build:
# call the Dockerfile in ./wordpress
context: ./wordpress
restart: always
environment:
# Connect WordPress to the database
WORDPRESS_DB_HOST: db:xxxx
WORDPRESS_DB_USER: xxxx
WORDPRESS_DB_PASSWORD: xxxx
WORDPRESS_DB_NAME: xxxx
volumes:
# save the content of WordPress an enable local modifications
- ./wordpress/data:/var/www/html
networks:
- traefik
- backend
depends_on:
- db
- redis
The Dockerfile:
FROM wordpress
# printf statement mocks answering the prompts from the pecl install
RUN printf "\n \n" | pecl install redis && docker-php-ext-enable redis
RUN /etc/init.d/apache2 restart
Do you know what to investigate to understand this problem ?
Thanks
Ok, this was actually the logs... The logs are not counted by:
docker container ls --size
So I just truncated the logs, brutally:
sudo sh -c "truncate -s 0 /var/lib/docker/containers/*/*-json.log"
This solve the problem for a while.
For the long term, I added these lines to the Wordpress container's Docker Compose, then deleted and recreated the containers:
logging:
options:
max-size: "10m"
max-file: "3"
I am experimenting with a wordpress docker install.
I used the basic install with mounted volumes.
docker-compose.yml
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- ./mysql:/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:
- "8001:80"
restart: always
volumes:
- ./html:/var/www/html
- ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
But after adding several plugins and a theme, wp-admin gets terribly slow. Approx 5-7 seconds TTFB. Using elementor becomes basically impossible.
Throwing hardware (it's an AWS EC2) at the server did NOT change the performance.
Is it possible to have wordpress in a performant docker setup?
First: You should probably use the mysql:latest tag. 5.7 is now an older database, latest is now 8.0.23 (community server).
Second: You should specify wordpress version, and php version and keep these updated along the way. I use image: wordpress:5.6-php7.4-apache which gives me php7 for better performance.
Once you make changes in your docker-compose.yml, run docker-compose up --build to make sure to get clean versions of everything.
Your docker-compose version could be upgraded from 3.3 to 3.8 (has nothing to do with performance, though).
Make sure to upgrade your Docker installation to the latest (19.03+ at the moment).
Compare your docker-compose to mine, which is running great with plugins:
version: "3.8"
services:
db:
image: mysql:latest
command: "--default-authentication-plugin=mysql_native_password"
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:5.6-php7.4-apache
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
WORDPRESS_CONFIG_EXTRA: |
define('WP_DEBUG', true);
error_reporting(E_ALL);
ini_set('display_errors', 1);
working_dir: /var/www/html
volumes:
- ./wp-content:/var/www/html/wp-content
- ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
volumes:
db_data: {}
Note that I use working_dir so that the directory for your docker container is set correctly. By adding wp-content to your volumes you copy wp-content into your container to persist it. Plugins are located in wp-content and this may improve your performance situation.
There is almost no cost of running docker. The biggest difference is on the networking layer, but it is reduced with host networking. The cost is not as much as you should think about it.
What is docker
In simplification docker is nothing more than process and resources isolation. All processes are running on the host machine without any virtualozation. There are linux modules responsible for isolation resources and processes. Example of modules:
Cgroup and memory resource controller in Kernel: https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt
Docker uses this to limit of CPU and memory usage for containers. More info here: https://docs.docker.com/config/containers/resource_constraints/
Linux namespaces - https://man7.org/linux/man-pages/man7/namespaces.7.html
This is another important feature of kernel used by Docker.
iptables - Iptables are commonly used to define networking layers in Docker. This is probably the biggest bottleneck for Docker.
IBM investigation in 2014
IBM did some investigation for that topic few years ago here: https://dominoweb.draco.res.ibm.com/reports/rc25482.pdf
You can find network latency for Docker NAT networking:
But let's see another graph, that show us latency for Redis. You can see that for network=host docker is almost as fast as native host.
Debugging
We cannot say what is wrong with your deployment, because picture is too big, and you provided only small part of the photo.
However you can start debugging by yourself.
Create another EC2 instance.
Install Prometheus on new Instance
On the WordPress instance install the node exporter. This will export metrics for Prometheus
Configure Prometheus to collect metrics from your Wordpress instance
Optionally install the Grafana on the Prometheus Server.
Wait a day to collect data and analyze where you are hitting the ceil.
To install Prometheus use Prometheus Get Start Docs
To install node_exporter and set the Prometheus Scraper up use this docs: https://prometheus.io/docs/guides/node-exporter/
Summary
So answer to your question is: It depends, how your application is deployed to docker. Probably few important things, that can affect your performance
CPU limit for container
Memory limit for container
Networking type
Missing capabilities
Number of application deployed on the same host
Other limits like max open files, virtual memory limit, number of processes inside container, etc...
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'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