Docker is an open-source tool designed to make it easier, deploy, and run applications by using containers. It is a tool that is designed to benefit both developers and system administrators.
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application's services. It is great for development, testing, and staging environments. Using compose is simply a three steps process:
- Define your app's environment with a Dockerfile so it can be reproduced anywhere.
- Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
- Lastly, run docker-compose up and Compose will start and run your entire app.
Make sure that you have the latest version of Docker and Docker Compose installed on your machine. If it isn't installed and you don't know how to install it, click here. In this project:
- Build the image.
- Run MySQL.
- Run the image. I've picked up a content management system docker-compose.yml file will setup Wordpress and MySQL with a single command.
WordPress is a free and open-source content management system written in PHP and paired with a MySQL or MariaDB database. To run WordPress locally, you don't need Apache, MySQL server, XAMPP, Wamp and Mamp. All you need is Docker
mkdir project
cd project/
3. Create a docker-compose.yml file that will start your WordPress blog and a separate MySQL instance with a volume mount for data persistence:
vim docker-compose.yml
Or
vi docker-compose.yml
Or
gedit docker-compose.yml
Use whichever text-editor you're comfortable with! Then paste the content which is inside docker-compose.yml file I've attached here.
docker-compose up
Go to your web browser and run http://localhost:8080
.
To stop and remove all the containers use the down
command
docker-compose down
Use -v
if you also want to rmeove the database volume which is used to persist the database:
docker-compose down -v
dbos
- It's just a name here. One can use any other name also. Here we run the MySQL image from the Docker Hub in version 5.7 with the passwordqwerty
and other important information required to launch a MySQL container. To understand more, click here.wordpressos
- It's also a name here given for WordPress image. Here we build a Docker image for MySQL and map port 8080 on the host to port 80 inside the container. Then we give MySQL password, user, and database name via environment variable. Remember that you give the same password, username, and database name otherwise it'll give you and error.Why volumes??
- Since data in the containers is not persistent, i.e. if you stop the container and run it again, there will no longer be any data inside it, so to avoid this we add volume. Here add volume means that we make a separate volume for a particular container.volumes
- Don't give spaces betweenwp_storage_new:
and/var/www/html/
as it would lead to an error.