Docker Compose Wordpress+mysql - Alternative ports - wordpress

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.

Related

Wordpress on docker compose doesn't load css

I'm new to docker and databases but my first project on local host doesn't load as expected. The side appears but the content is scattered. Here is the .yml file
version: "3.9"
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:
- ./www:/var/www/html
ports:
- "80:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
Every time you run
$ docker-compose up
Docker create a new network to which all the containers in your compose file will be connected to.
In the first run of this compose file, you do the installation of Wordpress,
and Wordpress insert the IP address of your web site in the database.
The problem occur when you stop this compose file with:
$ docker-compose down
the network created with "docker-compose up" is removed
and when you run the compose file again, docker will create a new network with different IP addresses, so links become broken because your webpages still point to the old IP address.
A solution can be to create a network outside compose file with:
$ docker network create my_network
and empty the volume "dbdata" and the folder "./www" (and lose all your data) then update your compose file with:
version: "3.9"
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:
- ./www:/var/www/html
ports:
- "80:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
networks:
default:
external:
name: my_network
I think there is solution to this problem without losing your data, for more information you can read this article.

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
...

docker-compose wordpress:latest different default port

I'm trying to set up a dockerised site. It will be a node powered app as the front-end and will use wordpress as a headless cms. I've created a docker-compose.yml file that looks like this:
version: "3.3"
services:
web:
build: .
ports:
- "3000:80"
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8080:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
the problem is that wordpress is using port 80. I want to expose the node app on port 80 and have wordpress only accessible on some other port number. I've played around with the wordpress ports mapping but it always seems to sit on port 80:
e.g.
If you want to expose node app on port 80 then portyouwanttoexpose:nodecurrentusingport.
Here on your command you have done is exposing port 80 to 3000 exactly opposite.
I think first use correct ports for node and then try other ports for exposing your wordpress cms

Connecting to my local docker Database Instance from Table Plus

I have created a local docker wordpress instance and I am trying to connect to the database with a SQL Client (in my case TablePlus) but I am having trouble.
I created the docker containers from a docker-compose.yml file shown here:
version: '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:
- "8028:80"
- "8029:8029"
volumes:
- ./themes/travelmatic:/var/www/html/wp-content/themes/yadayada
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
VIRTUAL_HOST: leasepilot.local
volumes:
db_data:
I have tried any comibindation of wordpress and somewordpress in these fields:
I also have the option to connect over SSH but I don't feel I would need to do that?
1) What is the best way to debug this type of issue?
2) What are the creds? lol
There is another bit of information that should be added to the Praveen answer.
If you have already mysql installed locally, on your computer/laptop, settings the db ports to:
- "3306:3306"
it won't work because TablePlus will connect to your local mysql instance.
Instead you should set your Docker mysql on a different published port and access that from TablePlus.
For example, set these ports on your Dockerfile (published port is 3356):
"3356:3306"
Then set the same port on TablePlus:
Just as David has suggested in his comment, you need to add port mapping in docker-compose.yml. So, your modified docker-compose.yml would be something like this:
version: '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
ports:
- "3306:3306"
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8028:80"
- "8029:8029"
volumes:
- ./themes/travelmatic:/var/www/html/wp-content/themes/yadayada
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
VIRTUAL_HOST: leasepilot.local
volumes:
db_data:
And you have already provided the creds in the docker-compose.yml in environment variables.

Docker containers starts at different subtnets

I have to start up 3 containers, nginx, wordpress and wordpress2. The problem is the nginx container starts at 172.17.0.2, wordpress at 172.18.0.3 and wordpress2 at 172.19.0.4.
The wordpress containers starts with their docker-compose.yml configured as i show:
For wordpress:
version: '2'
services:
db:
image: mysql:5.7
volumes:
- "./.data/db:/var/lib/mysql"
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
links:
- db
ports:
- "8000:80"
networks:
- ipv4_address: 172.17.0.5
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: wordpress
For wordpress2:
version: '2'
services:
db:
image: mysql:5.7
volumes:
- "./.data/db:/var/lib/mysql"
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
links:
- db
ports:
- "8001:80"
networks:
- ipv4_address: 172.17.0.6
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: wordpress
As you can see, i put at networks field the ipv4_address but when i start up the docker-compose.yml it shows error at this line. I thought that by default, all containers usually stars at same network but not in this case. Can you help me to put all containers at same subnet?
Each docker-compose.yml will create it's own subnet. So your 3 containers can't see each other.
Networks created by other docker-compose are said to be external so you have to declare them as such. You can see these networks using docker network ls.
After declaring the external networks in your external networks in your docker-compose.yml file, you will have to define what networks should be using by the nginx service. You should keep the default network.
Usually the default name for your docker-compose network is <current_dir>_default. So suppose your current directory is called yourproject1 the network name is going to be yourproject1_default.
And then you will be able to access your wordpress containers using external_links (better than ip addresses IMO)
Here what I would do for nginx's docker-compose.yml file to make him able to see wordpress containers :
version: '2'
services:
nginx:
...
external_links:
- your_project1_wordpress1_1:wordpress1
- your_other_project2_wordpress2_1:wordpress2
networks:
- default
- your_project1_default
- your_other_project2_defaul
networks:
your_project1_default:
external: true
your_other_project2_default:
external: true

Resources