First to give you a few learning Docker website:

Docker official website: www.docker.com Docker Chinese website: www.docker-cn.com Docker Hub official website: hub.docker.com (warehouse)

Docker installation

I. Environmental description

We are using CentOS 7 (64-bit). Currently, CentOS only supports Docker kernels in distributions. Docker runs on CentOS 7. The operating system must be 64-bit and the kernel version must be at least 3.10.

View your own system kernel u-name -r This command displays the information about the current system, such as the kernel version, hardware architecture, host name, and operating system type.

[root@jianyou /]# uname -r
3.10.0-1127.19.1.el7.x86_64
Copy the code

View the version information cat /etc/os-release

[root@jianyou /]# cat /etc/os-release NAME="CentOS Linux" VERSION="7 (Core)" ID="centos" ID_LIKE="rhel fedora" VERSION_ID="7" PRETTY_NAME="CentOS Linux 7 (Core)" ANSI_COLOR="0; 31" CPE_NAME="cpe:/o:centos:centos:7" HOME_URL="https://www.centos.org/" BUG_REPORT_URL="https://bugs.centos.org/" CENTOS_MANTISBT_PROJECT="CentOS-7" CENTOS_MANTISBT_PROJECT_VERSION="7" REDHAT_SUPPORT_PRODUCT="centos" REDHAT_SUPPORT_PRODUCT_VERSION="7"Copy the code

Ii. Installation steps

1, the website reference manual installation: docs.docker.com/engine/inst…

2. Make sure you’re CentOS7 or above, we’ve already done that

3, yum install GCC related environment (need to ensure that the VM can access the Internet in advance)

yum -y install gcc
yum -y install gcc-c++
Copy the code
  1. Uninstall the previous version
yum remove docker \ 
docker-client \
docker-client-latest \ 
docker-common \ 
docker-latest \ 
docker-latest-logrotate \
docker-logrotate \
docker-engine
Copy the code

5. Install required software packages

yum install -y yum-utils
Copy the code
  1. Setting up the Mirror Warehouse
# error yum - config manager - add - 'https://download.docker.com/linux/centos/docker-ce.repo # # error [14] Errno curl# 35 - TCP connection reset by peer [Errno 12] curl# 35-timeout # sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repoCopy the code

7. Update the yum software package index

yum makecache fast
Copy the code

8. Install the Docker CE

yum install docker-ce docker-ce-cli containerd.io
Copy the code
  1. Start the Docker
systemctl start docker
Copy the code

10. Test commands

Docker version // View the current version of the docker docker run hello-world // Run the hello-world image docker images // List all images in the DockerCopy the code
  1. uninstall
Systemctl stop docker run yum -y remove docker-ce docker-ce-cli containerd. IO delete the docker installation package rm -rf /var/lib/docker // Delete the directory where the docker residesCopy the code

Three. Use the Ali cloud image acceleration

1, introduction: www.aliyun.com/product/acr 2, registered a belongs to own ali cloud account (reusable taobao account) 3, into the management console password, opened four accelerator, view image of himself

5. Configure mirror acceleration

sudo mkdir -p /etc/docker      //1.

sudo tee /etc/docker/daemon.json <<-'EOF'  //2.
{
"registry-mirrors": ["https://p9kvs31t.mirror.aliyuncs.com"] 
}
EOF

sudo systemctl daemon-reload  //3.

sudo systemctl restart docker  //4.
Copy the code

Test the hello – world

  1. Start the hello world
docker run hello-world
Copy the code
  1. What happens after run?

Common Docker commands

1. Help command

Docker version // Displays docker version information docker info // Displays docker system information, including the number of images and containers docker --help // Displays helpCopy the code

2. Run the mirror command

Viewing all mirrors

================== [root@jianyou /]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE Tomcat 9.0 710EC5C56683 2 Days ago 668MB nginx latest 08b152AFcfae 2 weeks ago 133MB Portainer/Portainer latest 580C0e4e98B0 4 months ago 79.1MB Hello -world latest D1165F221234 5 months ago 13.3KB centos latest 300E315ADb2f 8 months ago 209MB Rancher /server latest 98d8bb571885 15 months ago 1.08GB Rancher/Agent v1.2.11 1CC7591af4f5 3 years ago 243MB ## # A repository source can have multiple tags, representing different versions of the repository source. We use repository: TAG to define different images. Docker will use the default Lastest image! Docker images [optional] # command optional -a lists all local images. -q displays only the iD of the imageCopy the code

Search for an image

Search for images from the DockerHub repository

[root@jianyou /]# docker search nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 15284 [OK] Jwilder /nginx-proxy Automated Nginx Proxy for Docker Con... 2057 [OK] richarvey/nginx-php-fpm capable of running nginx + php-fpm 815 [OK] # docker search for the name of an image in the DockerHub repository # optional --filter=stars=50: lists all images whose collections are at least the specified value.Copy the code

Downloading an image

Download mirror

