Docker local environment custom local domains (hosts file?) - wordpress

I'm a bit new to docker, but am searching this for quite a long time now.
I am using docker almost always with this container: https://hub.docker.com/_/wordpress/ , because most of my project are WordPress based.
The point is that every time I'm running this container, I'm running it on a localhost domain. Now at the company I'm working, we still using Virtual Machine. Here I have a 'homestead.yml' file, were I can add a custom domain and it's path. I also need to add this to my hosts file, and run a vagrant provision.
I don't want to use Virtual Machine on this Mac, because I like the speed of using Docker, but I do want the custom domains. For example; I work on a project called 'sunglasses', I want to create a local domain called 'sunglasses.local' for my local environment. But i can't seem to get it working...
My docker-compose file looks like this:
version: '3.1'
services:
wordpress:
depends_on:
- db
image: wordpress:latest
restart: unless-stopped
working_dir: /var/www/html
volumes:
- ./wp-content:/var/www/html/wp-content
# - /Users/username/dev/wordpress-foundation-boilerplate/wp-content:/var/www/html/wp-content
# - /Users/username/dev/docker-wp-demo/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
environment:
WORDPRESS_DB_NAME: database
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: p4ssw0rd!
WORDPRESS_TABLE_PREFIX: wp_
WORDPRESS_DEBUG: 1
WORDPRESS_CONFIG_EXTRA: | # Add config to wp-config.php
define('FS_METHOD', 'direct');
define('WP_DEBUG_LOG', true);
define( 'WP_DEBUG', true );
ports:
- 8000:80
- 443:443
networks:
- back
db:
image: mysql:5.7
restart: unless-stopped
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: p4ssw0rd!
networks:
- back
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 8080:80
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: p4ssw0rd!
networks:
- back
networks:
back:
volumes:
db_data:
I'm using Gasmask (see: http://clockwise.ee/) for editing my hosts file, and already added the domain I want to use with the right IP-adres.
Any idea what I'm missing? I don't now were to place the domain address in my docker-compose file. I have tried added it under 'ports' but this didn't even run my docker-compose file. I hope someone knows what I'm doing wrong.

At work we use dnsmasq to pass requests to TLDs that end in .docker to localhost. Here is how:
Requirements: homebrew and administration access
To forward .docker TLDs, install & configure Dnsmasq.
$ brew up && brew install dnsmasq
$ sudo mkdir -p /etc/resolver
$ echo 'nameserver 127.0.0.1' | sudo tee -a /etc/resolver/docker > /dev/null
$ echo 'address=/docker/127.0.0.1' | tee -a /usr/local/etc/dnsmasq.d/docker-tld.conf > /dev/null
$ sudo brew services start dnsmasq
Note: The resolver will start working after a reboot
Modify /usr/local/etc/dnsmasq.conf
...
listen-address=127.0.0.1
...
conf-dir=/usr/local/etc/dnsmasq.d/,*.conf
Test the DNS server
$ dig test.docker #127.0.0.1
; <<>> DiG 9.9.7-P3 <<>> test.docker #127.0.0.1
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 40401
;; flags: qr aa rd ra ad; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;test.docker. IN A
;; ANSWER SECTION:
test.docker. 0 IN A 127.0.0.1
;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Thu Feb 08 16:24:12 CET 2018
;; MSG SIZE rcvd: 45
Test the configuration
; Make sure your DNS is still working.
$ ping -c 1 www.google.com
PING www.google.com (216.58.206.4): 56 data bytes
64 bytes from 216.58.206.4: icmp_seq=0 ttl=53 time=26.789 ms
--- www.google.com ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 26.789/26.789/26.789/0.000 ms
Check that .docker TLDs are working
$ ping -c 1 test.docker
PING test.docker (127.0.0.1): 56 data bytes
Source: Passing Curiosity

Related

Can't connect wordpress to mariadb (docker)

