Preface
Although the docker deployment environment is much more convenient than downloading the installation package yourself, it is very unfriendly to users to find the docker commands and mounting directories every time they start a container, and sometimes it is necessary to start multiple When it comes to containers, it is obviously very troublesome to start them one by one. So if the "startup script" can be written in advance, even if the virtual machine is restarted, the "startup script" will be executed when starting the container. Docker-compose solves this problem very well.
1. Introduction to Docker-compose
Personal understanding: Container orchestration tools may be most useful as scripts for starting containers for most people. It can start multiple containers at one time, and its automatic container restart strategy is also very useful.
2. Deployment steps
1. Create a new directory docker-compose in the /root directory, and create a new mysql file in the /root/docker-compose directory.
cd /root
mkdir docker-compose && cd docker-compose
mkdir mysql && cd mysql
2 Prepare to mount files
(If you do not need to modify the configuration file, you can skip this step, and then delete the mounting of the configuration file when the docker-compose.yml file is mounted)
Create a new config folder under the /root/docker-compose/mysql folder and a new my.cnf file under the conf file.
mkdir config && cd config
vim my.cnf
Copy the following configuration file to my.cnf
[mysqld]
user=mysql
default-storage-engine=INNODB
character-set-server=utf8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
3. Write the docker-compose.yml file
Create a new docker-compose.yml file in the /root/docker-compose/mysql folder,
cd /root/docker-compose/mysql
vim docker-compose.yml
Add the following content and copy it to docker-compose.yml
version: '3'
services:
mysql:
image: mysql
restart: always
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: password
TZ: Asia/Shanghai
ports:
- 3306:3306
volumes:
- /root/docker-compose/mysql/data:/var/lib/mysql
- /root/docker-compose/mysql/config/my.cnf:/etc/mysql/my.cnf
command:
--max_connections=1000
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--default-authentication-plugin=mysql_native_password
Note that line 8 of docker-compose.yml sets the password for mysql root account login. Just change the password here to the password you need.
4. Start
docker-compose up -d
Summarize
Even if the host machine is restarted later, you only need to go to the location of the docker-compose.yml file and execute the docker-compose up -d command to start the container. In fact, if the restart policy of the container is configured in the docker-compose.yml file, it is always, then the related containers will be restarted every time docker is restarted, that is, the mysql container will be automatically restarted without executing the docker-compose up -d command.