Docker
文档链接:https://pan.baidu.com/s/1kKkXuW5uOehC3E2iylrwGA 提取码:wy47
建议用谷歌浏览器,自带英文翻译功能
进入docker官方文档:https://docs.docker.com
进入docker中心(用于Docker社区查找和共享容器映像):https://hub.docker.com/search?q=&type=image
选择下载与安装,然后选择自己要想要安装的系统
本文档选择的是linux,CentOs7版本
选择Linux后在左侧目录中的installation per distro (按发行版安装)中选择相应镜像要安装的版本
可根据自我情况选择依照官方文档提示安装,也可以观看此文档进行安装与使用
一、安装与启动
1.卸载旧版本
yum remove docker
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
2.需要的安装包
yum install -y yum-utils
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 #阿里云的(推荐)
4、更新yum软件包索引
yum makecache fast
yum makecache fast
5、安装docker
yum install docker-ce docker-ce-cli containerd.io
-
#安装docker docker ce社区版 ee企业版 yum install docker-ce docker-ce-cli containerd.io [root@localhost /]# yum install docker-ce docker-ce-cli containerd.io Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Package 3:docker-ce-19.03.12-3.el7.x86_64 already installed and latest version Package 1:docker-ce-cli-19.03.12-3.el7.x86_64 already installed and latest version Package containerd.io-1.2.13-3.2.el7.x86_64 already installed and latest version Nothing to do
我之前已经安装,才如此显示
若想安装其他版本(默认安装时最新版本),可用以下代码**
yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
版本安装格式在官网文档此行代码上有,可以自行查看
6、启动docker
systemctl start docker
systemctl start docker
#检查docker是否安装成功
docker version
#查看docker版本
docker -v
7、通过运行hello-world
映像来验证是否正确安装了Docker Engine
[root@localhost docker]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:49a1c8800c94df04e9658809b006fd8a686cab8028d33cfba2cc049724254202
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、查看一下已有的镜像
docker images
docker images
REPOSITORY TAG IMAGE ID CREATED
hello-world latest bf756fb1ae65 7 months ago
9、了解:卸载docker
#1 卸载依赖
yum remove docker-ce docker-ce-cli containerd.io
#2 删除资源
rm -rf /var/lib/docker
# /var/lib/docker docker的默认工作路径
二、设置阿里云的镜像加速
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://x2gdib5d.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
三、Docker的启动与停止
systemctl命令是系统服务管理器指令
启动docker:
systemctl start docker
停止docker:
systemctl stop docker
重启docker:
systemctl restart docker
查看docker状态:
systemctl status docker
开机启动:
systemctl enable docker
查看docker概要信息:
docker info
查看docker帮助文档
docker --help
帮助文档的地址https://docs.docker.com/reference/
四、Docker的常用命令
镜像命令
1.查看所有本地的主机上的镜像
docker images
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 7 months ago 13.3kB
#解释
REPOSITORY 镜像名称
TAG 镜像标签
IMAGE ID 镜像ID
CREATED 镜像的创建日期(不是获取该镜像的日期)
SIZE 镜像大小
这些镜像都是存储在Docker宿主机的/var/lib/docker目录下
#可选项 docker images --help
-a, --all #列出所有镜像
-q, --quiet #只显示镜像id
2.搜索镜像
docker search
[root@localhost ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 9793 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3574
#解释
NAME 仓库名称
DESCRIPTION 镜像描述
STARS 用户评价,反应一个镜像的受欢迎程度
OFFICIAL 是否官方
AUTOMATED 自动构建,表示该镜像由Docker Hub自动构建流程创建的
#可选项 docker search --help
--filter filter
eg:
--filter=STARS=5000 #可以通过搜索来过滤 #搜索出来的镜像就是STARS大于5000的
[root@localhost ~]# docker search mysql --filter=STARS=5000
NAME DESCRIPTION STARS OFFICI AL AUTOMATED
mysql MySQL is a widely used, open-source relation… 9793 [OK]
3.下载镜像
docker pull
#下载镜像 docker pull 镜像名[:tag]
[root@localhost ~]# docker pull mysql
Using default tag: latest #如果不写tag,默认就是latest
latest: Pulling from library/mysql
6ec8c9369e08: Pull complete #分层下载,docker images的核心 联合文件系统
177e5de89054: Pull complete
ab6ccb86eb40: Pull complete
e1ee78841235: Pull complete
09cd86ccee56: Pull complete
78bea0594a44: Pull complete
caf5f529ae89: Pull complete
cf0fc09f046d: Pull complete
4ccd5b05a8f6: Pull complete
76d29d8de5d4: Pull complete
8077a91f5d16: Pull complete
922753e827ec: Pull complete
Digest: sha256:fb6a6a26111ba75f9e8487db639bc5721d4431beba4cd668a4e922b8f8b14acc #签名 防伪标志
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实地址
#等价
docker pull mysql
docker docker.io/library/mysql:latest
#指定版本下载
[root@localhost ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
6ec8c9369e08: Already exists
177e5de89054: Already exists
ab6ccb86eb40: Already exists
e1ee78841235: Already exists
09cd86ccee56: Already exists
78bea0594a44: Already exists
caf5f529ae89: Already exists
4e54a8bcf566: Pull complete
50c21ba6527b: Pull complete
68e74bb27b39: Pull complete
5f13eadfe747: Pull complete
Digest: sha256:97869b42772dac5b767f4e4692434fbd5e6b86bcb8695d4feafb52b59fe9ae24
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
4.删除镜像
docker rmi
#按镜像ID删除镜像
docker rmi 镜像ID
或
docker rmi -f 镜像ID
eg:
[root@localhost ~]# docker rmi 8679ced16d20
Untagged: mysql:5.7
Untagged: mysql@sha256:97869b42772dac5b767f4e4692434fbd5e6b86bcb8695d4feafb52b59fe9ae24
Deleted: sha256:8679ced16d206961b35686895b06cfafefde87ef02b518dfc2133081ebf47cda
Deleted: sha256:355f87dc5125a32cc35898a4dde17fb067585bc0d86704b5a467c0ccc0eea484
Deleted: sha256:8299d5c38042216210125535adb2600e46268a0e2b9ec799d12ea5b770236e79
Deleted: sha256:07311a303b2c7cf2ac6992aaf68e12326fe7255985166939cbab7d18b10e0f47
Deleted: sha256:306c9bc1ce2997d000bb6f1ea4108420d9752df93ce39164b7a2f876b954afc4
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest e3fcc9e1cc04 11 days ago 544MB
hello-world latest bf756fb1ae65 7 months ago 13.3kB
#删除命令
docker rmi -f 镜像id #删除指定镜像
docker rmi -f 镜像id 镜像id 镜像id 镜像id #删除多个镜像
docker rmi -f $(docker images -aq) #删除所有镜像
容器命令
说明:我们有了镜像才能创建容器,linux下载一个centos镜像来测试学习
docker pull centos
1、新建容器并启动
docker run [可选参数] image
#参数说明
--name="Name" 容器名字 tomcat1 tomcat2,用来区分容器
-d 后台方式运行
-it 使用交互式运行,进入容器查看内容
-p (小p) 指定容器的端口,-p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口 (常用)
-p 容器端口
容器端口
-P(大p) 随机指定端口
#测试 启动并进入容器
docker run -it --name=容器名称 镜像名称:标签 /bin/bash #交互式方式创建容器
[root@localhost ~]# docker run -it centos /bin/bash
我连接的时候出现以下问题
WARNING: IPv4 forwarding is disabled. Networking will not work.
会导致本地连接不上docker上的容器
解决办法:
vi /etc/sysctl.conf
net.ipv4.ip_forward=1 #添加这段代码
#重启network服务
systemctl restart network && systemctl restart docker
#查看是否修改成功 (备注:返回1,就是成功)
[root@docker-node2 ~]# sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 1
#正常进入容器
[root@localhost ~]# docker run -it centos /bin/bash
[root@f9727fa537a4 /]#
#接着测试
[root@538b7de76fc5 /]# ls #查看容器内的centos 基础版本很多命令是不完善的
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
2、从容器中退回主机
(1)exit 停止并退出容器
(2)Ctrl + p + q 不停止退出容器
#exit 直接停止并退出容器
[root@538b7de76fc5 /]# exit
exit
[root@localhost ~]#
#Ctrl + p + q 不停止退出容器
[root@localhost ~]# docker run -it centos /bin/bash
[root@d5da10017431 /]# [root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
d5da10017431 centos "/bin/bash" 31 seconds ago Up 30 seconds
3、列出所有在运行的容器
docker ps
#docker ps 命令
#列出当前正在运行的容器
-a #列出当前正在运行的容器+带出历史运行过的容器
-n=? #显示最近创建的容器
-q #只显示容器编号
#查看所有在运行的容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#查看之前运行过的容器
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
538b7de76fc5 centos "/bin/bash" 11 minutes ago Exited (130) 5 minutes ago unruffled_nash
f9727fa537a4 centos "/bin/bash" 18 minutes ago Exited (127) 12 minutes ago charming_fermi
4c7dced39dee centos "/bin/bash" 23 minutes ago Exited (127) 23 minutes ago sad_swirles
c135f7f0035b centos "/bin/bash" 24 minutes ago Exited (1) 23 minutes ago suspicious_lehmann
c6d859e55086 centos "/bin/bash" 49 minutes ago Exited (130) 25 minutes ago bold_leavitt
fd897408a819 centos "/bin/bash" 50 minutes ago Exited (130) 49 minutes ago dazzling_hamilton
c4f5c13879b7 bf756fb1ae65 "/hello" 6 hours ago Exited (0) 6 hours ago pedantic_chatelet
#显示最近创建的2个容器
[root@localhost ~]# docker ps -a -n=2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
538b7de76fc5 centos "/bin/bash" 21 minutes ago Exited (130) 15 minutes ago unruffled_nash
f9727fa537a4 centos "/bin/bash" 28 minutes ago Exited (127) 22 minutes ago charming_fermi
#只显示容器的编号
[root@localhost ~]# docker ps -aq
538b7de76fc5
f9727fa537a4
4c7dced39dee
c135f7f0035b
c6d859e55086
fd897408a819
c4f5c13879b7
4、删除容器
docker rm 容器id #删除指定的容器 不能删除正在运行的容器 如果要强制删除 rm -f
docker rm -f $(docker ps -aq) #删除所有容器
docker ps -a -q|xargs docker rm #删除所有容器
5、启动并停止容器
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止容器
docker kill 容器id #强制停止当前容器
6、后台启动容器
docker run -d 镜像名
#docker run - d 镜像名
[root@localhost ~]# docker run -d centos /bin/bash
df2f98023927ae83d3da95e83c7941ff83f6a4b7a73ccee8b5b42e3dd11b9c25
#问题docker ps,发现centos 停止了
#常见的坑,docker容器使用后台运行,就必须有一个前台进程,docker发现没有应用,就会自动停止
#nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序
7、查看日志
docker logs
docker logs -f -t --tail [number] 容器 没有日志
#自己编写一段shell日志
[root@localhost ~]# docker run -d centos /bin/sh -c "while true;do echo Santa;sleep 1;done"
e9cc823b5a6732a77159e7fb8ce56e97baa01d7a4cc81e927ad4beb20c62d34d
#查看
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e9cc823b5a67 centos "/bin/sh -c 'while t…" 28 seconds ago Up 27 seconds brave_liskov
#查看日志
[root@localhost ~]# docker logs -ft --tail 10 e9cc823b5a67
2020-08-03T09:26:01.342380023Z Santa
2020-08-03T09:26:02.345681136Z Santa
2020-08-03T09:26:03.350124251Z Santa
2020-08-03T09:26:04.353270896Z Santa
2020-08-03T09:26:05.356494066Z Santa
2020-08-03T09:26:06.359486117Z Santa
2020-08-03T09:26:07.361925694Z Santa
2020-08-03T09:26:08.364800919Z Santa
2020-08-03T09:26:09.367819068Z Santa
2020-08-03T09:26:10.370290437Z Santa
2020-08-03T09:26:11.372604642Z Santa
2020-08-03T09:26:12.374797002Z Santa
2020-08-03T09:26:13.377226865Z Santa
2020-08-03T09:26:14.379389029Z Santa
2020-08-03T09:26:15.382546465Z Santa
2020-08-03T09:26:16.386634389Z Santa
2020-08-03T09:26:17.390531223Z Santa
2020-08-03T09:26:18.393130983Z Santa
#显示日志
-ft #显示日志
--tail #要显示日志条数
[root@localhost ~