I have two containers, one for mariadb and the other for wordpress.
I'm using this configuration for mariadb:
version: "3"
services:
mariadb:
image: mariadb
container_name: mariadb
environment:
MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
volumes:
- ./data:/var/lib/mysql
ports:
- "127.0.0.1:3306:3306"
restart: unless-stopped
networks:
- web
networks:
web:
external: true
And this for wordpress
version: '3'
services:
# Wordpress
myWordpress:
image: wordpress:php8.1
container_name: myWordpress
hostname: myWordpress
restart: unless-stopped
volumes: [ './data:/var/www/html' ]
environment:
- WORDPRESS_DB_HOST=mariadb
- WORDPRESS_DB_USER="${WORDPRESS_DB_USER}"
- WORDPRESS_DB_PASSWORD="${WORDPRESS_DB_PASSWORD}"
- WORDPRESS_DB_NAME="${WORDPRESS_DB_NAME}"
networks:
- web
networks:
web:
external: true
I already created the network web and logged in mysql root , created the database & user and executed this command: GRANT ALL PRIVILEGES ON wp_db.* TO 'wp_user'#'%';.
Then I logged in mysql using that user and its password.
docker exec -it mariadb bash
mysql -u wp_user -pMyPassword
# Welcome to the MariaDB monitor. Commands end with ; or \g.
# ...
The issue appears when I visit wordpress, I see this message:
Warning: mysqli_real_connect(): (HY000/1045): Access denied for user '"wp_user"'#'172.16.32.5' (using password: YES) in /var/www/html/wp-includes/wp-db.php on line 1753
Access denied for user '"wp_user"'#'172.16.32.5' (using password: YES)
I feel that the problem is in WORDPRESS_DB_HOST variable, I pointed it to mariadb container name, I also tried pointing it to localhost since both containers share the same network web, but his didn't work either, and the same exact error message appeared.
One month later, I went back to this issue and tried putting environment variables in quotes, it worked as expected.
So instead of:
- WORDPRESS_DB_USER="${WORDPRESS_DB_USER}"
It should be
- "WORDPRESS_DB_USER=${WORDPRESS_DB_USER}"

Images from external volume on docker return ERR_INVALID_HTTP_RESPONSE

I'm running a docker-compose containing wordpress and an external volume on a different server //yyy.yyy.y.yyy/wptest/docker_wordpress where I'm storing contents from the uploads folder.
Writing and reading media on the file system works just fine, after having set the proper uid and gid settings in the ext-storage.
However, accessing resources via http (for ex. http://localhost:8000/wp-content/uploads/2022/03/myimage.png ) returns a ERR_INVALID_HTTP_RESPONSE
on curling the image url it returns:
Received HTTP/0.9 when not allowed
This is the docker-compose.yml
version: "3.9"
services:
wordpress:
image: wordpress:latest
volumes:
- wordpress_data:/var/www/html
- ext-storage:/var/www/html/wp-content/uploads:rw
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: "xxx.xxx.x.xxx:3306"
WORDPRESS_DB_USER: "docker_wordpress"
WORDPRESS_DB_PASSWORD: "xxxxxxxxxxx"
WORDPRESS_DB_NAME: "docker_wordpress"
volumes:
db_data: {}
wordpress_data: {}
ext-storage:
driver_opts:
type: "cifs"
o: "username=xxxx,password=xxxx,uid=33,forceuid,gid=33,forcegid,file_mode=0644,dir_mode=0755"
device: "//yyy.yyy.y.yyy/wptest/docker_wordpress"
I solved this issue by adding EnableMMAP Off to the vhost config file in docker.
Open terminal and type:
docker exec -it <docker-id> /bin/bash
Then you'll need to edit 000-default.conf
If you don't have vim installed
apt-get update
apt-get install vim
vim /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
....
#EnableSendfile Off
EnableMMAP Off
...
restart Apache
/etc/init.d/apache2 reload
I'm still wondering how to add EnableMMAP Off in docker-compose.yml though.

Docker container taking 27GB on disk while docker container ls --size only report 500MB

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"

docker-composer wordpress asks installation whenever I reboot

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}

Docker Nginx Reverse Proxy

