docker-compose with wp-cli and custom theme wrong permission - wordpress

I have the docker-compose file at the end of the post
In this file I create a volume to link the custom theme folder.
When I try to run a wp-cli command in a CONTAINER - CLI I have a permission problem because the folder wp-content is owned by root and the container is run as user xfs.
In the CONTAINER - WORDPRESS we can see that the wp-content folder is also owned by root.
CONTAINER - WORDPRESS
CONTAINER - CLI
But when I unlink my custom theme folders this issue no longer happens.
it is possible to see in the CONTAINER - CLI that the owner of the wp-content folder is xfs as it is correct, as well as in the WORDPRESS - CONTAINER the wp-content folder also belongs to the user www-data as it should be.
CONTAINER - WORDPRESS
CONTAINER - CLI
This is mine docker-compose.yml
version: "3.9"
services:
wordpress:
image: wordpress:5.2.1-php7.3
environment:
WORDPRESS_DB_HOST: database:3306
WORDPRESS_DB_NAME: ${MYSQL_DATABASE}
WORDPRESS_DB_USER: ${MYSQL_USER}
WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
WORDPRESS_TABLE_PREFIX: ${WORDPRESS_TABLE_PREFIX}
WORDPRESS_DEBUG: ${WORDPRESS_DEBUG:-false}
env_file: .env
restart: always
volumes:
- ./wp:/var/www/html # Full wordpress project
- ./theme:/var/www/html/wp-content/themes/custom-theme # Theme development
ports:
- ${WORDPRESS_PORT}:80
depends_on:
- database
cli:
image: wordpress:cli-2.3.0
env_file: .env
user: xfs
depends_on:
- database
- wordpress
volumes:
- ./wp:/var/www/html
database:
image: mariadb:10.5.9
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
restart: always
volumes:
- db_data:/var/lib/mysql
healthcheck:
test: mysqladmin ping -h 127.0.0.1 -u root --password=${MYSQL_PASSWORD}
interval: 5s
retries: 5
manage:
image: phpmyadmin:5
environment:
PMA_HOST: database
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
ports:
- ${MANAGE_PORT}:80
restart: always
depends_on:
database:
condition: service_healthy
volumes:
db_data:
wp_data:

I found on this answer here on StackOverflow that you need to add ./wp-content/:/var/www/html/wp-content/ to the volumes, because:
Docker defaults any named volumes to root, so by binding it dropped the root owner.

Related

docker/wordpress/docker-compose permissions on bind mount volume trying to install themes and plugins

I getting permission issues (or so it appears) on a bind mounted volume on a Mac that is preventing themes and plugins from being installed. I'm using docker-compose to create my container as follows.
wordpress:
depends_on:
- db
image: wordpress
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: *****
WORDPRESS_DB_NAME: wordpress
working_dir: /var/www/html
volumes:
- ./wp-content:/var/www/html/wp-content
The Container is created no problem. On my host mac the wp-content directory is created just fine and correctly populated. I can connect to my container (using docker exec) and see the mounted volume has default permissions of drwxr-xr-x 1 1000 staff 160 Aug 14 23:04 wp-content Whenever I try to install a theme or plugin it prompts for FTP credentials or if I add define('FS_METHOD', 'direct') to wp-config.php fails with "Could not create directory". I've also tried making permissions to wp-config from my host host wide open with chmod -R 777 wp-content and changing ownership from the container with chown -r www-data:www-data wp-content. Enabling debugging produces no debug.log in wp-content. I've also tried running apache as user 1000 using user: "1000:1000" and/or environment: APACHE_RUN_USER/APACHE_RUN_GROUP as per https://github.com/docker-library/wordpress/pull/249. Nothing seems to make any difference and I have no idea what to try next. I suspect this is something really simple but have exhausted everything I can find. Any tips or help would be greatly appreciated. I'm just trying to do some basic plugin development and would like to edit wp-content locally.
I think you need to make volume from the root directory of your wordpress install.
There is my docker-compose.yml file
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: dbname
MYSQL_USER: dbuser
MYSQL_PASSWORD: dbpassword
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- wp_data:/var/www/html
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: dbuser
WORDPRESS_DB_PASSWORD: dbpassword
WORDPRESS_DB_NAME: dbname
volumes:
db_data: {}
wp_data:
driver: local
driver_opts:
type: none
o: bind
device: /var/www/html/

