This is the second day of my participation in the August More text Challenge. For details, see:August is more challenging

The installation

Install Docker, set boot, and then configure Ali Cloud image acceleration

1. Install the Docker

Docker official installation

Mysql > install Docker; mysql > install Docker;

Sh sudo sh get-docker.sh --mirror Aliyun # start docker sudo systemctl enable Docker sudo systemctl start dockerCopy the code

2. Ali Cloud image acceleration

Attention! Registry-mirrors need to be replaced with your own Ali Cloud mirror accelerator address. Click the address to obtain it

sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": EOF sudo systemctl daemon-reload sudo systemctl restart dockerCopy the code

Docker CLI

The CLI stands for Command Line Interface. For details, see Docker Reference Documentation

Here are a few scenarios to put Docker to use first

Scenario 0: Tomcat

Enter docker and press Enter, docker commands will be listed, easy to query docker # query MySQL image docker search tomcatCopy the code

Compared to direct search, using search Docker Hub is more convenient

Search the Tomcat

You can view the related image introduction, help, and historical version: Tomcat

To run the image, follow the instructions in the document “How to use this image”

Docker pull tomcat:8.0-jre8 Docker run tomcat:8.0-jre8 # Docker run -d tomcat:8.0-jre8 Docker ps # 查看 tomcat log, docker ps # 查看 tomcat log Docker top clever_swanson # into containers # I and T The bash is using shell Docker exec-it clever_swanson bash # Inside the container you can use the Linux command # exit the container, Docker stop clever_swanson # Docker stop swanson # Docker ps -a # stop docker ps -a # stop docker ps -a # stop docker start clever_swanson # Docker rm -f clever_swanson docker rm -f clever_swanson # Docker rmi -f tomcat:8.0-jre8 Docker rmi -f tomcat:8.0-jre8Copy the code

Scenario 1: Tomcat

You need to map the network ports of the container to the host to enable external access to the internal services of the container. For convenience, you also need to mount the Data directory and configuration directory of Tomcat to the host for direct editing

Refer to the official document: Tomcat

# pull # -p to map the network port in the Tomcat # run container to the host (8080:8080) Docker run -p 8080:8080 -d --name mytomcat tomcat:8.0-jre8 Ps,exec,top,stop # this container has occupied port 8080 of the host, so that later Tomcat can bind to port 8080 of the host. So stop docker stop mytomcat # map the data in the container to the host by using the data volume / usr/local/tomcat/webapps # configuration file: /usr/local/tomcat/conf docker run -p 8080:8080 -v apps:/usr/local/tomcat/webapps -v confs:/usr/local/tomcat/conf -d - the name mytomcat2 tomcat: 8.0 jre8Copy the code

Tomcat has been launched, this time by http://ip hosting: 8080 to access the Tomcat default home page, I visit the address http://192.168.43.166:8080, for example, saw Tom cat logo was a success. If the access fails, port 8080 May not be enabled. For details about how to enable port on CentOS7, see enable port on CentOS7

Scenario 2: MySQL

Refer to the official documentation: MySQL

MySQL > select * from 'root' where 'root' = 'root'; 1234 docker run --name mysql -e MYSQL_ROOT_PASSWORD=1234 -p 3306:3306 -d mysql:5.7.32 Docker stop mysql # Docker stop mysql # Docker stop mysql # Docker stop mysql # Docker run --name mysql2 -e MYSQL_ROOT_PASSWORD=1234 -p 3306:3306 -d -v mysqldata:/var/lib/mysql -v Mysqlconfig: / etc/mysql mysql: 5.7.32Copy the code

You can test the database using Navicat or another tool. The host IP address is root, the password is 1234, and you can try to store data. The data will be stored in the data volume named mysqlData

Docker inspect mysqldata # docker inspect mysqldata # docker inspect mysqldata # File corresponding to the mount position # I here mount address is: / var/lib/docker/volumes/mysqldata / _data while forming # into this directory, you can see the file storageCopy the code

The advantage of using data volumes is that when the container is removed, you can run a new container and directly mount the original data volumes. Data is not lost

Docker rm -f mysql2 # Docker run --name mysql2 -e MYSQL_ROOT_PASSWORD=1234 -p 3306:3306 -d -v mysqldata:/var/lib/mysql -v Docker run --name mysql2 -e MYSQL_ROOT_PASSWORD=1234 -p 3306:3306 -d -v mysqldata:/var/lib/mysql -v Mysqlconfig: / etc/mysql mysql: 5.7.32Copy the code

Redis

Refer to the official document: Redis

Add redis-server –appendonly yes after the image name Redis :5.0.10 to override the default command

Docker run --name redis -p 6379:6379 -d redis:5.0.10 Docker stop redis # enable persistent redis-server --appendonly yes Docker run --name redis2 -p 6379:6379 -d -v redisdata:/data reis :5.0.10 redis-server docker run --name redis2 -p 6379:6379 -d -v redisdata:/data reis :5.0.10 redis-server docker run --name redis2 -p 6379:6379 -d -v redisdata:/data reis :5.0.10 redis-server # use tools such as Redis Desktop Manager to test the host IP connectionCopy the code

Clean up the container

Docker ps # a: docker ps # a: docker ps # a: Docker ps # Docker ps --help # docker ps --help # Docker rm -f $(docker ps -aq) # docker rm -f $(docker ps -aq) # Be careful with the Docker Volume Prune for important dataCopy the code

docker

We can find the location of Docker by using the following command

Whereis docker # I here is the result of the execution returns: docker: / usr/bin/docker/etc/docker/usr/libexec/docker/usr/share/man/man1 / docker. 1. GzCopy the code

As you can see, the docker executable is located in /usr/bin, which is stored in the environment variable PATH, so we can use the docker command in any PATH

Docker is C/S architecture mode (client-server), so Docker above is actually Docker client, Docker server is Docker Deamon corresponds to Dockerd, which is also in this directory. Deamon is the Docker engine, and Docker clients communicate with Deamon through the Docker API

Docker is an executable program that contains many commands, input

docker --help
Copy the code

It displays Usage, options, Commands, and Management Commands. S is also Commands. Each command may have its own subcommands, options

The docker run --help # volume command has many subcommands, such as ls, rm, inspect docker volume --helpCopy the code