How to import my db dump into mariadb on start? - wordpress

I have a docker-compose.yml file like this
version: '2'
services:
wordpress:
image: wordpress
ports:
- 8080:80
environment:
WORDPRESS_DB_NAME: my_wp
WORDPRESS_DB_USER: my_user
WORDPRESS_DB_PASSWORD: my_pass
volumes:
- ./src:/var/www/html
mysql:
image: mariadb
environment:
MYSQL_ROOT_PASSWORD: my_pass
volumes:
- ./db:/etc/mysql
I can run my containsers using the command docker-compose up and it works fine, but shows the default wordpress site with empty db to setup. What I want is, I have a db dump file my_db.sql which I want to load into mariadb when its initializing. How can I do that? and where do I need to put my my_db.sql file to be picked by mariadb container?

You have 3 options:
Mount your .sql file to /docker-entrypoint-initdb.d/
version: '2'
services:
wordpress:
image: wordpress
ports:
- 8080:80
environment:
WORDPRESS_DB_NAME: my_wp
WORDPRESS_DB_USER: my_user
WORDPRESS_DB_PASSWORD: my_pass
volumes:
- ./src:/var/www/html
mysql:
image: mariadb
environment:
MYSQL_ROOT_PASSWORD: my_pass
volumes:
- ./db:/etc/mysql
- ./my_db.sql:/docker-entrypoint-initdb.d/my_db.sql
Use build directive in your docker-compose file and point to a dockerfile with following content:
FROM mysql
COPY my_db.sql /docker-entrypoint-initdb.d
Create a dummy container, Mount /my/own/datadir:/var/lib/mysql, then restore the database manually using the docker exec, then you can use the host directory when ever you want that db in a container.

Mount the folder with your sql files to /docker-entrypoint-initdb.d on the container in the volumes section of the db definitions. Any .sql (and .sql.gz) files will be automatically run in.
See the Initializing a fresh instance section of the docs for more info.

Related

wp-cli in Docker fetches wrong database login credentials from wp-config

Here is my docker-compose.yml file:
version: '3.9'
services:
db:
image: mysql:5.7
restart: always
command: [
'--disable-partition-engine-check',
'--default_authentication_plugin=mysql_native_password',
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_unicode_ci',
'--max_allowed_packet=100M'
]
volumes:
- ./db_data:/var/lib/mysql
environment:
MYSQL_DATABASE: wordpress
MYSQL_ROOT_PASSWORD: pswd4!
pma:
image: phpmyadmin/phpmyadmin
environment:
PMA_HOST: db
PMA_PORT: 3306
MYSQL_ROOT_PASSWORD: pswd4!
UPLOAD_LIMIT: 1G
ports:
- 8080:80
depends_on:
- db
wp:
image: wordpress:php8.0
ports:
- 80:80
volumes:
- ./config/php.conf.ini:/usr/local/etc/php/conf.d/conf.ini
- ./:/var/www/html
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: pswd4!
WORDPRESS_DEBUG: 1
depends_on:
- db
links:
- db:mysql
hostname: test.localhost
wpcli:
image: wordpress:cli-php8.0
volumes_from:
- wp
depends_on:
- db
- wp
links:
- db:mysql
entrypoint: wp
command: "--info"
volumes:
db_data:
When I try to use wp-cli in Docker (e.g. docker-compose run --rm wpcli plugin list), it gets an error that it cannot connect to the database:
Error: `Access denied for user 'username_here'#'192.168.32.5' (using password: YES)`
Error establishing a database connection
This either means that the username and password information in your `wp-config.php` file is incorrect or we can’t contact the database server at `mysql`. This could mean your host’s database server is down.
Are you sure you have the correct username and password?
Are you sure you have typed the correct hostname?
Are you sure the database server is running?
If you’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums. `Access denied for user 'username_here'#'192.168.32.5' (using password: YES)
`
It looks like wp-cli is seeing bad database credentials (username_here instead of root)
Result of executing docker-compose run --rm wpcli config list command:
What could be wrong? I've searched all over the internet, lost several hours and still haven't found the cause of the problem.
You should specify the same set of environment variables as in your wp container for your wpcli container. If not, default variables from a php file in wordpress are used.
Do be careful : volumes_from and link options are deprecated in compose v3. For the link option, docker-compose creates a network automatically (or you can specify one if you prefer) and the embed docker dns automatically attributes aliases to your containers based on their names (or in compose the service name). More info on that here
For volumes, you can find more info here

Where is located data folder in docker-compose for wordpress

