Docker container, connect to each others databases, Nginx proxy and WordPress - wordpress

I trying to figure out how docker network and that kind of stuff works.
So here is my setup, I have 2 docker containers with WordPress and mysql. And theese two needs to be able to talk to each other thru direct database connections. And I have domain pointed to them also.
So I tried to set it up with an docker nginx proxy, but when i'm trying to visit that site thru the domain i'm getting 502 bat gateway.
So my docker ngxinx proxy compose file looks like this, but with different ports:
version: "3.1"
services:
nginx-proxy:
image: jwilder/nginx-proxy:alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./certs:/etc/nginx/certs
- /var/run/docker.sock:/tmp/docker.sock:ro
restart: unless-stopped
networks:
default:
external:
name: nginx-proxy
And both of my WordPress containers look like this:
version: '3.1'
services:
wordpress:
image: wordpress
restart: always
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
VIRTUAL_HOST: wp1.local
VIRTUAL_PORT: 3000
volumes:
- ./wp:/var/www/html
- ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
expose:
- 3000
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
ports:
- "8086:3306"
mailhog:
image: mailhog/mailhog
ports:
- "1025:1025" # smtp server
- "8025:8025" # web ui
networks:
default:
external:
name: nginx-proxy
volumes:
db:
And I have added 127.0.0.1 wp1 to /etc/hosts
But when I start the proxy and one of the WP containers I'm getting bad gateway. And I have no clue how to move forward. And maybe this is not even the right way todo it. Becuase the problem i'm trying to solve is that wp2 needs to be able to connect to wp1 database thru custom pdo connection.
I tried to explain as good as I can, but this is a new area for me when it comes to docker networks. Also i'm running docker for windows with wsl2

You should add
networks:
- default
to each service definition in docker-compose.
version: '3.1'
services:
wordpress:
image: wordpress
restart: always
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
VIRTUAL_HOST: wp1.local
VIRTUAL_PORT: 3000
volumes:
- ./wp:/var/www/html
- ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
expose:
- 3000
networks:
- default
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
ports:
- "8086:3306"
networks:
-default
mailhog:
image: mailhog/mailhog
ports:
- "1025:1025" # smtp server
- "8025:8025" # web ui
networks:
- default
networks:
default:
external:
name: nginx-proxy
volumes:
db:

Related

Add ioncube to wordpress image

