One of the biggest challenges in web development is to have a stable development environment and assuring that the website works when deploying on different servers.
Docker helps us build isolated containers that give a this stable and predictable environment and saves many headaches when debugging our website. Thanks to the preconfigured images available on Docker Hub and the Docker compose it's a breeze to set up a development environment.
This post assumes that you already have Docker and Docker compose isntalled on your system. On Linux Docker Compose doesn't come installed with Docker so make sure you install it separately.
Create a directory to put the docker compose file into and then paste the following into the docker-compose file:
version: '3.3' services: db: image: mysql:5.7 # declaring the volumes so we have persistent data volumes: - wp_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: password MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress wordpress: container_name: docker_wordpress_dev depends_on: - db # using the latest wordpress image # Docker will automatically look it up on Docker Hub image: wordpress:latest # binding the loopback port 8000 on the host to port 80 on the docker container ports: - "127.0.0.1:8000:80" restart: always environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress WORDPRESS_DB_NAME: wordpress # we need to declare the volumes that we will use volumes: wp_data:
After running the "docker-compose up -d" command you should be able to access WordPress on by entering localhost:8000 in your browser. This way you should should be able to bring up the installation page of WordPress:
After completing the setup steps you're ready to start working with WordPress.
If you'd like to access the source code you can the following command, which will bring you to the main folder with your WordPress installation:
docker exec -it docker_wordpress_dev bash
The database volume persists after the container is stopped. You can check that running the command "docker-compose stop" to stop the containers. Whenever you reinitialize the container you will be able to pick up where you left, so whatever changes you mad to your WordPress installation in Docker will persist. Also the database will persist in the wp_data volume.
Thanks to the persisting database volume, even if you remove the container you should be able to spin the WordPress site back up from the same image.