A WordPress development environment using docker
A WordPress development environment, using docker (via docker-compose), complete with WordPress, PHP FPM, MariaDB, Nginx, composer, WP-CLI, npm and git services.
mkdir my-wordpress-project
cd my-wordpress-project
git clone https://github.com/GrottoPress/wordpress-dev.git ../wp-dev
src/
files into your new project’s directory: cp -rf ../wp-dev/src/. .
sample.env
file to .env
: cp sample.env .env
.env
file: chmod 0600 .env
.env
file to tastedocker-compose up -d
to start the serviceshttp://<NGX_HOST>:<NGX_PORT>
(use the host and port you set in .env
) in your browser to complete WordPress’ installationThe following variables are available for use in your .env
file:
Variable | Description |
---|---|
DB_ROOT_PASS |
Database root user password |
DB_NAME |
Database name |
DB_USER |
Database user |
DB_PASS |
Database user password |
NGX_HOST |
Hostname of virtual host |
NGX_PORT |
The port on the host machine to bind the nginx container’s port to. Determines the port to access the WordPress installation on the host machine |
PHP_VERSION |
Format: <major>.<minor> . |
WP_VERSION |
Format: <major>.<minor> . |
WP_DEBUG |
|
WP_TABLE_PREFIX |
|
WP_AUTH_KEY |
|
WP_LOGGED_IN_KEY |
|
WP_AUTH_SALT |
|
WP_LOGGED_IN_SALT |
|
WORK_DIR |
Directory of the particular plugin or theme you are working on. Sets the context for composer, npm and git commands. Eg: wp-content/plugins/my-plugin |
The wp-content
directory of the WordPress installation corresponds to the the app/
directory of your project.
Therefore, if developing a theme, it goes into the app/themes/
directory. Plugins go into the app/plugins/
directory.
You should have set the WORK_DIR
environment variable to the directory of the theme or plugin you are working on.
This way, you do not need to cd
into that directory to issue git
, npm
or composer
commands.
Copy and paste the following into your ~/.bashrc
file.
# BEGIN: Docker/compose
#
docker_compose_git()
{
if [ -f "${PWD}/docker-compose.yml" ]; then
docker-compose run --rm git "$@"
else
$(which git) "$@"
fi
}
docker_compose_composer()
{
if [ -f "${PWD}/docker-compose.yml" ]; then
docker-compose run --rm composer "$@"
else
$(which composer) "$@"
fi
}
docker_compose_wp()
{
if [ -f "${PWD}/docker-compose.yml" ]; then
docker-compose run --rm wp-cli wp "$@"
else
$(which wp) "$@"
fi
}
docker_compose_npm()
{
if [ -f "${PWD}/docker-compose.yml" ]; then
docker-compose run --rm node npm "$@"
else
$(which npm) "$@"
fi
}
alias wp="docker_compose_wp"
alias composer="docker_compose_composer"
alias npm="docker_compose_npm"
alias git="docker_compose_git"
#
# END: Docker/Compose
Run source ~/.bashrc
or restart terminal, to load the new configuration.
You may then use wp
, composer
, npm
and git
commands as you would normally.
docker-compose up -d
docker-compose down
docker-compose logs <service>
. Eg: docker-compose logs nginx
docker-compose exec <service> /bin/sh
. Eg: docker-compose exec nginx /bin/sh
Currently, the entire WordPress installation is persisted in a named volume (app
), to ensure the other services (eg: nginx
) have access to them. (This is not ideal; any ideas are welcome.)
Therefore, if you change the WordPress version, you need to remove the corresponding volume (wordpress_app
) with the command docker volume rm wordpress_app
, before running docker-compose up -d
again.
Remember to stop containers, with docker-compose down
, before removing volume.