[root@jianyou /]# docker pull redis Using default tag: latest Latest: Pulling from library/redis 33847F680F63: Already exists download 26a746039521: Pulling FS Layer 18d87DA94363 Pulling fs layer 5e118a708802: Pulling fs layer ecf0dbe7c357: Waiting 46f280ba52da: WaitingCopy the code

Remove the mirror

Remove the mirror

Docker rmI -f $(Docker images-QA) # delete docker rmI -F $(Docker images-QA) # delete docker rmI -F $Copy the code

3. Container command

There are images to create containers, we use centos image here to test, is virtualization a centos!

docker pull centos
Copy the code

Create a new container and start

Docker run [OPTIONS] IMAGE [COMMAND][ARG...] Docker run -d it -p 8888:80 --net bridge --name tomcat01 tomcat # docker run -d it -p 8888:80 --net bridge --name tomcat01 And returns the container ID! -i # Run the container in interactive mode, reassign a terminal to the container by using -t # with -t, usually with -i using -p # random port mapping (uppercase) -p # Specify port mapping (lowercase), - v # # data volume mapping - net set up the network connection mode Can generally have four writing IP: hostPort: containerPort IP: : containerPort hostPort: containerPort (common) ContainerPort # test [root@jianyou /]# Docker images REPOSITORY TAG IMAGE ID CREATED SIZE Tomcat 9.0 710EC5C56683 2 days ago 668MB redis latest aa4d65e670d6 2 weeks ago 105MB nginx latest 08b152afcfae 2 weeks ago 133MB portainer/portainer Latest 580C0E4E98B0 4 months ago 79.1MB Hello -world latest D1165F221234 5 months ago 13.3kB centos Latest 300E315ADB2f 8 Months ago 209MB Rancher/Server latest 98D8BB571885 15 months ago 1.08GB Rancher/Agent V1.2.11 1CC7591AF4F5 3 years ago Run the /bin/bash command on the centos container in interactive mode. [root@jianyou /]# docker run --name="mycentos" -d -it centos /bin/bash If 605 e63a044bb943e8f7bf2f4ad3ddc02fec9040087330af6ad6e29f82b5bab0e appear a string, represents the centos running success!Copy the code

Just in background form, now we don’t run in background form

[root@jianyou /]# docker run --name="mycentos02" -it centos /bin/bash [root@9bbd00c72f6f /]# CTRL +P+Q # The container does not stop exitingCopy the code

Start/stop the container

Docker start (containers id or name) # start the container docker restart (containers id or name) # container restart docker stop (containers id or name) # stop container docker kill (container id Or the container name) # force stop the containerCopy the code

Remove the container

Docker rm containers id # delete specified docker rm -f $# (docker ps - a - q) remove all containers docker ps - a - q | xargs docker rm # delete all containersCopy the code

See the log

Docker logs -f -t --tail Container IDCopy the code

View all running containers

docker ps
Copy the code

View information about processes running in a container. Ps is supported

[root@jianyou /]# docker ps // Create STATUS PORTS NAMES 605e63A044bb centos "/bin/bash" 16 hours ago Up 16 hours mycentos 8ad9f258cbab portainer/portainer "/portainer" 18 hours ago Up 17 hours 0.0.0.0:8088 - > 9000 / TCP. :::8088->9000/ TCP naughty_hamilton [root@jianyou /]# docker top 605e63A044bb UID PID PPID C STIME TTY TIME CMD root 27276 27257 0 Aug09 pts/0 00:00:00 /bin/bashCopy the code

View metadata of containers and mirrors

Docker inspect container ID [root@jianyou /]# docker inspect 605e63A044bb [{" id ": "605e63a044bb943e8f7bf2f4ad3ddc02fec9040087330af6ad6e29f82b5bab0e", "Created": "The 2021-08-09 T10:01:31. 469732325 z", "Path" : "/ bin/bash", "Args" : [], "State" : {" Status ":" running ", "running" : true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 27276, "ExitCode": 0, "Error": ", "StartedAt": "2021-08-09T10:01:31.896550068z ", "FinishedAt": "0001-01-01T00:00:00Z", "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55", "ResolvConfPath": "/var/lib/docker/containers/605e63a044bb943e8f7bf2f4ad3ddc02fec9040087330af6ad6e29f82b5bab0e/resolv.conf", "HostnamePath": "/var/lib/docker/containers/605e63a044bb943e8f7bf2f4ad3ddc02fec9040087330af6ad6e29f82b5bab0e/hostname", "HostsPath": "/var/lib/docker/containers/605e63a044bb943e8f7bf2f4ad3ddc02fec9040087330af6ad6e29f82b5bab0e/hosts", "LogPath": "/var/lib/docker/containers/605e63a044bb943e8f7bf2f4ad3ddc02fec9040087330af6ad6e29f82b5bab0e/605e63a044bb943e8f7bf2f4ad3 ddc02fec9040087330af6ad6e29f82b5bab0e-json.log", xxxxxxxxxxx.................Copy the code

Enter the running container