I would like to deploy my instance of wordpress using docker compose.
I have docker-compose code:
version: '3.3'
services:
wordpress:
image: wordpress:latest
ports:
- "8000:80"
restart: always
volumes:
- ./data-docker/wordpress:/data
environment:
WORDPRESS_DB_HOST: host.docker.internal:3306
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD:
WORDPRESS_DB_NAME: wordpress
I would like to have some "wordpress" folder contains data (images, etc) on my local machine to be able make a backup of current data. I tried to use ./data-docker/wordpress:/data but this folder is still empty.
Where are for example uploaded images stored? And how can I backup it?
I know that there is also mysql - it is backuped separately.
According to wordpress docker documentation the following should work:
...
volumes:
- ./data-docker/wordpress:/var/www/html
...

How does WordPress default docker image copies files into the host system?

There is a question that asks about volumes, mine is a bit different.
What I'm interested in is that when I use a simple docker-compose.yml file
version: '3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: user
MYSQL_PASSWORD: password
app:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
volumes:
- .:/var/www/html/
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: user
WORDPRESS_DB_PASSWORD: password
volumes:
db_data:
The default image somehow downloads the entire WordPress installation to my host. How does it do that?
I'm asking, because I have a custom Dockerfile in which I ADD the wp zip file, unzip it and put the contents to /var/www/html which is mapped in my docker-compose.yml file in the ../:/var/www/html (my docker-compose.yml file is in the project-root/.docker/ folder so I map the project root to the WP root).
When I tried with the official image the contents were copied to my host, so I'm clearly missing some crucial part that is in the official images. But which one?
So it turns out that you have to copy/download/install using wpcli the WP inside the entrypoint script, and native WP docker images are doing that. Either that or inside the CMD command.
It's the same with this question.
Once done there, all the files in the container will appear in the host as well.

Docker volumes that share the same parent?

I'm trying to build a docker based WordPress development environment and I want to be able to have a folder structure like this:
.
|
--wp-data
|
--wp-content
|
--plugins
|
--themes
where plugins and themes are also inside wp-content
this is my docker-compose file:
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- ./wp-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:
- '8000:80'
restart: always
volumes:
- ./wp-content:/var/www/html/wp-content/
- ./themes:/var/www/html/wp-content/themes/
- ./plugins:/var/www/html/wp-content/plugins/
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
wp-data:
wp-content:
themes:
plugins:
the wp-data and w-content are created ok
but the nested themes and plugins arent
what im missing?
Regards
You are misusing volumes. In your docker-compose.yml you create bind mounts for each service - that means you mount a particular directory of the host into containers.
At the same time you are declaring a section volumes where explicitly declare volumes with the same names, but they are never used and created as empty directories.
Of you want to create and use volumes, you need to rewrite your docker-compose.yml in the following manner:
services:
...
db:
...
volumes:
- wp-data:/var/lib/mysql
...
wordpress:
volumes:
- wp-content:/var/www/html/wp-content/
- themes:/var/www/html/wp-content/themes/
- plugins:/var/www/html/wp-content/plugins/
volumes:
wp-data:
wp-content:
themes:
plugins:
This will enable volumes, but you still need a way to put data from host into them (like docker cp for example).
From the other hand, of you intended to use bind mounts, you need to completely remove ending volumes: section not to get confused.

Docker Compose WordPress Volumes Appear Empty

I'm trying to set up a simple WordPress build using docker compose. However, when I build it, the volumes appear to be empty.
Here is my docker-compose.yml:
version: '3'
services:
wordpress:
image: wordpress
restart: always
ports:
- 8000:80
volumes:
- ./development:/var/www/html
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: password
depends_on:
- db
networks:
- wordpress-network
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
ports:
- 8080:80
links:
- db:db
db:
image: mariadb:latest
ports:
- 127.0.0.1:3306:3306
command: [
'--default_authentication_plugin=mysql_native_password',
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_unicode_ci'
]
volumes:
- wp-data:/var/lib/mysql
environment:
MYSQL_DATABASE: wordpress
MYSQL_ROOT_PASSWORD: password
networks:
- wordpress-network
networks:
wordpress-network:
driver: bridge
volumes:
wp-data:
driver: local
Here is my local project structure, with theme stylesheet:
I run docker-compose to build the image:
docker-compose up -d --build
But when I open the build in my browser, it looks like the theme is empty:
This leads me to believe the volume is empty. I'd appreciate any help or insights into this issue, thank you.
In your docker-compose file you say ./wp-data:/var/lib/mysql which is host folder mapping (not volume) but in your docker-compose you define docker named volume called wp-data and if you want to use this volume you have to use it as wp-data:/var/lib/mysql. I would also suggest to remove ${PWD} because it might cause problem in sh

Resources