Docker installation and mirroring, container basic commands

One, install Docker

1. Install docker's dependent environment

yum -y install yum-utils device-mapper-persistent-data lvm2

2. Set up the mirror source of docker (here is Alibaba source)

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

3. Install docker

yum makecache fast
yum -y install docker-ce docker-ce-cli containerd.io 

4. Start and set to start self-starting

systemctl start docker 			#启动docker
systemctl enable docker			#设置开机自启动

5. Test

docker run hello-world        #先在本地仓库查找,本地没有会去网上找

6. Docker's central warehouse

1)docker官方的中央仓库,这个仓库镜像最全,但是下载速度较慢
http://docs.docker.com
2)国内的镜像网站
https://c.163yun.com/hub#/home      (网易数帆)
http://hub.daocloud.io (推荐使用)
http://docker.mirrors.ustc.edu.cn  (中科大开源镜像)
http://hub-mirror.c.163.com    (网易开源镜像)

Second, the basic mirroring command

docker version 			   #显示docker的版本信息
docker info				   #显示docker的更加详细的信息,包括镜像和容器的数量
systemctl status docker    #查看docker状态
docker stats               #查看docker内容器的运行状态
docker info                #查看docker概要信息
docker --help              #查看docker帮助文档

REPOSITORY:镜像的仓库源
TAG :镜像的标签
IMAGE ID:镜像的id
CREATED:镜像创建的时间
SIZE:镜像的大小

可选项:
-a:查看全部的容器,包括没有运行的
-q:只查看容器的标识

docker search 镜像名称      #搜索镜像
可选项,通过收藏来过滤
--filter=stars=对应项的数字
#拉取镜像到本地
docker pull 镜像名称[:tag]

#上传镜像
docker push 镜像名称[:tag]

#查看本地全部镜像
docker images 

#删除本地镜像
docker rmi 镜像的标识(image id) 例:docker rmi redis:5 或者 docker rmi d1165f221234 (通过image ID删除)
docker rmi $(docker images -qa)			#删除全部镜像

#镜像的导入导出(不规范)
将本地的镜像导出
docker save -o 导出的路径 镜像id
加载本地镜像文件
docker load -i 镜像文件
修改本地上传镜像名
docker tag 镜像id 新镜像名称:tag(版本)

#docker的默认工作路径
/var/lib/docker

#配置阿里云镜像加速
mkdir -p /etc/docker

tee /etc/docker/daemon.json <<-'EOF'
{
    
    
  "registry-mirrors": ["https:......com"] #此处镜像加速命令需要自己注册阿里云账号在控制台中自己复制
}
EOF

systemctl daemon-reload        #重新加载服务的配置文件
systemctl restart docker       #重启docker

First log in to "Alibaba Cloud", then log in after registration, click on the console
Insert picture description here
, search for "container mirroring service" in the search box , click on the
Insert picture description here
image acceleration service, and
Insert picture description here
finally select the corresponding system copy box.
Insert picture description here

Three, container commands

1. Run and start the container

#简单操作
docker run 镜像标识|镜像名称[:tag]
#常用参数
docker run -d -p 宿主机端口:容器端口 --name 容器名称 镜像标识|镜像名称[:tag]
选项:
-d:后台运行容器
-p:宿主机端口:容器端口:为了映射当前Linux的端口和容器的端口
--name 容器名称:指定容器的名称 
--it 使用交互方式运行,进入容器查看内容
-P:指定容器端口
-p:随机指定端口

例:docker run --name mynginx -p 80:80 nginx        #操作台运行
    docker run -di --name mynginx -p 80:80 nginx    #后台运行

2. View the running container

docker ps 
选项:
-f:过滤    
-a:查看全部的容器,包括没有运行的
-q:只查看容器的标识
-n=?:显示最近创建的容器
-l:查看最后创建的容器

例:docker ps -f status=exited  #过滤已经存在但停止的容器
    docker ps -n 5   #列出最近创建的n个容器

3. View the log of the container

docker logs -f 容器id
docker logs -ft --tail num 容器id
选项:
--tail num:要显示日志的条数
-tf:显示日志

4. Enter into the container

docker exec -it 容器id/容器名称 /bin/bash		
docker attach 容器id

区别:
docker exec:进入容器后开启一个新的终端,可以在里面操作(常用)
docker attach:进入容器正在执行的终端,不会启动新的进程

5. Delete the container (before deleting the container, you need to stop the container)

docker stop 容器id								   #停止指定的容器
docker stop $(docker ps -qa)					   #停止全部容器

docker rm 容器名/容器id						       #删除指定的容器,删除前该容器需要停止
docker rm $(docker ps -qa)						   #删除状态不是up外的所有容器
docker ps -a | awk '{print "docker rm "$1}'|bash   #删除状态不是up外的所有容器

6. Start the container

docker start 容器id/容器名称

7. Exit the container

exit			#直接容器停止并退出
Ctrl+P+Q		#容器不停止退出

8. Display the process information of the container

docker top 容器id

9. View the metadata of the image

docker inspect 容器id

10. Copy files from the container to the host

