目录
[1、docker search //从Docker Hub搜索镜像](#1、docker search //从Docker Hub搜索镜像)
[5、 为镜像添加新的标签](#5、 为镜像添加新的标签)
一、环境准备
1、开启虚拟化

2、关闭防火墙
bash
[root@agent ~] systemctl stop firewalld.service
[root@agent ~] setenforce 0
[root@agent ~] iptables -F
[root@agent ~] hostnamectl set-hostname docker
[root@agent ~] bash
[root@docker ~]
3、yum仓库获取阿里源(清华、京东都可以)
bash
[root@docker yum.repos.d] ll
总用量 12
drwxr-xr-x. 2 root root 195 7月 18 20:31 backup
-rw-r--r--. 1 root root 2523 8月 4 2022 Centos-7.repo
-rw-r--r--. 1 root root 2523 7月 17 2020 Centos-7.repo.0
-rw-r--r--. 1 root root 630 11月 23 2020 CentOS-Media.repo
4、确保能ping到外网
bash
[root@docker ~] ping www.baidu.com
PING www.baidu.com (220.181.38.150) 56(84) bytes of data.
64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=1 ttl=128 time=6.62 ms
64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=2 ttl=128 time=6.54 ms
二、安装docker
1、yum安装docker
bash
[root@docker ~] yum -y install docker
subscription-manager.x86_64 0:1.24.51-1.el7.centos
subscription-manager-rhsm.x86_64 0:1.24.51-1.el7.centos
subscription-manager-rhsm-certificates.x86_64 0:1.24.51-1.el7.centos
usermode.x86_64 0:1.111-6.el7
yajl.x86_64 0:2.0.4-4.el7
完毕!
2、启动docker并设置开机自启
bash
[root@docker ~] systemctl start docker
[root@docker ~] systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@docker ~]# docker version
Client:
Version: 1.13.1
API version: 1.26
Package version: docker-1.13.1-209.git7d71120.el7.centos.x86_64
Go version: go1.10.3
Git commit: 7d71120/1.13.1
Built: Wed Mar 2 15:25:43 2022
OS/Arch: linux/amd64
3、安装docker-ce阿里镜像加速器
bash
[root@docker ~] wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@docker ~] yum -y install yum-utils device-mapper-persistent-data lvm2
[root@docker ~] yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@docker ~] systemctl daemon-reload
[root@docker ~] systemctl restart docker
[root@docker ~] docker version
Client:
Version: 1.13.1
API version: 1.26
Package version: docker-1.13.1-209.git7d71120.el7.centos.x86_64
Go version: go1.10.3
Git commit: 7d71120/1.13.1
Built: Wed Mar 2 15:25:43 2022
OS/Arch: linux/amd64
三、docker基本操作
1、查看版本

2、查看详细信息

四、镜像操作
1、docker search //从Docker Hub搜索镜像

命令格式:docker search 关键字
参数:
--automated=false 仅显示自动创建的镜像
--no-trunc=false 输出信息不截断显示
-s, --stars=0 指定仅显示评价为指定星级以上的镜像
2、获取镜像
root@docker \~\]# docker pull nginx \[root@docker \~\]# docker pull centos #### 3、查看镜像信息  #### 4、获取镜像详细信息  #### 5、 为镜像添加新的标签  #### 6、删除镜像 docker rmi 镜像名称\[:标签\] 或者 docker rmi 镜像ID  #### 7、存出镜像和载入镜像 ```bash 存: [root@docker ~] docker save -o nginx-images nginx [root@docker ~] ls -l nginx-images -rw-------. 1 root root 191071744 7月 31 13:50 nginx-images 载入: [root@docker ~] docker save -o nginx-images nginx [root@docker ~] ls -l nginx-images -rw-------. 1 root root 191071744 7月 31 13:50 nginx-images [root@docker ~] docker rmi nginx Untagged: nginx:latest Deleted: sha256:89da1fb6dcb964dd35c3f41b7b93ffc35eaf20bc61f2e1335fea710a18424287 Deleted: sha256:e5afcbbf8f223b546a1db3d4f3c83064f346a2a8e17d4bfbaec1d12c90e2a6e3 Deleted: sha256:fda03119193d4611de17fa3d1eb9f02fb94333ac5d27ca507139a09ba0eaba1d Deleted: sha256:04d32bbd70d3d7e3368290157afdfb502799784b7c60d87487e77c7aafd67d2d Deleted: sha256:00d0e91fd006a5c96ec790434df1bb4ee545d84b34554ac2fbe5667568f916a1 Deleted: sha256:4f15baf3c136dbeff8c6f90737f0e54bd641095fd6441e359a1789ccbe554714 Deleted: sha256:748e3217b5fa76ff3ebd97186a6fcb595b92611ca87f480ea3d622e460c9a212 Deleted: sha256:c6e34807c2d51444c41c15f4fda65847faa2f43c9b4b976a2f6f476eca7429ce [root@docker ~] docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/centos latest 5d0da3dc9764 22 months ago 231 MB [root@docker ~]# docker load --input nginx-images c6e34807c2d5: Loading layer [==================================================>] 77.81 MB/77.81 MB 24ee1d7d6a62: Loading layer [==================================================>] 113.2 MB/113.2 MB 4deafab383fa: Loading layer [==================================================>] 3.584 kB/3.584 kB 8aedfcd777c7: Loading layer [==================================================>] 4.608 kB/4.608 kB c88d3a8ff009: Loading layer [==================================================>] 2.56 kB/2.56 kB abc3beec4b30: Loading layer [==================================================>] 5.12 kB/5.12 kB 922d16116201: Loading layer [==================================================>] 7.168 kB/7.168 kB Loaded image: docker.io/nginx:latest ``` ### 五、容器操作 #### 1、创建容器与运行容器 * -i 让容器的输入保持打开 * -t 让Docker 分配一个伪终端 * -d 守护进程形式运行 ```bash [root@docker ~] docker create -it centos /bin/bash 5f97269757c19434948b415f2fad13791783dec1d59f4a7b375b98d826c70c71 ``` 注意:如果创建容器命令报错"WARNING: IPv4 forwarding is disabled. Networking will not work. ",就使用vi编辑器打开/etc/sysctl.conf文件,在其中添加net.ipv4.ip_forward=1,然后使用sysctl -p命令加载配置。 **\[root@localhost \~\]# vim /etc/sysctl.conf** **net.ipv4.ip_forward = 1** **\[root@localhost \~\]# sysctl -p** #### 2、查看运行状态 ```bash [root@docker ~] docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5f97269757c1 centos "/bin/bash" About a minute ago Created kickass_bassi ``` #### 3、容器启动与停止 ```bash #启动 [root@docker ~] docker start 5f 5f [root@docker ~] docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5f97269757c1 centos "/bin/bash" 2 minutes ago Up 7 seconds kickass_bassi #停止 [root@docker ~] docker stop 5f 5f [root@docker ~] docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ``` #### 4、容器运行命令 ```bash [root@docker ~] docker run centos /bin/bash -c ls / bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var ```