What is docker compose and how to setup a wordpress website with docker compose
Docker Compose is a tools for defining and running multiple container Docker application, you may spin up one or more container by using the configuration YAML file.
Docker compose work in all environment including production, staging, development, testing as well as CI workflows.
The latest version of Docker Container come with the Compose where you no need to install the Compose tools separately. If your yet to install the docker container in your machine, you may just following the blog post on how to installing the Docker Container in Ubuntu 16.04
By using the Docker Compose, it’s basically consist of the following three-step process:
1. Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment
3. Run docker-compose up and compose starts and runs your entire app.
Following are the few simple step to compose your first wordpress site with MySQL ready
1. Create an empty project folder
You may give the folder any name as you like as long as you can easily remember, the folder should not contain anything but only your docker-compose.yml file(docker-compose.yaml, but yml and yaml extension both working fine). I will name my folder as my_wordpress.
$ sudo mkdir my_wordpress
2. Change the current directory to the project directory
$ cd ./my_wordpress
3. Create a docker-compose.yml file that starts your WordPress blog and a separate MySQL instance with a volume mount for data persistence:
You may create the compose file with any of your favourite text editor, I’m using vim as my primary text editor on my machine
$ sudo vim docker-compose.yml
Copy and paste the following yml syntax into your newly created file
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_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress volumes: db_data:
4. Build the project
Once the above yml file save, now you can start to build your project by using the code
$ docker-compose up -d
By running the code above, it’s actually running at the deamon mode and the docker actually will check on your local images repository to see is the image exists, if not, they will go to the docker hub and pull the needed image. After the pulling process done, it will auto start the MySQL as well as wordpress by showing the following log
Creating network "mywordpress_default" with the default driver Creating volume "mywordpress_db_data" with default driver Pulling db (mysql:5.7)... 5.7: Pulling from library/mysql 2a72cbf407d6: Already exists … 94202acee04b: Pull complete Digest: sha256:e7b486e5548a3f1ef98c6571a44a0e8371a449a4b45e6f7f0e765842c10560f6 Status: Downloaded newer image for mysql:5.7 Pulling wordpress (wordpress:latest)... latest: Pulling from library/wordpress 2a72cbf407d6: Already exists 273cd543cb15: Pull complete ec5ac8875de7: Pull complete 9106e19b56c1: Pull complete … eb7e47a83064: Pull complete Digest: sha256:759200aa27e2e61ec7120cc108fee8b5b1db15af1a126d22491b07d791fbdb2f Status: Downloaded newer image for wordpress:latest Creating mywordpress_db_1 ... done Creating mywordpress_wordpress_1 ... done
Step 5: Browse the website
At this point of time, your wordpress website should be up and running at the port 8000 (this port we specific early in the docker compose file). In order to view your wordpress site, just open up your browser and enter the address of http://localhost:8000 in the address bar.
Step 6: Shutdown and cleanup your docker container
allow you to removes the containers and default network, but preserves your WordPress database.
docker-compose down --volumes
will removes the containers and default network and also volumes
Hopefully by following the above simple step will let’s you know how’s the docker-compose work and able to kick start by build your own container for your purpose, and also please do share with me if you have any concern or idea to make this better, so that we can learn the docker container together.