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'</p> <p>services:<br /> db:<br /> image: mysql:5.7<br /> volumes:<br /> - db_data:/var/lib/mysql<br /> restart: always<br /> environment:<br /> MYSQL_ROOT_PASSWORD: somewordpress<br /> MYSQL_DATABASE: wordpress<br /> MYSQL_USER: wordpress<br /> MYSQL_PASSWORD: wordpress</p> <p> wordpress:<br /> depends_on:<br /> - db<br /> image: wordpress:latest<br /> ports:<br /> - "8000:80"<br /> restart: always<br /> environment:<br /> WORDPRESS_DB_HOST: db:3306<br /> WORDPRESS_DB_USER: wordpress<br /> WORDPRESS_DB_PASSWORD: wordpress<br /> volumes:<br /> 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<br /> Creating volume "mywordpress_db_data" with default driver<br /> Pulling db (mysql:5.7)...<br /> 5.7: Pulling from library/mysql<br /> 2a72cbf407d6: Already exists<br /> …<br /> 94202acee04b: Pull complete<br /> Digest: sha256:e7b486e5548a3f1ef98c6571a44a0e8371a449a4b45e6f7f0e765842c10560f6<br /> Status: Downloaded newer image for mysql:5.7<br /> Pulling wordpress (wordpress:latest)...<br /> latest: Pulling from library/wordpress<br /> 2a72cbf407d6: Already exists<br /> 273cd543cb15: Pull complete<br /> ec5ac8875de7: Pull complete<br /> 9106e19b56c1: Pull complete<br /> …<br /> eb7e47a83064: Pull complete<br /> Digest: sha256:759200aa27e2e61ec7120cc108fee8b5b1db15af1a126d22491b07d791fbdb2f<br /> Status: Downloaded newer image for wordpress:latest<br /> Creating mywordpress_db_1 ... done<br /> 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.
- Click to share on Twitter (Opens in new window)
- Click to share on Facebook (Opens in new window)
- Click to share on Tumblr (Opens in new window)
- Click to share on Google+ (Opens in new window)
- Click to share on Pinterest (Opens in new window)
- Click to share on Pocket (Opens in new window)
- Click to share on WhatsApp (Opens in new window)