#如果我们需要将文件拷贝到容器内可以使用cp命令。
docker cp 需要拷贝的文件或目录(目的主机路径) 容器名称/ID:容器目录(绝对路径)

#也可以将文件从容器内拷贝出来。
docker cp 容器名称/ID:容器目录(绝对路径) 需要拷贝的文件或目录(目的主机路径)

11. Directory mounting (container data volume operation) through directory mounting to prevent data loss due to accidental deletion of containers

(1) Mount the specified directory

docker run -di --name nginx02 -p 81:80 -v /mydata/docker_nginx/:/abc/123 nginx
##-di为后台运行,创建并运行一个叫做nginx02的容器,将宿主机的81端口与容器中nginx的80端口进行关联,使用-v选项将宿主机的/mydata/docker_nginx/目录与容器中的/abc/123目录进行关联挂载

#如果一开始没有进行挂载,可以后期在配置文件中进行修改
 docker inspect nginx02     #修改配置文件的命令
    "Mounts": [             #修改mounts下内容
            {
    
     
                "Type": "bind",
                "Source": "/mydata/docker_nginx",    #宿主机挂载目录
                "Destination": "/abc/123",           #容器挂载目录
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }

(2) Anonymous mounting

docker run -di -v /usr/local/data --name centos7 centos:7
#后台运行,创建并运行叫做centos7的容器,此处只指定了容器的目录/usr/local/data,没有指定宿主机的目录,因为匿名挂载,都会被默认挂载到宿主机的/var/lib/docker/volumes/目录下

(3), named mount

docker run -di -v docker_centos_data:/usr/local/data --name centos02 centos:7
#具名挂载也是挂载在宿主机的/var/lib/docker/volumes/下,但是不是随机起名字进行挂载的,而是根据自己起的名字进行挂载,此处起的名字为docker_centos_data

(4) When mounting, you can also set permissions, read only/write only

docker run -di --name nginx07 -P -v /mydata/docker_nginx/:/abc:ro nginx
#-P是随机指定端口,ro是只读权限,rw是只写权限

(5), inheritance (volumes-from)

docker run -di --name nginx09 --volumes-from nginx07:rw nginx
#创建运行叫做nginx09的容器,使用--volumes-from命令继承nginx07的所有的挂载目录与文件,并设置为只写权限(:rw)

12. View the directory mounting relationship

[root@localhost ~]# docker volume inspect docker_centos_data    #docker_centos_data是创建在的目录卷
    {
    
    
        "CreatedAt": "2021-03-19T16:02:53+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/docker_centos_data/_data",   #从这里可以看到创建的数据集位置
        "Name": "docker_centos_data",
        "Options": null,
        "Scope": "local"
    }
]

13. Check the container IP address

[root@localhost ~]# docker inspect  nginx07        #查看容器nginx07的IP
                    "Gateway": "172.17.0.1",       #容器的网关
                    "IPAddress": "172.17.0.5",     #容器的IP
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:05",
                    "DriverOpts": null

14. Container export

docker export 6c9aabe7b1e5 > nginx_c

15. Container import (image will be generated, but container will not be created)

cat nginx_c | docker import-nginx:web

16. Container interconnection (using centos mirroring)

docker run -itd -P --name web11 centos:7 /bin/bash  #创建并运行容器取名web11,端口号自动映射
docker run -itd -P --name web22 --link web11:web11 centos:7 /bin/bash   #创建并运行容器取名web22,链接到web11和其通信

进web22容器ping web1
#--link name:alias --link容器名:别名

17. Docker image backup, recovery and migration

(1) Mirror backup

Use docker save to save the specified image as a tar archive file.
docker save [OPTIONS] IMAGE [IMAGE...]

For example: docker save -o /root/mycentos.tar mycentos:7
● -o: The output directory of the archive file after the image is packaged.

(2) Mirror recovery

Use docker load to import the image archive file exported by the docker save command.

docker load [OPTIONS]

For example: docker load -i /root/mycentos.tar
●–input, -i: specify the imported file;
●–quiet, -q: streamline the output information.

(3) Mirror migration

Mirror migration involves the above two operations at the same time, backup and recovery.
We can migrate any Docker image from one machine to another. In the migration process, we first need to build the container as a Docker image. Then, the Docker image is saved locally as a tar package file. At this time, we only need to copy or move the image to the machine we want, restore the image and run the container.
Of course, in addition to this method, we can also use the mirror warehouse to implement mirror backup, recovery and migration. Next, we will learn how to use the DockerHub mirror warehouse.

to sum up:

1、镜像上传(push) / 下载(pull) / 打标签(tag) / 删除(rmi) / 导出(save -o) / 导入(load)
2、容器创建(create) / 进入(exec) / 运行(run) / 删除(rm) / 导出(export) / 导入(import)
   数据卷(-v) / 数据卷容器(--volumes-from) / 链接容器(--link) / 暴露端口(随机:-P 指定:-p)
3、仓库下载registry镜像-->运行容器暴露端口5000---->修改daemon.json文件---->重启docker----->打标签(IP: 端口/名称)--->上传/下载

Guess you like

Origin blog.csdn.net/Gengchenchen/article/details/115092287