[root@jianyou /]# docker ps // list all running containers CONTAINER id IMAGE COMMAND  CREATED STATUS PORTS NAMES 605e63a044bb centos "/bin/bash" 16 hours ago Up 16 hours mycentos 8ad9f258cbab Portainer /portainer" /portainer" 18 hours ago Up 17 hours 0.0.0.0:8088->9000/ TCP, :::8088->9000/ TCP naughty_hamilton [root@jianyou /]# docker exec -it 605e63A044bb /bin/bash //(in the form of a newly opened terminal) into the container [root@605e63a044bb /]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 Aug09 pts/0 00:00:00 /bin/bash root 30 1 0 01:46 PTS /0 00:00:00 ps -ef # docker attach CONTAINER id # test2 [root@jianyou /]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 605e63a044bb centos "/bin/bash" 16 hours ago Up 16 hours mycentos 8ad9f258cbab portainer/portainer "/portainer" 18 hours ago Up 17 hours 0.0.0.0:8088->9000/ TCP, :::8088->9000/ TCP naughty_hamilton [root@jianyou /]# docker Attach 605e63A044BB [root@605e63a044bb /]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 00 Aug09 PTS /0 00:00:00 /bin/bash  root 30 1 0 01:46 pts/0 00:00:00 ps -efCopy the code

Copy files from a container to a host

Docker cp Container ID: Inside path Destination host path

# test # execute in container, Create a file to test [root@605e63a044bb /]# CD /home// into the home directory in the container [root@605e63a044bb home]# ls [root@605e63a044bb home]# touch Jianyou. Java // Create a new Java file in the container [root@605e63a044bb home]# ls jianyou. Java [root@605e63a044bb home]# exit [root@jianyou /]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 605e63A044bb centos "/bin/bash" 16 hours ago Up 16 hours mycentos 8ad9f258cbab portainer/portainer "/portainer" 18 hours ago Up 17 Pump 0.0.0.0:8088 - > 9000 / TCP. :::8088->9000/tcp naughty_hamilton [root@jianyou /]# docker cp 605e63a044bb:/home/jianyou.java /home [root@jianyou /]# CD home [root@jianyou home]# ls jianyou. Java WWW xinyou // [root@jianyou home]#Copy the code

Container data volume

Data mount

Using a Data Volume

Method 1: Use the command directly in the container to add

mount

[root@jianyou dockerfile]# docker run -it -v /home/ceshi:/home centos /bin/bashCopy the code

Check whether the data volume is successfully mounted

Docker inspect container id "Mounts" : [{" Type ":" bind ", "Source" : "/ home/ceshi", "Destination" : "/ home", "Mode" : ""," RW ": True, "Propagation": "rprivate"}Copy the code

Next, we respectively enter the host and docker container corresponding directory, test data sharing discovery, has been successful!

[root@jianyou home]# docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql - e MYSQL_ROOT_PASSWORD = 2559 - name mysql01 mysql: 5.7 039 df7f3e8e2eeab38620292e96216cd45ee0fe5985515fcc01826231116910e [root@jianyou home]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 039DF7f3e8e2 mysql:5.7 "Docker - entrypoint. S..." 4 seconds ago Up 3 seconds 33060/ TCP, 0.0.0.0:3310->3306/ TCP, :::3310->3306/tcp mysql01 04203c3c0b33 centos "/bin/bash" 14 minutes ago Up 12 minutes centos222 [root@jianyou home]#Copy the code

Dockerfile file configuration

The instance

Springboot project configuration

FROM Java :8 MAINTAINER jianyou<[email protected]> # Introduce the.jar file FROM the same directory into the container and rename it app.jar COPY *.jar app.jar # Change the access time and modification time of this file to the current time, Without modifying the contents of the file. EXPOSE 8080 ENTRYPOINT ["java", "-jar", "app.jar", "--spring.profiles.active=dev", "--server.port=8080", "> /log/app.log"]Copy the code

Docker network connection

Viewing the Local IP Address

[root@jianyou /]# IP addr // <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host LO valid_lft forever preferred_lft forever Inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 52:54:00: B3:32 :e5 BRD FF :ff:ff:ff:ff inet 172.21.0.17/20 BRD 172.21.15.255 scope global eth0 VALID_lft forever preferred_lft forever inet6 fe80::5054:ff:feb3:32e5/64 scope link valid_lft forever preferred_lft forever // Docker0 is our installed docker will appear, it uses the bridge mode to connect with the host. 3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:d1:9d:5a:14 brd Ff :ff:ff:ff:ff:ff :ff inet 172.17.0.1/16 BRD 172.17.255.255 scope Global Docker0 VALID_lft forever preferred_lft forever inet6 fe80::42:d1ff:fe9d:5a14/64 scope link valid_lft forever preferred_lft foreverCopy the code

Custom network

Create a custom network

// Add a bridge to the driver, Docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet docker exec-it Tomcat01 IP addr Directly enter the container and run the commandCopy the code

View network information

Docker network inspect network ID (or name) docker network inspect a networkCopy the code

Add a container to the network (so they can ping each other)

Docker network connect Mynet tomcat01 docker network connect mynet tomcat01 docker network connect mynet tomcat01 docker network connect mynet tomcat01 docker network connect mynet tomcat01Copy the code