Copying plugins to base wordpress image, Plugins folders are visible in cmd but not visible in volume mount

Copy plugins to wordpress base image. Plugins folders are visible in cmd CMD using command docker run -it --rm arslanliaqat/testwordpressimage sh but not visible in volume mounted Folder. I have attached the screenshot of cmd commands and folder as well.
Dockerfile
FROM wordpress:php7.1-apache
WORKDIR /var/www/html
COPY plugins/wordpress-seo/ /var/www/html/wp-content/plugins/wordpress-seo/
COPY plugins/wp-super-cache/ /var/www/html/wp-content/plugins/wp-super-cache/
EXPOSE 80
Docker-compose.yml
version: '3.3'
services:
db:
image: 'mysql:5.7'
volumes:
- './dbdata:/var/lib/mysql'
restart: always
environment:
#PMA_HOST: db
#PMA_PORT: 3000
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: 'arslanliaqat/testwordpressimage:latest'
#image: 'wordpress:latest'
volumes:
#- './wordpress/:/var/www/html/'
- './Wordpress:/var/www/html'
#- './wordpress:/var/www/html'
#- './wordpress/wp-content/plugins/wordpress-seo/:/wp-content/wordpress-seo/'
#- './wordpress/wp-content/plugins/wp-super-cache/:/wp-content/wp-super-cache/'
# - './plugins/wordpress-seo/:/var/www/html/wp-content/plugins/wordpress-seo/'
# - './plugins/wp-super-cache/:/var/www/html/wp-content/plugins/wp-super-cache/'
- './docker/wordpress/php/php.ini:/usr/local/etc/php/conf.d/php.ini:ro'
ports:
- '8000:80'
restart: always
environment:
WP_LOCALE: en_US
WORDPRESS_LOCALE: en_US
WORDPRESS_DB_HOST: 'db:3306'
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_TABLE_PREFIX: wp_
WORDPRESS_DEBUG: 1
WORDPRESS_DB_NAME: wordpress
working_dir: /var/www/html
volumes:
db_data:
wordpress:
Bind mounts have been around since the early days of Docker. Bind mounts have limited functionality compared to volumes. When you use a bind mount, a file or directory on the host machine is mounted into a container.
That means: when you define - './Wordpress:/var/www/html' in compose, the directory /var/www/html in container will be override by the directory ./Wordpress on host. So you can just see the contents of your hosts.
But for this, you did not use bind mount volume, so your things in container not override by host directory.
Detail refers to official guide

Wordpress intallation on Docker. trying to skip installation page