I need to run multiple WordPress containers linked all to a single MySQL container + Nginx Reverse Proxy to easy handle VIRTUAL_HOSTS.
Here is what I'm trying to do (with only one WP for now):
Wordpress (hub.docker.com/_/wordpress/)
Mysql (hub.docker.com/_/mysql/)
Nginx Reverse Proxy (github.com/jwilder/nginx-proxy)
I'm working on OSX and this is what I run on terminal:
docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy
docker run --name some-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:latest
docker run -e VIRTUAL_HOST=wordpress.mylocal.com --name wordpress --link some-mysql:mysql -p 8080:80 -d wordpress
My Docker is running on 192.168.99.100 and that brings me to a 503 nginx/1.9.12 error ofc.
Then 192.168.99.100:8080 brings me to the WordPress as expected.
But http://wordpress.mylocal.com it's not working; it's not redirecting to 192.168.99.100:8080 and I don't understand what I'm doing wrong.
Any suggestions? Thanks!
First of all I recommend you start using docker-compose , running your containers and finding errors will become much easier.
As for your case it seems that you should be using VIRTUAL_PORT to direct to your container on 8080.
Secondly you cannot have two containers(the nginx-proxy + wordpress) napped to the same port on the host.
Good luck!
One:
Use docker compose.
vi docker-compose.yaml
Two:
paste this into the file:
version: '3'
services:
nginx-proxy:
image: budry/jwilder-nginx-proxy-arm:0.6.0
restart: always
ports:
- "80:80"
- "443:443"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
- certs:/etc/nginx/certs:ro
- confd:/etc/nginx/conf.d
- vhostd:/etc/nginx/vhost.d
- html:/usr/share/nginx/html
labels:
- com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy
environment:
- DEFAULT_HOST=example2.com
networks:
- frontend
letsencrypt:
image: jrcs/letsencrypt-nginx-proxy-companion:stable
restart: always
volumes:
- certs:/etc/nginx/certs:rw
- confd:/etc/nginx/conf.d
- vhostd:/etc/nginx/vhost.d
- html:/usr/share/nginx/html
- /var/run/docker.sock:/var/run/docker.sock:ro
environment:
# - LETSENCRYPT_SINGLE_DOMAIN_CERTS=true
# - LETSENCRYPT_RESTART_CONTAINER=true
- DEFAULT_EMAIL=example#mail.com
networks:
- frontend
depends_on:
- nginx-proxy
#########################################################
..The rest of the containers go here..
#########################################################
networks:
frontend:
driver: bridge
backend:
driver: bridge
volumes:
certs:
html:
vhostd:
confd:
dbdata:
maildata:
mailstate:
maillogs:
Three:
Configure as many docker as you need and configure them to your liking. Here are some examples:
mysql (MariaDB):
mysql:
image: jsurf/rpi-mariadb:latest #MARIADB -> 10 #82eec62cce90
restart: always
environment:
MYSQL_DATABASE: nameExample
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_RANDOM_ROOT_PASSWORD: passwordRoot
MYSQL_ROOT_HOST: '%'
ports:
- "3306:3306"
networks:
- backend
command: --init-file /data/application/init.sql
volumes:
- /path_where_it_will_be_saved_on_your_machine/init.sql:/data/application/init.sql
- /physical_route/data:/var/lib/mysql
nginx-php7.4:
nginx_php:
image: tobi312/php:7.4-fpm-nginx-alpine-arm
hostname: example1.com
restart: always
expose:
- "80"
volumes:
- /physical_route:/var/www/html:rw
environment:
- VIRTUAL_HOST=example1.com
- LETSENCRYPT_HOST=example1.com
- LETSENCRYPT_EMAIL=example1#mail.com
- ENABLE_NGINX_REMOTEIP=1
- PHP_ERRORS=1
depends_on:
- nginx-proxy
- letsencrypt
- mysql
networks:
- frontend
- backend
WordPress:
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
- VIRTUAL_HOST=example2.com
- LETSENCRYPT_HOST=example2.com
- LETSENCRYPT_EMAIL=example2#mail.com
volumes:
- wordpress:/var/www/html #This must be added in the volumes label of step 2
You can find many examples and documentation here
You must be careful since in some examples I put images that are for rpi and it is very likely that they will give problems in amd64 and intel32 systems.You should search and select the images that interest you according to your cpu and operating system
Four:
Run this command to launch all dockers
docker-compose up -d --remove-orphans
"--remove-orphans" serves to remove dockers that are no longer in your docker-compose file
Five:
When you have the above steps done you can come and ask what you want, we will be happy to read your dockerFile without dying trying to read a lot of commands
According to your case I think that the best solution for you is to use an nginx reverse proxy that is listening on the docker socket and can pass request to different virtual hosts.
for example, let's say you have 3 WPs.
WP1 -> port binding to 81:80
WP2 -> port binding to 82:80
WP3 -> port binding to 83:80
for each one of them you should use a docker environment variable with the virtual host name you want to use.
WP1-> foo.bar1
WP2-> foo.bar2
WP3-> foo.bar3
After doing so you should have 3 differnt WP with ports exposed on 81 82 83.
Now download and start this nginx docker container (reverse proxy) here
it should listen on the docker socket and retrives all data coming to you machine on port 80.
and when you started the WP container and by the environment variable that you provide he will be able to detect which request shouuld get to which WP instance...
This is an example of how you should run one of you WP docker images
> docker run -e VIRTUAL_HOST=foo.bar1.com -p 81:80 -d wordpres:tag
In this case the virtual host will be the virtual host coming from the http request

Resources