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 :-)
Related
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.
I tried using networks, but the network with the folder's name concatenated with "_default" is not found.
If I specify the network like this:
wpcli:
container_name: ...
image: wordpress:cli
working_dir: /var/www/html
depends_on:
- db
- wordpress
networks:
- A_default
I get:
ERROR: Service "wpcli" uses an undefined network "A_default"
although A_default exists in the output of docker network ls.
If I remove the part:
networks:
- A_default
I get this error:
wpcli_1 | Error: This does not seem to be a WordPress installation.
wpcli_1 | Pass --path=path/to/wordpress or run wp core download.
A_wpcli_1 exited with code 1
and only this error in the docker-compose up output.
If I use
volumes:
- /var/www/html
inside the wpcli section of my docker-compose.yml I get the same error.
docker-compose.yml
Below is my docker-compose.yml with sensible information replaced with ...:
version: '3.3'
services:
db:
container_name: A_db_1
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: "no"
environment:
MYSQL_DATABASE: '...'
MYSQL_USER: '...'
MYSQL_PASSWORD: '...'
MYSQL_ROOT_PASSWORD: ...
phpmyadmin:
container_name: A_phpmyadmin_1
depends_on:
- db
restart: "no"
ports:
- "8080:80"
image: phpmyadmin/phpmyadmin
environment:
PMA_HOST: db:3306
PMA_USER: root
PMA_PORT: 3306
PMA_PASSWORD: ...
wordpress:
container_name: A_wordpress_1
depends_on:
- db
image: wordpress:latest
ports:
- "80:80"
volumes:
- type: bind
source: ./html
target: /var/www/html
volume:
nocopy: true
restart: "no"
environment:
WORDPRESS_DB_NAME: '...'
WORDPRESS_DB_USER: '...'
WORDPRESS_DB_PASSWORD: '...'
WORDPRESS_DB_HOST: db:3306
WORDPRESS_TABLE_PREFIX: 'wp_'
WORDPRESS_AUTH_KEY: '...'
WORDPRESS_SECURE_AUTH_KEY: '...'
WORDPRESS_LOGGED_IN_KEY: '...'
WORDPRESS_NONCE_KEY: '...'
WORDPRESS_AUTH_SALT: '...'
WORDPRESS_SECURE_AUTH_SALT: '...'
WORDPRESS_LOGGED_IN_SALT: '...'
WORDPRESS_NONCE_SALT: '...'
wpcli:
container_name: A_wpcli_1
image: wordpress:cli
working_dir: /var/www/html
depends_on:
- db
- wordpress
networks:
- A_default
volumes:
db_data:
Thank you.
Update 1
I have seen this file but I do not find it helpful in my situation.
Update 2
I wish to use a separate container just for WP-CLI because the command
docker run -it --rm \
--volumes-from A_wordpress_1 \
--network A_default wordpress:cli \
$*
does not remove the container after it has done its job, and removing --rm makes me unable to work with the files it has created because I cannot access the volumes of A_wordpress_1 because the temporary WP-CLI container has a different working directory.
I see two problems in your docker-compose file:
(removed) networks section was malformed
./html not mounted in wpcli service
I would try the following:
version: '3.3'
services:
db:
...
phpmyadmin:
...
wordpress:
...
wpcli:
...
volumes:
- type: bind
source: ./html
target: /var/www/html
volume:
nocopy: true
...
volumes:
db_data:
networks:
A_default:
Just starting in docker here
So I got this in my 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_NAME: wordpress
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_TABLE_PREFIX: "wp_"
WORDPRESS_DEBUG: 1
wordpress-cli:
depends_on:
- db
- wordpress
image: wordpress:cli
command: wp core install --path="/var/www/html" --url=localhost --title="Local Wordpress By Docker" --admin_user=admin --admin_password=secret --admin_email=foo#bar.com
volumes:
db_data:
So I wanted to run the wp core install so that I won't have to go through the process of manually setting up my test wordpress site.
However when I run docker-compose up, this does not seem to work, I got this error on the console
What am I missing here? Anyone can help me accomplish my goal of automating the of setting up wordpress install?
Thanks in advance
Well there are a couple of problems. The first one is that those two containers (wordpress and wordpress-cli) don't share a volume. So while wordpress has a wordpress installation ready, the wordpress-cli doesn't.
So you can add volumes to both containers, and then wordpress-cli will find the wordpress installation.
Then there's a second problem: the wordpress:latest and wordpress:cli images both run with the user www-data, but the problem is that the individual www-data users have different user-id's:
$ docker run --rm wordpress:latest grep www-data /etc/passwd
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
$ docker run --rm wordpress:cli grep www-data /etc/passwd
www-data:x:82:82:Linux User,,,:/home/www-data:/bin/false
It seems they aren't exactly compatible here. So if you use a shared volume you have to make sure they both use the same user-id. I solved this by having the wordpress:cli run with the user xfs which also has the user id 33.
The last problem is that your containers have dependencies on each other. Wordpress needs a running MySQL instance and the wordpress-cli needs also the MySQL and the Wordpress to be ready. To make sure MySQL is ready for the wordpress cli installation you either use something like "wait-for-it" or in a simple case you can just wait a couple of seconds and then try it.
I have tested all those changes and came up with the following docker-compose.yml. I have annotated all the changes I've made with "vstm":
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_NAME: wordpress
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_TABLE_PREFIX: "wp_"
WORDPRESS_DEBUG: 1
# vstm: add shared volume
volumes:
- wp_data:/var/www/html
wordpress-cli:
depends_on:
- db
- wordpress
image: wordpress:cli
# vstm: This is required to run wordpress-cli with the same
# user-id as wordpress. This way there are no permission problems
# when running the cli
user: xfs
# vstm: The sleep 10 is required so that the command is run after
# mysql is initialized. Depending on your machine this might take
# longer or it can go faster.
command: >
/bin/sh -c '
sleep 10;
wp core install --path="/var/www/html" --url="http://localhost:8000" --title="Local Wordpress By Docker" --admin_user=admin --admin_password=secret --admin_email=foo#bar.com
'
# vstm: add shared volume
volumes:
- wp_data:/var/www/html
volumes:
db_data:
# vstm: add shared volume
wp_data:
It uses a docker-volume but you can also map it to a filesystem. Depends on how you plan to use your docker-compose.
This one worked for me:
wpcli:
depends_on:
- mysql
- wordpress
image: wordpress:cli
links:
- mysql:db
entrypoint: wp
command: "--info"
container_name: ${COMPOSE_PROJECT_NAME}_wpcli
volumes:
- ${WORDPRESS_DATA_DIR:-./wordpress}:/var/www/html
working_dir: /var/www/html
Note that in the line:
links:
- mysql:db
mysql = name of my service
db = alias name I gave it, can be anything
Then you issue run wp like so:
docker-compose run --rm wpcli WORDPRESS_COMMAND
Source: https://medium.com/#tatemz/using-wp-cli-with-docker-21b0ab9fab79
this's my first answer at Stack Overflow :">
Actually, your question inspired me, and #vstm's answer guided me a bit.
You could try my piece of code:
1.wait-for-mysql.sh
#!/bin/bash -e
HOST=$(echo $WORDPRESS_DB_HOST | cut -d: -f1)
PORT=$(echo $WORDPRESS_DB_HOST | cut -d: -f2)
CMD=$#
until mysql -h $HOST -P $PORT -D $WORDPRESS_DB_NAME -u $WORDPRESS_DB_USER -p$WORDPRESS_DB_PASSWORD -e '\q'; do
>&2 echo "Mysql is unavailable - sleeping..."
sleep 2
done
>&2 echo "Mysql is up - executing command"
exec $CMD
2.compose.yml
version: '3.9'
services:
wordpress:
image: wordpress:5.7.0-php7.4-apache
ports:
- "80:80"
volumes:
- ./wp-data/:/var/www/html/
networks:
wp-net: {}
wp-cli:
image: wordpress:cli-2.4.0-php7.4
depends_on:
- wordpress
volumes:
- ./wait-for-mysql.sh:/wait-for-mysql.sh
- ./wp-data:/var/www/html/ # shared with wordpress service
user: "33"
command: >
/wait-for-mysql.sh
wp core install
--path="/var/www/html"
--url="http://your-url-here"
--title=your-title-here
--admin_user=your-user-here
--admin_password=your-password-here
--admin_email=your-email-here}
--skip-email
networks:
wp-net: {}
networks:
wp-net: {}
For your reference:
https://docs.docker.com/compose/startup-order/
https://gitlab.com/hino-hatake/wordpress
I am using Wordpress and docker container. The problem is that I updated the wp-config.php file but everything looks the same.
I have something like this:
CONTAINER ID IMAGE NAMES
b2711d4b72a1 phpmyadmin/phpmyadmin website_phpmyadmin_1
8a89ee46d673 wordpress:4.7.5 website_wordpress_1
2a167667f705 mysql:5.7 website_db_1
My docker-compose.yaml looks like this:
version: '2'
services:
wordpress:
depends_on:
- db
image: wordpress:4.7.5
restart: always
volumes:
- ./wp-content:/var/www/html/wp-content
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: p4ssw0rd!
ports:
- 80:80
- 443:443
networks:
- back
db:
image: mysql:5.7
restart: always
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: p4ssw0rd!
networks:
- back
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 8080:80
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: p4ssw0rd!
networks:
- back
networks:
back:
volumes:
db_data:
I have run docker-compose up, build and down but everything is the same.
The official WordPress docker image will automatically configure wp-config.php using the environment variables you set [documentation].
If there are any variables such as WORDPRESS_DB_HOST, WORDPRESS_DB_PASSWORD, etc., they will be used to build a wp-config.php file upon container creation.
If you want to provide a custom wp-config.php file, you need to make sure there are no related environment variables, and create a volume mapping for your modified file, like so:
version: '2'
...
volumes:
- ./wp-content:/var/www/html/wp-content
- ./wp-config.php:/var/www/html/wp-config.php
...
On docker-compose up, Docker will load your custom wp-config.php into the container and then run the WordPress image's docker-entrypoint.sh which updates the file with the values set in your environment variables.
You can make use of the WORDPRESS_CONFIG_EXTRA environment variable to define any other config values in the wp-config.php file.
As an example:
WORDPRESS_CONFIG_EXTRA: |
define('WP_ALLOW_MULTISITE', true );
define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', false);
The trick:
wp:
image: wordpress:latest
ports:
- 80:80
volumes:
- ./wp-content:/var/www/html/wp-content
- ./wp-config.php:/usr/src/wordpress/wp-config-sample.php
When local wp-config.php changes you should delete /var/www/html/wp-config.php from container, docker will copy it again... but it works!
I'm try to setup a docker workspace with Alpine, PHP, Apache, MySQL and Composer.
Currently I'm trying to use the following images:
PHP, Alpine and Composer: https://hub.docker.com/r/petehouston/docker-alpine-php-composer/
Wordpress: https://hub.docker.com/_/wordpress/
I created a docker-compose.yml file to manage this dependencies for me.
docker-compose.yml
version: '2'
services:
db:
image: mysql:5.7
volumes:
- ./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
volumes:
- ./www:/var/www/html
links:
- db
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: wordpress
alpine:
image: petehouston/docker-alpine-php-composer:latest
links:
- wordpress
So my problem is I'm trying to use the composer of the alpine container to manager my Wordpress in the wordpress container but when i try to use the following command:
docker run --rm -v $(pwd):/www -w /wordpress/var/www/html composer/composer create-project roots/sage your-theme-name 8.5.0
nothing happens, and the alpine container doesn't stay up, after i run compose-docker.up he exits
You can't access to Wordpress container from Alpine container using -w /wordpress/var/www/html.
As far as I understand, image petehouston/docker-alpine-php-composer:latest provides PHP deployment environment. That means you should use this image instead of wordpress image.
Your compose file can be like
version: '2'
services:
db:
image: mysql:5.7
volumes:
- ./db:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
alpine:
image: petehouston/docker-alpine-php-composer:latest
volumes:
- ./www:/home
links:
- db
ports:
- "8000:80"
command: composer require phpunit/phpunit
restart: always
Plz give me feedback. I'm sorry because i can't comment to get more information from you. Should you still get errors, comment here. I'll recheck