Docker installation and use
Docker Chinese Community
Docker 中文 版,docker help,docker manual,docker tutorial,docker installation manual – docker中文 版) ()
What is the docker
Docker is a container virtualization technology that solves the running environment and configuration problems of the software container, facilitating continuous integration and contributing to the overall release
Why docker
The environment
1. The virtual machine
Centos7.7, the vm installed by yourself is not ali cloud
2. View the system version command
lsb_release -a
Copy the code
Execute the following command if the command execution above prompts that the command was not found
yum install -y redhat-lsb
Copy the code
3. View and disable the firewall
systemctl status firewalld
Copy the code
systemctl stop firewalld
Copy the code
Install the docker
1. Install the tool package
sudo yum install -y yum-utils
Copy the code
Or perform
yum update
Copy the code
Wait for a long time
2. Configure the remote vault
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Copy the code
3. Install the docker
sudo yum install docker-ce
Copy the code
4. Start the docker
Start the
service docker start
Copy the code
stop
service docker stop
Copy the code
restart
service docker restart
Copy the code
Check the version
Docker version/docker - vCopy the code
Join open start
chkconfig docker on
Copy the code
5. A charm
docker run hello-world
Copy the code
6. Basic docker commands
View all running containers
docker ps
Copy the code
View all containers that are running, including those that failed to start
docker ps -a
Copy the code
Remove the container
Docker RM Container name/container IDCopy the code
If the container is running, you need to stop the container to delete it
Stop the container
Docker stop Container IDCopy the code
View all mirrors
docker images
Copy the code
Remove the mirror
Docker RmI IMAGE name /IMAGE IDCopy the code
Forcibly Deleting a Mirror
Docker image rm -f Image name/image IDCopy the code
Deleting all Mirrors
As shown below, I pulled three images
docker images
Copy the code
Get the IMAGE ids of all images
docker images -qa
Copy the code
Deleting all Mirrors
docker rmi $(docker images -qa)
Copy the code
Check again, it’s gone
docker images
Copy the code
Delete all containers
You can run the docker ps-qa command to query the ids of all containers, then run the docker stop (dockerps− QA) command to stop all containers, then run the dockerrm command to stop all containers, and then run the Docker rm command to stop all containers Run the dockerps−qa command to close all containers, and then run the dockerrm(docker ps-qa) command to delete all containers
The following example shows how to delete all mirrors, as shown in the figure below, and encounters the following problems
To resolve the problem, run the following command
Delete container ID The container ID (d652B36931F5 in the figure above) occupied by docker rm or forcibly delete the docker image rm -f Image name/image ID
Configure Ali Cloud accelerator
1. Log in to Aliyun Developer Platform
[Ali Cloud login – welcome to Ali Cloud, a secure and stable cloud computing service platform (aliyun.com)](cr.console.aliyun.com/cn-hangzhou…)
2. Set the password upon your first login
3. Obtain the acceleration address
https://********.mirror.aliyuncs.com
4. Select the DOCker OS according to personal needs, and modify the Docker configuration file according to installation requirements. I use centos here, and the steps are as follows
Create a daemon.json file
In the/etc/docker/directory file new daemon. Json, vim editor/etc/docker/daemon. Json file add the following content
{ "registry-mirrors": ["https://*******.mirror.aliyuncs.com"]}Copy the code
Save and exit, then restart the Docker
systemctl daemon-reloadsystemctl restart docker
Copy the code
Or run the following command once
sudo mkdir -p /etc/dockersudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://hmchwtyi.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
Copy the code
Check the startup status of docker
service docker status
Copy the code
In the future, it will be much faster to obtain the required image through the Docker pull command than to download it directly from the official website
Docker install tomcat
1. Obtain the Tomcat image
Search the mirror
docker search tomcat
Copy the code
Pull the mirror
docker pull tomcat
Copy the code
The speed will be much faster after the aliyun accelerator is configured
Specify version pull
Docker pull tomcat: 8.0.53Copy the code
[Docker Hub](hub.docker.com/) (super slow) search you need…
2. View all mirrors
docker images
Copy the code
REPOSITORY: represents the REPOSITORY source of the image
TAG: indicates the TAG of the mirror
IMAGE ID: indicates the ID of an IMAGE
CREATED: indicates the time when a mirror is CREATED
SIZE: indicates the mirror SIZE
3. Run the container
docker run --name tomcat -p 8080:8080 -v $PWD/test:/usr/local/tomcat/webapps/test -d tomcat
Copy the code
Parameter interpretation
A) -p 8080:8080: maps port 8080 of the container to port 8080 of the host
B) – v $PWD/test: / usr/local/tomcat/webapps/test: the host of the current directory in the test/test mounted to the container
Access test 192.168.0.192:8080
4. If there are two versions of Tomcat, see the following figure
At run time, you need to specify a TAG to run, and it is best to use an alias to distinguish them to avoid confusion
Docker run - name mytomcat (alias) - p (designated port 8081) : 8081-8080 v $PWD/test: / usr/local/tomcat/webapps/test - d Tomcat :8.0.53 (specified TAG)Copy the code
5. Deploy the WAR package
Into the container
docker exec -it NAME/ CONTAINER ID bash
Copy the code
Upload the WAR package to any directory
/home/liucong/tomcat
Run cp command
Then execute in the directory where the war package resides:
docker cp datamgt.war tomcat:/usr/local/tomcat/webapps Copy the code
Move the WAR package under WebApps in the Tomcat container
Go into the container to see if datamgt.war exists
Go into the container to see if datamgt.war exists
The WAR package shown above will automatically decompress, exit the container, and then execute the command to restart the container
docker restart tomcat
Copy the code
View container logs in the last 30 minutes
docker logs --since 30m tomcat
Copy the code
Browser access
http://192.168.0.192:8081/datamgt/treesoft/login
Docker mysql installation
1. Search for the version you want to pull from the official website
Mysql5.7.28 will be installed by default if no version is specified
docker pull
Docker pull mysql: 5.7.28Copy the code
Run the container
docker run -p 3306:3306 --name mysql -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/logs:/logs -v / home/mysql/data: / var/lib/mysql - e MYSQL_ROOT_PASSWORD = rootroot - d mysql: 5.7.28Copy the code
-p 3306:3306: Maps port 3306 of the container service to port 3306 of the host. The external host can directly access the MySQL service through host IP address 3306
-v: mounts the host directory to the container for data synchronization
MYSQL_ROOT_PASSWORD=rootroot: Set the password of user root of the mysql service
Check whether the installation is successful: Docker PS, as shown below, starts tomcat and mysql containers
Enter the mysql container and log in to mysql
sudo docker exec -it mysql /bin/bash
Copy the code
Enter the password set above
2. Use a tool to connect
Upload the local Docker image to Ali Cloud image warehouse
1. Log in to the Aliyun image repository
Ali Cloud login – Welcome to Ali Cloud, a secure and stable cloud computing service platform (aliyun.com)
Enter the Ali Cloud container image service console, and set the password for the first time, as shown in the following figure
Currently the control does not have any private images, as shown below
Create a mirror repository as shown in the following figure
Set the namespace, repository name, private or public, if it’s public then everyone can search for the image you uploaded
Run the following command to log in to the Aliyun image warehouse
docker login [email protected] registry.cn-hangzhou.aliyuncs.com
Copy the code
The password is the previous one. The login is successful, as shown in the following figure
2. View the image to be uploaded
docker images
Copy the code
3. Push the image we need to upload
As shown below, wait patiently (feel super slow, is the mirror too big? Bad Internet?)
Docker tag ef6a7c98d192 registry.cn-hangzhou.aliyuncs.com/liucong/tomcat:8.0.53Copy the code
Sudo docker push registry.cn-hangzhou.aliyuncs.com/liucong/tomcat:8.0.53Copy the code
4. Upload successfully, as shown below, ali Cloud image service console will exist
5. If the image is set to public, it will be searched by others. Otherwise, set it to private
6. Pull the image we just uploaded from another server and start the Tomcat service
Now use another server (192.168.0.191) to install the Docker, and then pull the image. Please refer to the steps above. No more mapping will be repeated here
First login Ali cloud mirror service
sudo docker login [email protected] registry.cn-hangzhou.aliyuncs.com
Copy the code
Then execute the pull command:
Sudo docker pull registry.cn-hangzhou.aliyuncs.com/liucong/tomcat:8.0.53Copy the code
The version number is consistent with that of Ali Cloud mirroring service
Look at mirror
docker images
Copy the code
Run the image
docker run --name tomcat -p 8080:8080 -v $PWD/test:/usr/local/tomcat/webapps/test -d ef6a7c98d192
Copy the code
Into the container
docker exec -it CONTAINER ID bash
Copy the code
access
http://192.168.0.191:8080/
Docker container data volume
1. What is the container data volume
When we use docker container, a series of data files will be generated. These data files will disappear when we close docker container, but part of the content generated in it we hope to be able to save it for another purpose. Docker will package the application and the running environment into a container and publish it. We want some of the data generated by the running process clock to be persistent, and we want to be able to share data between containers.
Generally speaking, docker container data volume can be regarded as a USB disk commonly used in our life. It exists in one or more containers and is mounted to the container by Docker, but it does not belong to the joint file system. Docker will not delete the mounted data volume when the container is deleted
2. What are its features
A) Data volumes can share or reuse data before containers
B) Changes in data volumes can take effect directly
C) Changes in the data volume are not included in the update of the mirror
D) The life cycle of a data volume continues until no container can use it
3. There are two ways to add a data volume
Mount by command (synchronizes even after the container exits)
-
First of all by
docker pull centos Copy the code
Pull a centos image, the following shows the way to mount by command
-
Execute the command
docker run -d -it -v /home/mycentosdata:/mycentos centos Copy the code
Docker run – d (background, otherwise after exit containers, container will stop) – it – v/hosting an absolute path: / container mirror directory name/image ID
This command creates two directories on the host and in the container. The two directories are connected and the data in the two directories can be shared
-
Check whether the mount is successful
Run ls -ll in the container to see that the mycentos folder has been generated
Looking at the host’s home directory, myCentosData is also generated
-
View with command
Docker inspect Container IDCopy the code
After this command is executed, a JSON string will be returned. Because it is too long to attach here, only part of the screenshot will be attached, as shown in the following figure, indicating that the mount is successful
-
Create a new file a.toxt in myCentosData, and mycentos in the container will be generated accordingly, as shown in the figure below
Docker exec it 9f55d0ebb0e4 /bin/bashCopy the code
Even after stopping the container, it will be shared, so I won’t do the demo here, but try it out
Add it as a Dockerfile
Note: Using Dockerfile will not generate the corresponding directory, but will also generate a default file, as shown below
-
In order to standardize, I created a docker folder in the root directory of the host and created a Dockerfile file, and added the following content
FROM centos VOLUME ["/mydockerfiledata"."/mydockerfiledata1"] -- privileged=true CMD echo "success build" CMD /bin/bash Copy the code
It is equivalent to running the docker run it -v/host directory path: / Directory path image name
-
Docker build and docker commit can be used to build docker images. Docker commit needs to be done inside the container,docker build does not need to be done.
Docker build -f '/docker/DockerFile' -t liucong/centosCopy the code
docker build -f '/docker/DockerFile' -t liucong/centos . Copy the code
The following figure indicates that the compilation is successful
docker images Copy the code
Command, as shown in the following figure, there are two centos images
-
Run liucong/centos mirroring: Mount the /home/mycentosdata directory to the two newly generated directories by using commands
docker run -d -it -v /home/mycentosdata:/mydockerfiledata liucong/centos Copy the code
Use Dockerfile to mount the default folder on the host, as shown in the following figure
use
Docker inspect 978edCF8F273Copy the code
View mounting information
The following figure shows all mounted folders Host machine and the corresponding container mounted directory, last step I not manual will be hosting/home/mycentosdata mounted to the inside of the container/mydockerfiledata directory, so below corresponding relationship of the first show is so, but we use Dockerfile mydocker The fileData1 directory does not use a command to specify a mount directory, so one is assigned by default
-
4. Read and write permissions
docker run -d -it -v /home/centosdata:/centosdata:ro centos
Copy the code
If ro is not included in the command, the file in the container can be read and written
Ro indicates that the files in the container are read-only, as shown in the following figure
Docker inspect FAe237C35C06Copy the code
First create a new a.txt variable in the host directory to write content, and then enter the container to see if it can be edited, as shown below
Readonly files
Docker data volume container
The so-called data volume container, in plain English, is the active hard disk hanging on the active hard disk, the realization of data transfer dependence
-
The following mirror liucong/centos created in the previous step is a template and runs containers LC01 / LC02 / LC03
-
In order to distinguish between the two, I have deleted all the files that were previously mounted in the host and container. I will create a new mount directory lc01 and create a new file lc01.txt in the mount directory
docker run -d -it --name lc01 -v /home/docker/lc01:/lc01 liucong/centos Copy the code
TXT file is created in /home/docker-lc01 directory of the host computer. The content of the file is create LC01
As shown below, the data has been synchronized to the container
-
The above new LC01 container, so how to achieve the dependency of data transfer, see the following steps
Then create lc02 container and inherit LC01 to implement data transfer. If successful, then lc01. TXT created in mount directory will also appear in lc02 container
docker run -d -it --name lc02 --volumes-from lc01 liucong/centos Copy the code
As shown in the following figure, LC02 is created successfully. Now let’s enter lc02 container to check whether lc01.txt is stored
If we create lc02.txt on the host, then lc01 and LC02 will be synchronized
As shown in the above image, we have created lc02txt in the host mount directory and edited it to create LC02. Now let’s see if the two containers are synchronized
As shown in the figure above, lc01 and LC02 both exist in LC02.txt, it seems that the data transfer is successful, so the new LC03 container is the same, there is no map here
Since new can be shared with data passing, deletion must be synchronized. If lc01 is deleted, does the data from lc02, which inherits LC01, still exist?
Take a look at the picture below
The conclusion: Still there
-
Delete all data container volumes
docker volume rm $(docker volume ls -q) Copy the code
To stop and remove containers before delete data volume, otherwise you will be prompted data volume is being used to remove failure, found a problem, is through the command starts and create data volume of container, the container delete again after operation, the previously created data volume, and created by way of Dockerfile data volume, will always exist