Install Docker-ce on Centos7. The docker version installed directly with yum install docker -y is 1.12, but docker has developed rapidly, and now it is 18.03. docker-ce refers to the community version of docker. 1. Install yum-utils, which provides yum-config-manager, which can be used to manage yum sources yum install -y yum-utils
2、 Add yum source of Docker-CE yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
If you can't download the file, copy the file directly to /etc/yum.repos.d/docker-ce.repo. If you can't update it, delete the locked file rm.pid. 3. Update the yum source index yum makecache fast
4、 yum install docker-ceyum install docker-ce -y
5、 Start and stop Dockersystemctl command is a system service manager command, it is a combination of two commands service and chkconfig. ? Start docker: systemctl start? docker? Stop docker: systemctl stop? docker? Restart docker: systemctl restart? docker? Check docker status: systemctl status? docker? Startup: systemctl enable? docker
6、 Verify whether the installation is successful docker info
List all mirrors under docker: docker images
? REPOSITORY: the name of the warehouse where the image is located? TAG: the image tag? IMAGE ID: the image ID? CREATED: the creation date of the image (not the date when the image was obtained)? SIZE: the image size? These images are stored in Docker [host] (https://cloud.tencent.com/product/cdh?from=10680) under the /var/lib/docker directory
3.3 Search for the mirror If you need to find the mirror you need from the network, you can search for the docker search mirror name through the following command
? NAME: warehouse name? DESCRIPTION: image description? STARS: user evaluation, reflecting the popularity of an image? OFFICIAL: official AUTOMATED: automatic construction, which means that the image was created by the Docker Hub automatic construction process
3.4.1 Pull the Docker image homepage from Docker Hub, including official images and other public images
Use command to pull: docker pull centos:7 At present, the speed of accessing docker hub in China is a bit awkward, and it is imperative to use docker mirror. There are many domestic businesses that provide docker image acceleration services. The following focuses on daocloud image acceleration. 3.4.1.1 Use Docker accelerator to register for users whose Docker client version is greater than 1.8 https://www.daocloud.io
Execute the following command under the Linux command line to use the accelerator: curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://8b4bf98a.m.daocloud.io
systemctl daemon-reload
systemctl restart docker
3.5 Delete image 1. docker rmi $IMAGE_ID: delete the specified image 2. docker rmi docker images -q
: delete all images
? View the last run container: docker ps -l
? View stopped containers docker ps -f status=exited
4.2 Create and start a container? Description of the parameters commonly used to create a container:? Create a container command: docker run? -i: means to run the container? -t: means that the container will enter its command line after it is started. After adding these two parameters, the container creation can log in. That is, a pseudo terminal is allocated. ?--name: Name the created container. ?-v: Represents the directory mapping relationship (the former is the host directory, the latter is the directory mapped to the host), you can use multiple -v to do multiple directory or file mapping. Note: It is best to do directory mapping, make changes on the host, and then share to the container. ?-d: Add the -d parameter after run, it will create a daemon container to run in the background (so that the container will not be automatically logged in after the container is created. If only the -i -t two parameters are added, it will be automatically created after creation. Go into the container). ?-p: Represents port mapping, the former is the host port, the latter is the mapping port in the container. You can use multiple -p to do multiple port mapping
4.1.1 Interactive container Create an interactive container and name it mycentosdocker run -it --name=mycentos centos:7 /bin/bash At this time, we check through the ps command and find that we can see the started container, and the status is started.
Use the exit command to exit the current container
Then use the ps -a command to see that the container is also stopped:
The guardian container creates a guardian container: For a container that needs to run for a long time, we can create a guardian container. The command is as follows (the container name cannot be repeated): docker run -di --name=mycentos2 centos:7? Log in to the guardian container: docker exec -it container_name (or container_id) /bin/bash (the container will not stop when exiting) )
4.3 Stop and start the container? Stop the running container: docker stop $CONTAINER_NAME/ID
? Start the container that has been running: docker start $CONTAINER_NAME/ID
4.4 File copy If we need to copy files to the container, we can use the cp command docker cp the files or directories that need to be copied Container name: container directory can also copy files from the container docker cp container name: the file or directory that the container directory needs to copy 4.5 Directory Mounting When creating a container, we can map the directory of the host machine to the directory in the container, so that we can modify the file in a directory of the host machine to affect the container. Create the container and add the -v parameter to the host directory: container directory docker run -di -v /usr/local/myhtml:/usr/local/myhtml --name=mycentos2 centos:7 If you are sharing a multi-level directory , A prompt of insufficient permissions may appear.
This is because the security module selinux in CentOS7 has disabled permissions. We need to add the parameter --privileged=true to solve the problem that the mounted directory does not have permissions. 4.6 View the container IP address We can use the following command to view the various types of containers running Data docker inspect mycentos2 can also directly execute the following command to directly output the IP address docker inspect --format='{{.NetworkSettings.IPAddress}}' mycentos2
4.7 Delete container? Delete the specified container: docker rm $CONTAINER_ID/NAME
Note that only stopped containers can be deleted? Delete all containers: docker rm docker ps -a -q
View mirror
5.1.2 Create a MySQL container docker run -di --name=123_mysql -p 33306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql-p stands for port mapping, the format is host mapping port: container running port -e stands for adding environment variables MYSQL_ROOT_PASSWORD is root user Login password 5.1.3 Enter MySQL container, login MySQL enter mysql container docker exec -it 123_mysql /bin/bash login mysqlmysql -u root -p 5.1.4 remote login MySQL (1) We connect to the virtual machine Centos on our local computer Docker container in, where 192.168.247.130 is the IP of the virtual machine operating system
5.1.5 To view the container IP address, we can use the following command to view various data of the container running docker inspect 123_mysql The running effect is as follows:
We can see that the IP of our database server is 172.17.0.2 5.2 Tomcat deployment 5.2.1 Pull tomcat image docker pull tomcat:8.5-jre85.2.2 Create tomcat container Create container for deploying java web application -p means address mapping docker run -di --name=123_tomcat -p 9000:8080 -v /usr/local/myhtml:/usr/local/tomcat/webapps --privileged=true tomcat:8.5-jre85.3 Nginx deployment 5.3.1 pull Nginx Mirror docker pull nginx5.3.2 Create Nginx container docker run -di --name=123_nginx -p 80:80 nginx5.3.3 Test Nginx browser address bar input: http://192.168.247.135
5.3.4 Configure the official nginx mirror of the reverse proxy, the nginx configuration file default.conf is in the /etc/nginx/conf.d directory. It is not convenient to edit the configuration file in the container. We can first copy the configuration file from the container to the host, edit and modify it and then copy it back. (1) Copy the configuration file from the container to the host docker cp 123_nginx:/etc/nginx/conf.d/default.conf default.conf (2) Edit default.conf and add reverse proxy configuration upstream daili {server 172.17.0.7 :8080;}server {listen 80; server_name www.test.com; location / {proxy_pass http://daili; index index.html index.htm; }}
(3) Copy the modified configuration file to the container docker cp default.conf 123_nginx:/etc/nginx/conf.d/default.conf (4) Restart the container docker restart 123_nginx (5) Set the domain name to 192.168.247.135 www.test.com 5.4 Redis Deploy 5.4.1 Pull Redis image docker pull redis 5.4.2 Create Redis container docker run -di --name=123_redis -p 6379:6379 redis
5.4.3 Client test In your local computer command prompt, use the window version of redis client to test redis-cli -h 192.168.247.135
< pre spellcheck="false" class="md-fences md-end-block contain-cm modeLoaded" lang="" contenteditable="false" cid="n484" mdtype="fences" style="box-sizing: border- box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: normal; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2 ; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0 px; text-decoration-style: initial; text-decoration-color: initial;">At this point, the images are all packaged. If you restore the docker image on a new machine, you only need to use: docker load --input centos_nginx. Tar will do. 6.3 Image restoration and migration
First, we delete the mynginx image and then execute this command to restore docker load --i mynginx.tar-i The input file is executed and the image is checked again, you can see that the image has been restored
Recommended Posts