We start with this section with a very important tool, Docker Compose, which is used frequently.
Let's start with a case study of why we use Docker Compose.
Case study: How do I deploy a WordPress via a container?
Now pull WordPress locally:
docker pull wordpress
docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE vincent/my-nginx latest e3e9c5946773 23 hours ago 109MB wordpress latest a541a1a59631 41 hours ago 447MB vincent/flask-redis latest 31b9d9c546b0 3 days ago 919MB vincent/ubuntu-base latest 68941b82b183 4 days ago 557MB ubuntu 16.04 13c9f1285025 2 weeks ago 119MB redis latest 3c41ce05add9 3 weeks ago 95MB python 2.7 37093962fbf5 3 weeks ago 914MB nginx latest 719cd2e3ed04 3 weeks ago 109MB mysql latest c7109f74d339 3 weeks ago 443MB hello-world latest fce289e99eb9 6 months ago 1.84kB
You already have mysql locally, so you don't have to pull it.
Create a mysql container:
We want to set a root password for mysql, how?You can see the mysql Introduction.Use -e MYSQL_ROOT_PASSWORD=123456, and create a database WordPress while starting the container, -e MYSQL_DATABASE=wordpress
Why is the -p parameter not applicable?Because our databases are not accessed by people outside, but by wordpress.So the command is as follows:
docker run -d --name mysql -v mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=wordpress mysql
docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fc99f5a686a1 mysql "docker-entrypoint.s..." 21 seconds ago Up 20 seconds 3306/tcp, 33060/tcp mysql
Version mysql8.0 will encounter the following issues:
The server requested authentication method unknown to the client mysqli_real_connect(): The server requested authentication method unknown to the client [caching_sha2_password] mysqli_real_connect(): (HY000/2054): The server requested authentication method unknown to the client
The solution is:
mysql> ALTER USER root IDENTIFIED WITH mysql_native_password BY 'PASSWORD'; Query OK, 0 rows affected (0.08 sec)
Restart wordpress.
Create a wordpress container:
Configuration parameters for wordpress can also be found in docker hub See above.The commands are as follows:
docker run -d -e WORDPRESS_DB_HOST=mysql:3306 -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=123456 --link mysql -p 8080:80 wordpress
View containers:
ocker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES aa6ae7b0795b wordpress "docker-entrypoint.s..." 4 seconds ago Up 3 seconds 0.0.0.0:8080->80/tcp jolly_zhukovsky fc99f5a686a1 mysql "docker-entrypoint.s..." 4 minutes ago Up 4 minutes 3306/tcp, 33060/tcp mysql
Then visit 127.0.0.1:8080 and the following page appears:
Indicates that the installation was successful.
We started an application of wordpress with two docker container s, similar to the process we used to link redis with Python flask.
Many modules and containers like this depend on each other, creating, modifying, deleting and other management operations are inconvenient.Hopefully this dependency will be managed together in a group.So docker Compose came into being