Docker
Docker
1 Docker的安装
1.1 检查安装环境
环境查看
uname -r
#docker支持的系统 centos7+
[root@root ~]# uname -r
4.18.0-147.5.1.el8_1.x86_64
1.2 安装Docker Server
#1.卸载旧版本
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
#2.设置Docker存储库
yum install -y yum-utils
#3.设置镜像的仓库
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo #国外
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #阿里云镜像(推荐)
#更新yum软件包索引
yum makecache fast
#4.安装docker
#docker-ce 社区 ee 企业版
yum install docker-ce docker-ce-cli containerd.io
#4.1安装低版本docker
#4.1.1检查相应的软件包版本
yum list docker-ce --showduplicates | sort -r
#4.1.2 安装对应的包 安装低版本的软件包
yum -y install docker-ce-18.06.0.ce-3.el7
#5.启动docker
systemctl start docker
# 6.判断是否安装成功
docker version
[root@root ~]# docker version
Client:
Version: 18.06.0-ce
API version: 1.38
Go version: go1.10.3
Git commit: 0ffa825
Built: Wed Jul 18 19:08:18 2018
OS/Arch: linux/amd64
Experimental: false
Server:
Engine:
Version: 18.06.0-ce
API version: 1.38 (minimum version 1.12)
Go version: go1.10.3
Git commit: 0ffa825
Built: Wed Jul 18 19:10:42 2018
OS/Arch: linux/amd64
Experimental: false
# 7.运行命令测试
docker run hello-world
[root@root ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
#8.查看下载的hello-world镜像
[root@root ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 5 months ago
docker run 运行流程图
1.3 卸载Docker
#卸载依赖
yum remove docker-ce docker-ce-cli containerd.io
#删除目录
rm -rf /var/lib/docker
docker的默认配置文件
2 底层原理
docker 是一个Client-Server结构的系统,Docker的守护进程运行在主机上,通过Docker从客户端访问
DockerServer接收到Docker-Client的指令,就会执行这个命令!
3 Docker为什么比VM快?
1.Docker有着比虚拟机更少的抽象层
2.docker利用的是宿主机的内核,vm需要是Guest OS
所以说,新建一个容器的时候,docker不需要像虚拟机一样重新加载一个操作系统内核,避免引导。虚拟机试加载Guest OS, 分钟级别的,而Docker是李永宿主机的操作系统,省略了这个复杂的过程,秒级!
镜像命令
docker images
查看所有本地的主机上的镜像
[root@root ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 5 months ago 13.3kB
#解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATED 镜像的创建时间
SIZE 镜像的大小
docker hub
查询docker镜像https://hub.docker.com/
docker ps
# 可选项
-a , --all #列出所有镜像
-q , --quiet #只显示镜像的ID
[root@root ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6ff560abc065 hello-world "/hello" About an hour ago Exited (0) About an hour ago keen_goodall
086734d3fed6 hello-world "/hello" 21 hours ago Exited (0) 21 hours ago inspiring_shirley
docker search
搜索镜像
[root@root ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 9645 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3506 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 704 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 77
# 可选项,通过搜索来过滤
--filter=STARS=3000 #搜索出来的镜像就是STARS大于3000的
[root@root ~]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 9645 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3506 [OK]
docker pull
下载镜像
#下载镜像 docker pull 镜像名 [:tag]
[root@root ~]# docker pull mysql
Using default tag: latest #如果不指定版本默认下载最新版本
latest: Pulling from library/mysql
8559a31e96f4: Pull complete #分层下载,docker image的核心 联合文件系统
d51ce1c2e575: Pull complete
c2344adc4858: Pull complete
fcf3ceff18fc: Pull complete
16da0c38dc5b: Pull complete
b905d1797e97: Pull complete
4b50d1c6b05c: Pull complete
c75914a65ca2: Pull complete
1ae8042bdd09: Pull complete
453ac13c00a3: Pull complete
9e680cd72f08: Pull complete
a6b5dc864b6c: Pull complete
Digest: sha256:8b7b328a7ff6de46ef96bcf83af048cb00a1c86282bfca0cb119c84568b4caf6 #签名
Status: Downloaded newer image for mysql:latest
# 等价于
docker pull mysql
docker pull docker.io/library/mysql:latest
# 指定版本下载
[root@root ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
8559a31e96f4: Already exists
d51ce1c2e575: Already exists
c2344adc4858: Already exists
fcf3ceff18fc: Already exists
16da0c38dc5b: Already exists
b905d1797e97: Already exists
4b50d1c6b05c: Already exists
d85174a87144: Pull complete
a4ad33703fa8: Pull complete
f7a5433ce20d: Pull complete
3dcd2a278b4a: Pull complete
Digest: sha256:32f9d9a069f7a735e28fd44ea944d53c61f990ba71460c5c183e610854ca4854
Status: Downloaded newer image for mysql:5.7
#查看已下载镜像
[root@root ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 9cfcce23593a 9 days ago 448MB
mysql latest be0dbf01a0f3 9 days ago 541MB
hello-world latest bf756fb1ae65 5 months ago 13.3kB
docker rmi
删除镜像
[root@root ~]# docker rmi -f 镜像ID #删除指定的镜像
[root@root ~]# docker rmi -f 镜像ID 镜像ID 镜像ID 镜像ID #删除多个镜像
[root@root ~]# docker rmi -f $(docker images -aq) #删除所有镜像
容器命令
说明:我们有了镜像才可以创建容器,linux,下载一个centos镜像来测试学习
docker pull centos
新建容器并启动
docker run [可选参数] image
#参数说明
--name="Name" #容器名字 tomcat01 tomcat02 用来区分容器
-d #后台方式运行
-it #使用交互方式运行,进入容器查看内容
-p #指定容器的端口 -p 8080:8080(映射端口)
-p #ip:主机端口:容器端口
-p #主机端口:容器端口(常用)
-p #容器端口
#测试,启动并进入容器
[root@root ~]# docker run -it centos /bin/bash
[root@3810abcc6666 /]# ls #查看容器内的centos,基础版本,很多命令都是不完善的
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
#从容器中返回主机
[root@3810abcc6666 /]# exit
exit
列出所有运行的容器
#docker ps 命令
#列出当前正在运行的容器
-a #列出当前正在运行的容器+带出历史运行过的容器
-n=? #显示最近创建的容器
-q #只显示容器的编号
[root@root /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@root /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3810abcc6666 centos "/bin/bash" 2 hours ago Exited (0) 2 hours ago unruffled_babbage
6ff560abc065 bf756fb1ae65 "/hello" 4 hours ago Exited (0) 4 hours ago keen_goodall
086734d3fed6 bf756fb1ae65 "/hello" 24 hours ago Exited (0) 24 hours ago inspiring_shirley
退出容器
exit #退出,容器停止运行
Ctrl + P + Q #退出,容器不停止运行
删除容器
docker rm 容器ID #删除指定的容器,不能删除正在运行的容器,如果需要强制删除 rm -f
docker rm -f $(docker ps -aq) #删除所有的容器
docker ps -a -q|xargs docker rm #删除所有的容器
启动和停止容器的操作
docker start 容器ID #启动容器
docker restart 容器ID #重启容器
docker stop 容器ID #停止当前正在运行的容器
docker kill 容器ID #强制停止当前容器
常用命令
后台启动容器
# 命令 docker run -d 镜像名
[root@root /]# docker run -d centos
b0f32a8a488551c52390c871a4a95e976e02b61b024483ee2bed4f76f56428ef
#问题docker ps,发现centos 停止了
#常见的坑,docker 容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
#nginx 容器启动后 ,发现自己没有提供服务,就会立刻停止,就是没有进程了
查看日志
docker logs -f -t --tail 容器,没有日志
#自己编写一段shell脚本
[root@root /]# docker run -d centos /bin/sh -c "while true;do echo kuangshen;sleep 1;done"
78383dd4431e06db38dcec1997d9913ec767da318cc8a74338cb4f469bb8773f
[root@root /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
78383dd4431e centos "/bin/sh -c 'while t…" 14 seconds ago Up 13 seconds kind_jackson
#显示日志
-tf #显示日志
--tail number #要显示日志条数
[root@root /]# docker logs -tf --tail 10 78383dd4431e
查看容器中进程信息
# 命令 docker top 容器ID
[root@root /]# docker top 78383dd4431e
UID PID PPID C STIME TTY TIME CMD
root 29986 29973 0 14:25 ? 00:00:00 /bin/sh -c while true;do echo kuangshen;sleep 1;done
root 30220 29986 0 14:28 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
进入当前正在运行的容器
# 我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置
#命令
docker exec -it 容器ID bashShell
#测试
[root@root /]# docker exec -it 78383dd4431e /bin/bash
[root@78383dd4431e /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@78383dd4431e /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 06:25 ? 00:00:00 /bin/sh -c while true;do echo kuangshen;sleep 1;done
root 648 0 0 06:35 pts/0 00:00:00 /bin/bash
root 2343 1 0 07:03 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root 2344 648 0 07:03 pts/0 00:00:00 ps -ef
#方式二
docker attach 容器ID
[root@root /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
027e5f0cc694 centos "/bin/bash" 21 seconds ago Up 19 seconds brave_shirley
[root@root /]# docker attach 027e5f0cc694
[root@027e5f0cc694 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
docker exec # 进入容器后开启一个新的终端,可以在里面操作(正常)
docker attach # 进入容器正在执行的终端,不会启动新的进程
从容器内拷贝文件到主机
docker cp 容器ID:容器内路径 目的地主机路径
#查看当前主机目录下
[root@root home]# ls
log micro mysql-community-release-el7-5.noarch.rpm nginx-1.18.0 nginx-1.18.0.tar.gz tomcat www
[root@root home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
027e5f0cc694 centos "/bin/bash" 7 minutes ago Up About a minute brave_shirley
#进入docker容器内部
[root@root home]# docker attach 027e5f0cc694
[root@027e5f0cc694 /]# ls /home/
[root@027e5f0cc694 /]#
#在容器中新建一个文件
[root@027e5f0cc694 /]# cd /home/
[root@027e5f0cc694 home]# ll
bash: ll: command not found
[root@027e5f0cc694 home]# touch test.java
[root@027e5f0cc694 home]# exit
exit
#将这个文件拷贝出来到主机上
[root@root home]# docker cp 027e5f0cc694:/home/test.java /home
[root@root home]# ls
log micro mysql-community-release-el7-5.noarch.rpm nginx-1.18.0 nginx-1.18.0.tar.gz test.java tomcat www
#拷贝是一个手动过程,未来我们使用 -V 卷的技术,可以实现,自动同步 /home /home
小结
使用遭遇
1.1 更新yum软件包索引
#执行命令 更新yum软件包索引
yum makecache fast
#异常:
usage: yum makecache [-c [config file]] [-q] [-v] [--version]
[--installroot [path]] [--nodocs] [--noplugins]
[--enableplugin [plugin]] [--disableplugin [plugin]]
[--releasever RELEASEVER] [--setopt SETOPTS]
[--skip-broken] [-h] [--allowerasing] [-b | --nobest]
[-C] [-R [minutes]] [-d [debug level]] [--debugsolver]
[--showduplicates] [-e ERRORLEVEL] [--obsoletes]
[--rpmverbosity [debug level name]] [-y] [--assumeno]
[--enablerepo [repo]] [--disablerepo [repo] | --repo
[repo]] [--enable | --disable] [-x [package]]
[--disableexcludes [repo]] [--repofrompath [repo,path]]
[--noautoremove] [--nogpgcheck] [--color COLOR]
[--refresh] [-4] [-6] [--destdir DESTDIR]
[--downloadonly] [--comment COMMENT] [--bugfix]
[--enhancement] [--newpackage] [--security]
[--advisory ADVISORY] [--bz BUGZILLA] [--cve CVES]
[--sec-severity {Critical,Important,Moderate,Low}]
[--forcearch ARCH] [--timer]
yum makecache: error: argument timer: invalid choice: 'fast' (choose from 'timer')
1.2 docker安装命令
# 执行命令 docker安装命令
yum install docker-ce docker-ce-cli containerd.io
# 异常:
Last metadata expiration check: 0:04:20 ago on Thu 18 Jun 2020 01:15:20 PM CST.
Error:
Problem: package docker-ce-3:19.03.11-3.el7.x86_64 requires containerd.io >= 1.2.2-3, but none of the providers can be installed
- cannot install the best candidate for the job
- package containerd.io-1.2.10-3.2.el7.x86_64 is excluded
- package containerd.io-1.2.13-3.1.el7.x86_64 is excluded
- package containerd.io-1.2.13-3.2.el7.x86_64 is excluded
- package containerd.io-1.2.2-3.3.el7.x86_64 is excluded
- package containerd.io-1.2.2-3.el7.x86_64 is excluded
- package containerd.io-1.2.4-3.1.el7.x86_64 is excluded
- package containerd.io-1.2.5-3.1.el7.x86_64 is excluded
- package containerd.io-1.2.6-3.3.el7.x86_64 is excluded
(try to add '--skip-broken' to skip uninstallable packages or '--nobest' to use not only best candidate packages)
#原因:
检查发现containerd.io此软件包已经安装,但版本是1.2.0;
所以造成这种安装失败的原因是containerd.io软件包版本过低
# 解决
安装低版本的docker镜像