I'm trying to serve multiple instances of
i have the following docker compose script to create a docker-WordPress container :
version: '3.7'
services:
db:
container_name: "${SITE_NAME}-db"
image: mysql:latest
env_file:
- .env
environment:
MYSQL_DATABASE: ${DB_NAME}
MYSQL_USER: ${DB_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_RANDOM_ROOT_PASSWORD: ${ROOT_PASSWORD}
ports:
- "3306"
volumes:
- './db:/var/lib/mysql'
restart: always
networks:
- lan
site:
container_name: "${SITE_NAME}-web"
image: wordpress:latest
environment:
WORDPRESS_DEBUG: 0
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: ${DB_USER}
WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
WORDPRESS_DB_NAME: ${DB_NAME}
WORDPRESS_CONFIG_EXTRA: |
define('AUTOMATIC_UPDATER_DISABLED', true);
VIRTUAL_HOST: ${SITE_DOMAIN},www.${SITE_DOMAIN}
LETSENCRYPT_HOST: ${SITE_DOMAIN},www.${SITE_DOMAIN}
volumes:
- './wordpress:/var/www/html/wp-content'
- './upload.ini:/usr/local/etc/php/conf.d/uploads.ini'
restart: always
depends_on:
- db
networks:
- lan
- net
volumes:
db:
wordpress:
upload.ini:
networks:
lan:
internal: true
net:
external: true
On instance of a website requires for one of it's plugins the installation of ioncube
I cant seem to make it work.
Is there a way to add ioncube ?
Perhaps it makes sense to Use a custom Docker Image for your Wordpress instance. You can create one based on the wordpress image you already use and than add ioncube to it for example like this:
https://github.com/atsu666/docker-ioncube/blob/master/Dockerfile#L43

docker compose file not working: services.wordpress Additional property port is not allowed

I am using the below codes but docker-compose file not working. I need to setup WordPress in docker for different environment.
ERROR: services.wordpress Additional property port is not allowed
Code:
version: '3'
services:
# Database
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD : password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- wpsite
# Wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
port:
- '8000:80'
restart: always
volumes: ['./:/var/www/html']
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
networks:
- wpsite
networks:
wpsite:
volumes:
db_data:
Actually it's "ports" (plural) that can accept a list, delimited by "-" and not 'port' (singular). Kindly check the below docs for docker compose file (yaml)
https://docs.docker.com/compose/compose-file/compose-file-v3/

Docker Compose Wordpress+mysql - Alternative ports

mysql by default listens to ports 3306 and 33060.
since im launching multiple instances, i have to provide alternative ports.
i managed to map the first one <DB_HOST: 8002> to <3306> but i cannot find any documentation for the second wordpress parameter name, i just found DB_HOST.
do you know how i can also map the second port?
version: "3.9"
services:
db:
image: mysql:latest
volumes:
- wordpress_db_data:/var/lib/mysql
ports:
- "8002:3306"
- "8003:33060"
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- wordpress_data:/var/www/html
ports:
- "80:80"
restart: unless-stopped
environment:
DB_HOST: 8002
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
wordpress_db_data: {}
wordpress_data: {}
On the bridge network that docker-compose creates, you use the container ports directly. You only use the mapped ports when you connect to a container from the host computer.
Since WordPress and MySQL are on the same network, you should use port 3306. You only need to map the MySQL ports to host ports if you intend to connect to the database from the host.
Also, WordPress doesn't connect to MySQL using port 33060, so that's why there's no parameter for it on the WordPress side.
Here's a docker-compose file that should work
version: "3.9"
services:
db:
image: mysql:latest
volumes:
- wordpress_db_data:/var/lib/mysql
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- wordpress_data:/var/www/html
ports:
- "80:80"
restart: unless-stopped
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
wordpress_db_data: {}
wordpress_data: {}
Note that I've removed the port mappings on the database, since they're not needed for WordPress to work. If you need to connect to the database from the host, you can just add them back. Usually port 3306 is enough. 33060 is rarely used, AFAIK.

Docker Wordpress: Error establishing a database connection

I'm having trouble deploying a Wordpress install via Docker compose/Portainer. Every time I try to deploy the docker-compose I get the following error: Error establishing a database connection on the Wordpress install.
I believe there is a mistake somewhere in the docker-compose below as I have eliminated other possibilities. It seems the WP install cannot communicate with the database volume in the container.
Any help would be appreciated, thanks
System info:
OS: Ubuntu 21.04
System: Rpi4 64bit
Docker-compose version: 1.29.2
docker-compose:
version: "3.9"
services:
wordpress:
depends_on:
- db
image: wordpress:latest
container_name: wordpress
restart: unless-stopped
ports: # For inital setup
- "91:80"
security_opt:
- no-new-privileges:true
environment:
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: mbA2tU9RyA5r#U
WORDPRESS_DB_NAME: wordpress
volumes:
- sbabaya:/var/www/html
mariadb:
image: mariadb:latest
container_name: db
restart: always
environment:
MYSQL_ROOT_PASSWORD: mbA2tU9RyA5r#U
MYSQL_DATABASE: wordpress
volumes:
- "db:/var/lib/mysql"
logging:
driver: "json-file"
options:
max-size: "10Mb"
max-file: "5"
volumes:
wordpress:
db:
You have missed the database host - this will be the container label used in the compose file, they are automatcally setup in their own private network.
Update your environment as follows -
environment:
WORDPRESS_DB_HOST: sbabaya-db
WORDPRESS_DB_USER: sbabaya
WORDPRESS_DB_PASSWORD: mbA2tU9RyA5r#U
WORDPRESS_DB_NAME: cue_sbabaya
Check your names, keep it simple - you might get more milage form mysql offical image (will work on arm64 as well).
This is also consistent with the offical docs here - https://hub.docker.com/_/wordpress
version: '3.1'
services:
wordpress:
image: wordpress
restart: always
ports:
- 80:80
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
- wordpress:/var/www/html
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
MYSQL_RANDOM_ROOT_PASSWORD: '1'
volumes:
- db:/var/lib/mysql
ports:
- 3306:3306
volumes:
wordpress:
db:

how to create multiple wordpress sites on docker

i have created one wordpress site using docker. to open it i need to open browser in got to localhost:8000. But if i want to make multiple wordpress sites how and where and what i need to configure to reach every each of them seperatly, becouse i cant get to them using only the same link. i am noob at docker.
i have created .yaml file from tutorial in youtube
version: '3'
# Database
services:
db:
image: mysql:5.7
platform: linux/amd64
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- wpsite
# phpmyadmin
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '8080:80'
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: password
networks:
- wpsite
# Wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- '8000:80'
restart: always
volumes: ['./:/var/www/html']
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
networks:
- wpsite
networks:
wpsite:
volumes:
db_data:
and then i run the dockeer command
docker-compose up -d
which made all wordpress files
to open it i need to open link localhost:8000
how do i open another wordpress site? becouse i cant open them both with the same link.
If you want to be able to access multiple services then just assign them different ports.
In your example you configured wordpress to be exposed on port 8000 using:
ports:
- '8000:80'
So just assign to another wordpress instance(s) different port(s) (eg 8001, 8002, ...) using:
ports:
- '8001:80'
Example docker-compose.yaml
version: '3'
services:
db:
image: mysql:5.7
platform: linux/amd64
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress-site1
MYSQL_USER: wordpress-site1
MYSQL_PASSWORD: wordpress-site1
networks:
- wpsite
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '8080:80'
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: password
networks:
- wpsite
wordpress-site1: # Wordpress site 1
depends_on:
- db
image: wordpress:latest
ports:
- '8000:80' # This site will be accessible on localhost:8000
restart: always
volumes: ['./site-1:/var/www/html'] # Set different path on host
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_NAME: wordpress-site1
WORDPRESS_DB_USER: wordpress-site1
WORDPRESS_DB_PASSWORD: wordpress-site1
networks:
- wpsite
wordpress-site2: # Wordpress site 2
depends_on:
- db
image: wordpress:latest
ports:
- '8001:80' # This site will be accessible on localhost:8001
restart: always
volumes: ['./site-2:/var/www/html'] # Set different path on host
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_NAME: wordpress-site2
WORDPRESS_DB_USER: wordpress-site2
WORDPRESS_DB_PASSWORD: wordpress-site2
networks:
- wpsite
# Other wordpress instances
networks:
wpsite:
volumes:
db_data:
Don't forget that every wordpress service will need it's own database.
You can either define for each wordpress service it's own database service or use single (shared) DB service and create on it multiple databases (one for each wordpress service).
To do so first start up only db and phpmyadmin services using:
$ docker-compose up -d db phpmyadmin
Since PhpMyAdmin is set to be exposed on port 8080 go to http://localhost:8080, log in and create for each wordpress service it's own database and user.
Once done, update WORDPRESS_DB_* env variables (if needed) in docker-compose.yaml to match databases and user credentials you just created and if everything is ok start all wordpress services.
Based on example docker-compose.yaml you should be able to access:
wordpress-site1 on hppt://localhost:8000
wordpress-site2 on hppt://localhost:8001
...

Resources