Docker installation and common basic command operations

One, Docker installation

  • Prepare the experimental environment
[root@docker ~]# systemctl stop firewalld && systemctl disable firewalld
[root@docker ~]# setenforce 0
[root@docker ~]# sed -i  "7s/enforcing/disabled/" /etc/selinux/config 	//关闭核心防护
  • Install docker engine dependency package
[root@docker ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 libsemanage-python libselinux-utils
  • Add Docker package source
[root@docker ~]# cd /etc/yum.repos.d/
[root@docker yum.repos.d]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

//这里我们设置阿里云镜像源,只有在下载完docker-ce.repo配置文件后,才可以正常安装Docker-CE(社区版Docker引擎)
  • Install Docker-CE engine, community edition
[root@docker ~]# yum install -y docker-ce
  • Turn on Docker and set it to turn on self-start
[root@docker ~]# systemctl start docker.service && systemctl enable docker.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

Two, Docker basic commands

2.1 Query docker version information

[root@docker ~]# docker version 
Client: Docker Engine - Community
 Version:           20.10.5
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        55c4c88
 Built:             Tue Mar  2 20:33:55 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.5
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       363e9a8
  Built:            Tue Mar  2 20:32:17 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.4
  GitCommit:        05f951a3781f4f2c1911b05e61c160e9c30eaa8e
 runc:
  Version:          1.0.0-rc93
  GitCommit:        12644e614e25b05da6fd08a38ffa0cfe1903fdec
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

2.2 Docker image acceleration and network optimization

  • Mirror acceleration registration address: https://help.aliyun.com/document_detail/60750.html

  • After the accelerated image loading is completed, the process and docker need to be restarted, because docker works in the background as a daemon process

[root@docker ~]# systemctl daemon-reload && systemctl restart docker
  • Network Optimization
[root@docker ~]# echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf 
[root@docker ~]# sysctl -p
net.ipv4.ip_forward = 1
[root@docker ~]# service network restart
Restarting network (via systemctl):                        [  确定  ]
[root@docker ~]# systemctl restart docker

2.3 Mirror operation

2.3.1 Mirror search command

  • format:
dcoker search 关键字
  • Example:
[root@docker ~]# docker search nginx

Insert picture description here

2.3.2 Download nginx image

  • format:
docker pull 关键字
  • Example:
[root@docker ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a076a628af6f: Pull complete 
0732ab25fa22: Pull complete 
d7f36f6fe38f: Pull complete 
f72584a26f32: Pull complete 
7125e4df9063: Pull complete 
Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
  • Note:
    Stored in the following path after downloading: /var/lib/docker/image/overlay2/repositories.json
[root@docker ~]# cat /var/lib/docker/image/overlay2/repositories.json

Insert picture description here

2.3.3 View image

  • format:
docker images
  • Example:
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    f6d0b4767a6c   8 weeks ago   133MB
  • View mirror information
格式:docker inspect 镜像ID

[root@docker ~]# docker inspect f6d0b4767a6c 	//借助IMAGE ID标签 详细查看镜像信息
[
    {
    
    
        "Id": "sha256:f6d0b4767a6c466c178bf718f99bea0d3742b26679081e52dbf8e0c7c4c42d74",
        "RepoTags": [
            "nginx:latest"
        ],
        "RepoDigests": [
            "nginx@sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa"
        ],
'//省略部分信息'

2.3.4 Add Mirror Label

  • format:
docker tag 镜像标签 新标签
  • Example:
[root@docker ~]# docker tag nginx:latest nginx:web		//添加标签web
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    f6d0b4767a6c   8 weeks ago   133MB
nginx        web       f6d0b4767a6c   8 weeks ago   133MB
  • Delete mirror
格式:docker rmi 镜像标签

[root@docker ~]# docker rmi nginx:web
Untagged: nginx:web
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    f6d0b4767a6c   8 weeks ago   133MB

//注意此处有两个相同的镜像,所以删除的只是别名;如果把最后一个也删除,那镜像也会被删除

2.3.5 Mirror output and import

  • Mirror output
格式:docker save -o 导出的文件名 镜像标签

[root@docker ~]# cd /opt/
[root@docker opt]# docker save -o nginx nginx:latest		//镜像输出
[root@docker opt]# ls
containerd  nginx  rh
  • Image import
格式:docker load < 镜像文件

示例:
[root@docker opt]# docker rmi f6d0b4767a6c	//先删除镜像
Untagged: nginx:latest
Untagged: nginx@sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Deleted: sha256:f6d0b4767a6c466c178bf718f99bea0d3742b26679081e52dbf8e0c7c4c42d74
Deleted: sha256:4dfe71c4470c5920135f00af483556b09911b72547113512d36dc29bfc5f7445
Deleted: sha256:3c90a0917c79b758d74b7040f62d17a7680cd14077f734330b1994a2985283b8
Deleted: sha256:a1c538085c6f891424160d8db120ea093d4dda393e94cd4713e3fff3c82299b5
Deleted: sha256:a3ee2510dcf02c980d7aff635909612006fd1662084d6225e52e769b984abeb5
Deleted: sha256:cb42413394c4059335228c137fe884ff3ab8946a014014309676c25e3ac86864

[root@docker opt]# docker images		//查看镜像是否删除
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

[root@docker opt]# docker load < nginx		//再导入镜像
cb42413394c4: Loading layer [==================================================>]  72.51MB/72.51MB
1c91bf69a08b: Loading layer [==================================================>]   64.6MB/64.6MB
56bc37de0858: Loading layer [==================================================>]  3.072kB/3.072kB
3e5288f7a70f: Loading layer [==================================================>]  4.096kB/4.096kB
85fcec7ef3ef: Loading layer [==================================================>]  3.584kB/3.584kB
Loaded image: nginx:latest

[root@docker opt]# docker images	
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    f6d0b4767a6c   8 weeks ago   133MB
//导入成功

2.3.6 Summary of Common Mirroring Commands

镜像搜索:
docker search 关键字(nginx)

镜像下载:
docker pull 关键字(nginx)

镜像查询:
docker images

查看镜像详细信息:
docker inspect 镜像ID

添加镜像标签:
docker tag 镜像标签 新标签
docker tag nginx:latest nginx:web

删除镜像:
docker rmi 镜像标签(nginx:latest)

镜像输出:
docker save -o nginx_img nginx:latest

镜像导入:
docker load <镜像文件(nginx_img)

2.4 Basic commands for container operations

2.4.1 Container list query

  • format:
docker ps -a 	(-a 列出最近一次启动的容器)
  • Example:
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

2.4.2 Create a container

  • format:
docker create -it 选项 /bin/bash

-i 让容器的标准输入保持打开
-t 让Docker分配一个伪终端
  • Example:
[root@docker ~]# docker create nginx:latest /bin/bash		//创建容器
bf659453c870227ef2763c6f74284396f89097ce067f26db1d838241b88eb0b1
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS    PORTS     NAMES
bf659453c870   nginx:latest   "/docker-entrypoint.…"   8 seconds ago   Created             elegant_galois

参数解释:
/bin/bash 指定终端
statys :created 创建状态,暂时还未开启
container id  容器ID

2.4.3 Start the container

  • format:
docker start 容器ID
  • Example:
[root@docker ~]# docker start bf659453c870
bf659453c870
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS                     PORTS     NAMES
bf659453c870   nginx:latest   "/docker-entrypoint.…"   3 minutes ago   Exited (0) 7 seconds ago             elegant_galois

2.4.4 One-time execution of container operations

  • format:
docker run 镜像名称 操作指令
  • Example:
[root@docker ~]# docker run centos:7 /usr/bin/bash -c ls /

#docker run 一次性运行centos:7镜像(如在本地未发现镜像会自动在容器仓库中下载该镜像) 
-c (command指令,进入到centos:7容器) ls / (执行ls /命令)

Insert picture description here

  • If you don't want to end the process after executing the command, you can give a loop statement command as follows:
docker run -d centos:7 /bin/bash -c "while true;do echo hello;done"

2.4.5 Enter the container

  • format:
docker run -it 容器名 容器环境

在容器为运行状态下:docker exec -it 容器ID 容器环境

[root@docker ~]# docker exec --help		//列出 docker exec 相关参数

Usage:  docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Run a command in a running container

Options:
  -d, --detach               Detached mode: run command in the background
      --detach-keys string   Override the key sequence for detaching a container
  -e, --env list             Set environment variables
      --env-file list        Read in a file of environment variables
  -i, --interactive          Keep STDIN open even if not attached
      --privileged           Give extended privileges to the command
  -t, --tty                  Allocate a pseudo-TTY
  -u, --user string          Username or UID (format: <name|uid>[:<group|gid>])
  -w, --workdir string       Working directory inside the container
  • Example:
[root@docker ~]# docker run -it nginx:latest /bin/bash
root@ae3b094a198d:/# exit
exit
[root@docker ~]#
  • Container export and import
容器导出:
docker export 容器ID > 导出的容器名 

容器导入:
cat 容器名 | docker import - 镜像名 (生成镜像,不会生成容器)

示例:
[root@docker ~]# docker export bf659453c870 > centos_c
[root@docker ~]# ls
anaconda-ks.cfg  container-selinux-2.119.2-1.911c772.el7_8.noarch.rpm  公共  视频  文档  音乐
centos_c         initial-setup-ks.cfg                                  模板  图片  下载  桌面

2.4.6 Container deletion

  • format:
docker rm 容器ID (必须先停止再删除,删除正在运行的容器添加 -f 参数)

停止容器有2种方式:
 docker stop 容器名或容器id
 docker kill 容器名或容器id
  • View docker rm related parameters
[root@docker ~]# docker rm --help

Usage:  docker rm [OPTIONS] CONTAINER [CONTAINER...]

Remove one or more containers

Options:
  -f, --force     Force the removal of a running container (uses SIGKILL)
  -l, --link      Remove the specified link
  -v, --volumes   Remove anonymous volumes associated with the container
  • Example:
[root@docker ~]# docker ps -a			//查看容器列表
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                      PORTS     NAMES
ae3b094a198d   nginx:latest   "/docker-entrypoint.…"   6 minutes ago    Exited (0) 6 minutes ago              kind_robinson
eac2f146ef01   centos:7       "/usr/bin/bash -c ls…"   38 minutes ago   Exited (0) 38 minutes ago             sleepy_liskov
bf659453c870   nginx:latest   "/docker-entrypoint.…"   48 minutes ago   Exited (0) 44 minutes ago             elegant_galois

[root@docker ~]# docker rm bf659453c870		//删除容器
bf659453c870

[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                      PORTS     NAMES
ae3b094a198d   nginx:latest   "/docker-entrypoint.…"   7 minutes ago    Exited (0) 7 minutes ago              kind_robinson
eac2f146ef01   centos:7       "/usr/bin/bash -c ls…"   39 minutes ago   Exited (0) 39 minutes ago             sleepy_liskov

注意:

  • If you want to delete containers in batches, you can use regular expressions
docker ps -a | awk '{print "docker rm " $1}' | bash

#遍历容器ID,依次删除,但如果有UP状态的容器则会报错

Supplementary commands:

退出容器
control + d 

查看日志
docker logs -f 容器名或id

2.4.7 Summary of Docker container commands

  • View, stop, delete container command summary
1.列出所有容器 ID
docker ps -aq

2.停止所有容器
docker stop $(docker ps -aq)

3.开启所有容器
docker start $(docker ps -aq)

4.开启/停止单个容器
docker start/stop 要停止的容器名

5.删除所有容器
docker rm $(docker ps -aq)

6.删除所有 Exited 状态的容器
docker rm $(docker ps -a | grep "Exited" | awk '{print $1 }')

7.删除单个容器
docker rm 要删除的容器名

8.删除所有的镜像
docker rmi $(docker images -q)

9.删除所有 none 状态的镜像
docker rmi $(docker images | grep "none" | awk '{print $3}')
  • Other commonly used commands
容器列表状态查询
docker ps -a

创建容器
docker create -it 容器名 环境
docker create -it nginx:latest /bin/bash

一次性容器操作
docker run 镜像名称 操作指令
docker run centos:7 /usr/bin/bash -c ls /

进入容器
docker run -it 容器名 环境
docker run -it nginx:latest /bin/bash

容器导入
cat 容器ID | docker import -镜像名 (会生成镜像,不会生成容器)

容器导出
docker export 容器ID > 导出的容器名

容器删除
docker rm 容器ID (必须先停止再删除)

Guess you like

Origin blog.csdn.net/weixin_42449832/article/details/114630628