说明:
Docker是容器,功能类似vm虚拟机。
安装在CentOS7系统上。
一、安装:
1.通过yum在线安装:
[root@localhost ~]# yum install docker
2.查看Docker版本:
[root@localhost ~]# docker -v
二、启动/停止Docker:
1.启动:
[root@localhost ~]# systemctl start docker
2.停止:
[root@localhost ~]# systemctl stop docker
3.重启:
[root@localhost ~]# systemctl restart docker
4.查看启动状态:
[root@localhost ~]# systemctl status docker
5.设Docker为开机启动:
[root@localhost ~]# systemctl enable docker
6.查看信息:
[root@localhost ~]# docker info
7.查看帮助:
[root@localhost ~]# docker --help
三、镜像操作:
1.查看本地镜像列表:
[root@localhost ~]# docker images
2.从网络搜索镜像(格式:docker search 名称),首选安装最顶部的:
[root@localhost ~]# docker search centos
3.设置使用ustc镜像仓库(下载速度快):
编辑daemon.json文件:
[root@localhost ~]# vi /etc/docker/daemon.json
增加如下内容,保存退出:
{
"registry-mirrors": ["https://9cpn8tt6.mirror.aliyuncs.com","https://hub-mirror.c.163.com","https://mirror.ccs.tencentyun.com","https://reg-mirror.qiniu.com","https://docker.mirrors.ustc.edu.cn","https://dockerhub.azk8s.cn","http://f1361db2.m.daocloud.io"]
}
4.从网络下载镜像(格式:docker pull 名称):
[root@localhost ~]# docker pull centos
5.删除本地镜像:
(1)删除指定,后面为某镜像的IMAGE_ID值:
[root@localhost ~]# docker rmi 470671670cac
(2)删除所有:
[root@localhost ~]# docker rmi `docker images -q`
四、容器操作:
1.查看容器:
(1)查看正在运行的容器:
[root@localhost ~]# docker ps
(2)查看所有容器:
[root@localhost ~]# docker ps -a
(3)查看最后一次运行的容器:
[root@localhost ~]# docker ps -l
(4)查看停止运行的容器:
[root@localhost ~]# docker ps -f status=exited
2.创建并启动容器:
说明:
-i:必须,运行容器
-t:容器运行后,登录并进入命令行
--name=名称:容器名称
-v:将docker指定目录与容器内指定目录建立映射关系,此目录为共享目录,可通过此目录实现2个系统间的文件转移。
-d:创建守护式后台容器,加此参数不会登录进入命令行
-p:端口映射,将容器端口和CentOS7端口建立映射
--restart=always:设置容器随Docker启动而启动
(1)创建并启动交互式容器(登录并进入命令行),centos为镜像名称,latest为TAG值,/bin/bash为进入centos命令行参数:
[root@localhost ~]# docker run -it --name=centos1 centos:latest /bin/bash
(2)创建并启动守护式后台容器:
[root@localhost ~]# docker run -id --name=centos2 centos:latest
(3)启动已创建的容器:
[root@localhost ~]# docker start centos1
(4)设置容器随Docker启动而启动:
[root@localhost ~]# docker update --restart=always centos1
3.进入已启动容器的命令行:
[root@localhost ~]# docker exec -it centos2 /bin/bash
4.退出登录、停止容器:
退出,在容器命令行中输入:
[root@0871899e446b /]# exit
停止:
[root@localhost ~]# docker stop centos1
5.文件拷贝:
(1)将文件从宿主机系统中拷贝到容器内(当前宿主机系统目录为root,文件位置/root/file1):
格式:docker cp 待拷贝文件或目录 容器名称:存放目录
[root@localhost ~]# docker cp file1 centos1:/root
(2)将文件从容器内拷出到宿主机系统(当前宿主机目录为root,拷出到的位置为/root/file1):
格式:docker cp 容器名称:待拷贝文件或目录绝对路径 文件或目录名
[root@localhost ~]# docker cp centos1:/root/file1 file1
6.目录挂载(将宿主机系统与容器内指定目录建立映射关系,此目录为共享目录,可通过此目录实现2个系统间的文件转移,—privileged=true解决权限问题):
格式:docker run -di -v docker目录:容器目录 --privileged=true 容器名:TAG值
[root@localhost ~]# docker run -di -v /usr/local/a:/usr/local/b --name=centos3 --privileged=true centos:latest
7.查看容器IP:
[root@localhost ~]# docker inspect --format='{{.NetworkSettings.IPAddress}}' centos3
8.删除容器(只能删除停止的容器):
[root@localhost ~]# docker rm centos1
五、备份/恢复:
1.将容器保存为镜像:
格式:docker commit 容器名 镜像名
[root@localhost ~]# docker commit redis1 image_redis1
2.镜像备份/恢复:
(1)镜像备份:
格式:docker save -o 压缩包名称.tar 镜像的REPOSITORY值
[root@localhost ~]# docker save -o image_redis1.tar image_redis1
(2)镜像恢复:
[root@localhost ~]# docker load -i image_redis1.tar