Wordpress on docker compose doesn't load css - wordpress

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.

Related

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

How do I setup a local wordpress using Docker?

So I decided to try Docker for my local Wordpress development.
Luckily, Docker has a quickstart guide for that.
I followed the entire process and I *think* I understood most of it. However, when I had the Docker container up and running, it made a clean installation of Wordpress instead of using the local files I had for a project. I initially thought that changing the directory to the project folder allowed it to read the files in it. Apparently, I was mistaken. I've tried searching the net for an answer and
most of them are just tutorials into how to use Docker for WP.
So with that in mind, how do I create a Docker container (or change the Docker YAML file) that uses the local WP files I have?
docker-compose.yml
version: '3.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_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
Project structure
/project
/app
/sql
docker-compose.yml
I'm running on Ubuntu 19.04
Wordpress on docker
Create the docker-compose.yml with the follwing
version: '3.1'
services:
wordpress:
image: wordpress
restart: always
ports:
- 8080:80
volumes:
- ./app:/var/www/html
environment:
WORDPRESS_DB_PASSWORD: DoKRteST
WORDPRESS_DB_HOST: mysql
mysql:
image: mysql:5.7
# Uncomment the below code to maintain the persistancy of the data
# volumes:
# - ./wordpress:/var/www/html
restart: always
environment:
MYSQL_ROOT_PASSWORD: DoKRteST
./app folder is the actual wordpress app folder
Simply click on
to run it on Play with docker
You just need to mount wp-content of the host to the container. you can look for wp-content in your current directory structure probably under app/wp-content
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
volumes:
- wp-content:/var/www/html/wp-content
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
You can read more details here and here

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.

How to access wordpress-docker site on another computer in network

I created WordPress website and configure it within the Docker. The site displays all it's contains locally correctly. Now I want to access that website from the another machine within same network but it did not display all the contents correctly. it displays only the text. no images and the applied theme is not displays. I think problem is WORDPRESS_DB_HOST: db:3306 but how to change that. I access my site locally at http://localhost:8000/
version: '2'
services:
wordpress:
depends_on:
- db
image: wordpress:latest
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
ports:
- 8000:80
- 4443:443
networks:
- back
db:
image: mysql:5.7
restart: always
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- back
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 8001:80
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: somewordpress
networks:
- back
networks:
back:
volumes:
db_data:
From another system in the same network, you can call the WordPress website using the IP of the System where your docker container is running.
For Example, Let say you are running the WordPress Container in System A.
Find the System IP of A It will be like 192.168.0.10
Then from System B Call 192.168.0.10:8000 This will work for you.

Resources