Running wordpress on docker. Trying to skip install install.php by defining user in compose file. I have already added wordpress user , password and title to skip that page. but not working.
Is there some problem with my code? Is there another way to do it. or help me fix this code.
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
volumes:
- ./wordpress:/var/www/html
- ./docker/wordpress/php/php.ini:/usr/local/etc/php/conf.d/php.ini:ro
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_VERSION: 5.1
WORDPRESS_LOCALE: en_US
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_TABLE_PREFIX: "wp_"
WORDPRESS_DEBUG: 1
WORDPRESS_DB_NAME: wordpress
# WORDPRESS_WEBSITE_TITLE: "My blog"
# WORDPRESS_WEBSITE_URL: "http://example.com"
# WORDPRESS_WEBSITE_URL_WITHOUT_HTTP: "example.com"
# WORDPRESS_WEBSITE_URL: "http://http://localhost:8000"
# WORDPRESS_WEBSITE_URL_WITHOUT_HTTP: "localhost"
# WORDPRESS_WEBSITE_POST_URL_STRUCTURE: "/%year%/%monthnum%/%day%/%postname%/"
# WORDPRESS_ADMIN_USER: "admin"
# WORDPRESS_ADMIN_PASSWORD: "admin"
# WORDPRESS_ADMIN_EMAIL: "admin#admin.com"
working_dir: /var/www/html
wordpress-cli:
depends_on:
- db
- wordpress
image: wordpress:cli
entrypoint: wp
user: xfs
command: >
/bin/sh -c ' sleep 10;
wp core install --url="http://localhost:8000" --title="Sample Title" --admin_name=admin --admin_password=admin --admin_email=you#domain.com '
volumes:
- ./wordpress:/var/www/html
- ./docker/wordpress/php/php.ini:/usr/local/etc/php/conf.d/php.ini:ro
volumes:
db_data: {}
wordpress:
Got this error:
C:\DockerProjects\test6>docker-compose up
ERROR: yaml.scanner.ScannerError: while scanning a simple key
in ".\docker-compose.yml", line 54, column 6
could not find expected ':'
in ".\docker-compose.yml", line 55, column 6
Problem is in this part of code
user: xfs
command: >
/bin/sh -c ' sleep 10;
wp core install --url="http://localhost:8000" --title="Sample Title" --admin_name=admin --admin_password=admin --admin_email=you#domain.com '
It has to be like this
user: xfs
command: >
/bin/sh -c ' sleep 10;
wp core install --url="http://localhost:8000" --title="Sample Title" --admin_name=admin --admin_password=admin --admin_email=you#domain.com '
after command: > next 2 lines you need to give 1 Tab.
You can check your code with online formatter here.
Edit 1:
Formatted Code looks like this
version: '3.3'
services:
db:
image: 'mysql:5.7'
volumes:
- './db-data:/var/lib/mysql'
restart: always
environment: null
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: 'wordpress:latest'
volumes:
- './wordpress:/var/www/html'
- './docker/wordpress/php/php.ini:/usr/local/etc/php/conf.d/php.ini:ro'
ports:
- '8000:80'
restart: always
environment: null
WORDPRESS_VERSION: 5.1
WORDPRESS_LOCALE: en_US
WORDPRESS_DB_HOST: 'db:3306'
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_TABLE_PREFIX: wp_
WORDPRESS_DEBUG: 1
WORDPRESS_DB_NAME: wordpress
working_dir: /var/www/html
wordpress-cli:
depends_on:
- db
- wordpress
image: 'wordpress:cli'
entrypoint: wp
user: xfs
command: "/bin/sh -c ' sleep 10; wp core install --url=\"http://localhost:8000\" --title=\"Sample Title\" --admin_name=admin --admin_password=admin --admin_email=you#domain.com '\n"
volumes:
- './wordpress:/var/www/html'
- './docker/wordpress/php/php.ini:/usr/local/etc/php/conf.d/php.ini:ro'
volumes:
db_data: {}
wordpress: null
You can install wordpress Cli either in the compose file. or you can copy the wp cli into the image using dockerfile and install wordpress using custom entrypoint.
I've found this on dockerhub:
Since March 2021, WordPress images use a customized wp-config.php that pulls the values directly from the environment variables defined above (see wp-config-docker.php in docker-library/wordpress#572 and docker-library/wordpress#577). As a result of reading environment variables directly, the cli container also needs the same set of environment variables to properly evaluate wp-config.php.
So you'd need to set the environment like in your original wordpress, like below (which worked for me, also using traefik..):
version: "3.9"
services:
db:
image: mysql:5.7
volumes:
- ./db:/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
restart: always
environment: &env
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
WORDPRESS_CONFIG_EXTRA: |
define( 'AUTOMATIC_UPDATER_DISABLED', true );
define( 'FS_METHOD', 'direct' );
volumes:
- &html
./html:/var/www/html
labels: &lab
- "traefik.enable=true"
- "traefik.port=80"
- "traefik.frontend.rule=Host:subdomain.domain.com"
networks:
- default
- web
wordpress-cli:
image: wordpress:cli
depends_on:
- db
- wordpress
environment:
<<: *env
volumes:
- *html
entrypoint: sh
command: -c 'sleep 10; wp core install --url="https://subdomain.domain.com" --title="greatname" --admin_name=admin --admin_password="mysupersecureadminpw" --admin_email=my#domain.com'
networks:
web:
external: true
You can also leave out the entrypoint and command part and just run docker-compose run wordpress-cli core install ... after you checked that everything is running. If you remove the entrypoint part, you can also run any other wp-cli command instead of core install that way :-)

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