概念
Docker镜像:Docker Images,每一个镜像都可能依赖一个或多个下层的镜像组成的另一个镜像,AUFS文件系统。
Docker仓库:Docker Registry,集中存放镜像的地方。
Docker容器:Docker Containers,镜像运行后的进程。
Docker与虚拟机的区别
- 容器与容器之间只是进程的隔离,而虚拟机是完全的资源隔离
- 虚拟机的启动可能需要分钟级别,Docker启动是秒级或者更短
- 容器使用宿主操作系统的内核,而虚拟机使用完全独立的内核
docker中文参考手册:https://www.docker.tips/
一、Docker安装与配置
1.1、windows10 环境下的安装与配置
系统要求:不支持 Windows10 家庭版操作系统
下载文件后,以管理员的身份运行Docker Desktop Installer.exe文件,进行安装。
1.2、Linux 环境下的安装与配置
Centos版本安装与配置
查看发行版本信息:
[root@VM_0_8_centos ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
1.2.1、docker安装
方式一
1、安装依赖:yum install -y yum-utils device-mapper-persistent-data lvm2
2、添加源:yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3、查看可安装的版本:yum list docker-ce --showduplicates | sort -r
4、安装Docker最新社区版:yum install -y docker-ce
方式二
1、下载docker安装脚本:curl -fsSL get.docker.com -o get-docker.sh
2、执行安装脚本:sh get-docker.sh
启动查询命令
Centos版本安装后,默认是不启动docker的,需要手动命令启动服务。
1、启动docker:systemctl start docker
2、开机自启docker:systemctl enable docker
3、查看运行的容器:docker ps
4、查看版本信息:docker version
1.2.2、删除老版本服务器步骤
1、停止docker服务:systemctl stop docker
2、查看当前版本安装的包:rpm -qa | grep docker
3、删除旧版本的包:yum remove docker docker-client docker-common
4、删除相关的文件:
find /etc/systemd -name '*docker*' -exec rm -f {
} \;
find /etc/systemd -name '*docker*' -exec rm -f {
} \;
find /lib/systemd -name '*docker*' -exec rm -f {
} \;
rm -rf /var/lib/docker #删除以前已有的镜像和容器,非必要
rm -rf /var/run/docker
5、注意:删除文件报错
最好在停止Docker 服务前先删除掉所有的容器;否则在删除文件过程中,可能会报错。
docker 容器运行过程中,会绑定系统的一些目录用于存储;直接停止Docker 服务,则这些绑定目录没有释放掉;删除文件时会提示“文件使用中”。
docker 服务运行中也会加载存储驱动,网络驱动(docker0网卡),这些设置可能也存在删除不掉的情况。
彻底的解决方式:重启系统。
1.3、镜像加速器
Centos
配置步骤
1、切换目录:cd /etc/docker
2、创建文件并写入内容:vim daemon.json
注意:云服务器在那个地方,就使用那个公司的加速器,一般加一个加速器就可以了。
{
"registry-mirrors": ["https://hkgournx.mirror.aliyuncs.com", "https://mirror.ccs.tencentyun.com"]
}
3、重启docker服务:sudo systemctl restart docker
4、配置完成后测试一下速度:sudo docker pull nginx
二、docker常用命令
2.1、基本命令
1、Docker版本信息: docker version
2、Docker系统信息:docker info
docker命令行的基本使用:
docker + 管理的对象(比如容器,镜像) + 具体操作(比如创建,启动,停止,删除)
示例
- 拉取一个叫nginx的docker image镜像:
docker image pull nginx
- 停止一个叫web的docker container容器:
docker container stop web
通过 docker + 管理命令 + --help,查看具体的命令
2.2、镜像管理
1、查看所有镜像:docker images
[root@VM_0_8_centos docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f0b8a9a54136 13 days ago 133MB
REPOSITORY: 表示镜像的仓库源
TAG: 镜像的标签,版本信息
IMAGE ID:镜像id,全局唯一
CREATED:镜像版本的发布时间
SIZE:镜像文件大小
2、搜索镜像:docker search 镜像名称
[root@VM_0_8_centos docker]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 14911 [OK]
OFFICIAL:是否是官方的镜像
3、拉取下载:docker pull 镜像名称[:版本信息]
版本信息可选,不指定默认选择最新版本
[root@VM_0_8_centos docker]# docker pull nginx <== 拉取最新版本的nginx下载
Using default tag: latest
latest: Pulling from library/nginx
Digest: sha256:df13abe416e37eb3db4722840dd479b00ba193ac6606e7902331dcea50f4f1f2
Status: Image is up to date for nginx:latest
docker.io/library/nginx:latest
[root@VM_0_8_centos docker]# docker pull nginx:1.18.0 < 指定nginx版本拉取下载
1.18.0: Pulling from library/nginx
f7ec5a41d630: Pull complete
0b20d28b5eb3: Pull complete
1576642c9776: Pull complete
c12a848bad84: Pull complete
03f221d9cf00: Pull complete
Digest: sha256:e90ac5331fe095cea01b121a3627174b2e33e06e83720e9a934c7b8ccc9c55a0
Status: Downloaded newer image for nginx:1.18.0
docker.io/library/nginx:1.18.0
4、导出:docker save 镜像名称[:版本信息] > 镜像名称.tar
[root@VM_0_8_centos ~]# docker save nginx:1.18.0 > nginx_1_18_0.tar
[root@VM_0_8_centos ~]# ll -h nginx_1_18_0.tar
-rw-r--r-- 1 root root 131M May 26 09:52 nginx_1_18_0.tar
5、删除:docker rmi 镜像名称[:版本信息] | 镜像id
不指定版本信息,只删除最新版本的镜像文件
[root@VM_0_8_centos ~]# docker rmi nginx <== 删除最新版本的nginx
Untagged: nginx:latest
...
[root@VM_0_8_centos ~]# docker rmi nginx:1.18.0 <== 删除1.18.0 版本的 nginx
Untagged: nginx:1.18.0
...
[root@VM_0_8_centos ~]# docker rmi c2c45d506085 <== 指定IMAGE ID删除
Untagged: nginx:1.18.0
...
6、导入:docker load < 文件名
[root@VM_0_8_centos ~]# docker load < nginx_1_18_0.tar
7e718b9c0c8c: Loading layer 72.52MB/72.52MB
f3fdf88f1cb7: Loading layer 64.6MB/64.6MB
6fcbf7acaafd: Loading layer 3.072kB/3.072kB
4fe7d87c8e14: Loading layer 4.096kB/4.096kB
4fa6704c8474: Loading layer 3.584kB/3.584kB
Loaded image: nginx:1.18.0
[root@VM_0_8_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.18.0 c2c45d506085 6 weeks ago 133MB
7、更改镜像名:docker tag 镜像名:旧TAG名称 镜像名:新TAG名称
[root@VM_0_8_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest d1a364dc548d 10 hours ago 133MB
[root@VM_0_8_centos ~]# docker tag nginx:latest nginx:1.19.10 <== 更改镜像名
[root@VM_0_8_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.19.10 d1a364dc548d 10 hours ago 133MB
nginx latest d1a364dc548d 10 hours ago 133MB
8、查看镜像创建历史:docker history 镜像名称
[root@VM_0_8_centos ~]# docker history nginx
IMAGE CREATED CREATED BY SIZE COMMENT
d1a364dc548d 10 hours ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B
<missing> 10 hours ago /bin/sh -c #(nop) STOPSIGNAL SIGQUIT 0B
<missing> 10 hours ago /bin/sh -c #(nop) EXPOSE 80 0B
...
9、删除所有镜像:docker image pruse -a
2.3、容器管理
1、运行容器:docker run -d --name 镜像名称 镜像名称:TAG ping ip
-d: 后台运行,–name:给启动的容器指定名称,ping ip:容器运行的指令
[root@VM_0_8_centos ~]# docker run -d --name busybox busybox:latest ping 114.114.114.114
3d2b701c841203781beaa1cb8dc0acca91a8a596e08776a8b3f6dc98430482f9
[root@VM_0_8_centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3d2b701c8412 busybox:latest "ping 114.114.11