Wordpress development in the local environment using Docker
Bài đăng này đã không được cập nhật trong 3 năm
The development environment is a trouble when developing Wordpress. With a staging environment, you can work without messing up the production environment , but it is difficult to collaborate between the two sides. It will be extremely convenient if it can be developed by a group of people and be possible to developed in the local environment.
If you prepare a development environment with Docker, the first setting is kind of complicated but you can prepare the development environment with one command. The following is a simple setting method of Docker and a method of Wordpress's local development environment.
How to install Docker
You can download Docker from its public site: https://docs.docker.com/docker-for-mac/install/#download-docker-for-mac
Docker terms and commonly used commands Overview
Image
"Image" is a file prepared by Docker. By downloading images you can build mySQL, Wordpress, etc. in local environment. Following is a commonly used command.
Build an image. A file linked to Dockerfile is specified.
$ docker build [-t {Image name} [:{Tag name}]]{Directory that contains Dockerfile}
Download Image
$ docker pull {Image name}
Delete Image
$ docker rmi ImageID
Container
"Container" is an application that operates in the local environment constructed based on the image.
Container status display
$ docker ps
Delete Container
$ docker rm ContainerID
Enter the container
$ docker exec -it ContainerName bash
Volume
"Volume" is data of sites used in containers and data such as DB information.
Display Volume content
$ docker volume ls
Remove all volume
$ docker volume rm $(docker volume ls -qf dangling=true)
Delete broken link
$ docker volume ls -qf dangling=true | xargs -r docker volume rm
Compose file
"Compass file" installs the image with the contents set in docker-compose.yml and then builds the container and the volume.
Docker images and containers are rarely used singly, it takes time to start up and link with each command one by one. Prepare a compass file to automate configuration.
Create containers. Run in background with -d
$ docker-compose up (-d)
Re-execute and update container
$ docker-compose build
Stop / delete containers
$ docker-compose down
How to implement Wordpress with Docker
-
Move to the working directory:
$cd /test
-
Prepare the
docker-compose.yml
version: "2"
services:
wordpress:
image: wordpress:latest
ports:
- 80:80
depends_on:
- db
environment:
WORDPRESS_DB_HOST: db:3306
env:
WORDPRESS_DB_NAME: docker_db
WORDPRESS_DB_USER: docker_user
WORDPRESS_DB_PASSWORD: dockerpass
db:
image: mysql:latest
ports:
- 3306:3306
env:
MYSQL_RANDOM_ROOT_PASSWORD: yes
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: docker_db
MYSQL_USER: docker_user
MYSQL_PASSWORD: dockerpass
volumes:
- db-data:/var/lib/mysql
volumes:
db-data:
driver: local
-
Run Command
$ docker-compose up (-d)
Locally accessible URLs are displayed with the following command.$ docker ps
Example: 0.0.0.0:80
Save SQL data in mySQL container
Execute the following command.
Command
$ docker exec -it Containername sh -c 'mysqldump DBname -u Username -pPASSWORD 2> /dev/null' > SaveDestination Filename
Example command
$docker exec -it 2275b287c1cf sh -c 'mysqldump docker_db -u docker_user -pdockerpass 2> /dev/null' > db.sql
#Wordpress theme plug-in · Load images and DB from the beginning.
Load themes
Volume of Wordpress of docker-compose.yml like below
volumes:
#Default themes
- ./sitedata/themes:/var/www/html/wp-content/themes/
- ./sitedata/plugins:/var/www/html/wp-content/plugins/
- ./sitedata/uploads:/var/www/html/wp-content/uploads/
Volume of mySQL of docker-compose.yml like below
volumes:
- db-data:/var/lib/mysql
#Default DB
- ./sql-data/db.sql:/docker-entrypoint-initdb.d/install_wordpress.sql
Create DB using for develop environment
-
Start Wordpress, MySQL with Docker-compose
-
Import the SQL data you want to load in mySQL, except the tables below DB table not to be imported
- Wp_options: Information on the site is stored
- Wp_usermeta: User information setting is stored
- Wp_users: User information is stored
-
Change the path after importing SQL command example
UPDATE wp_posts SET guid=REPLACE(guid,'http://pscreator.com','http://0.0.0.0/');
-
Adjust settings in Wordpress administration screen Setting contents → Custom Field, Permalink,
-
Export Wordpress DB
-
Set the volume to import the exported data as initial data in Docker-compose Volume of mySQL of docker-compose.yml like below
Volumes:
- ./sql-data/db.sql:/docker-entrypoint-initdb.d/install_wordpress.sql
- When rebooting, the same Wordpress as the origin one will be built
Sample file
The following is a sample file that can be linked with Wordpress theme and DB https://github.com/psc-kamei/dev_wp_docker
All rights reserved