查看当前镜像:
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@docker ~]#很明显docker是咱们新搭建的所以目前还没有镜像
1,搜索镜像:
[root@docker ~]# docker search centos
搜索镜像并过滤是官方的:
[root@docker ~]# docker search --filter 'is-official=true' centos
过滤星级镜像:
[root@docker ~]#例如过滤出大于10星级的镜像
[root@docker ~]# docker search --filter stars=10 centos
2,下载镜像:
[root@docker ~]# 例如下载centos7的镜像如果是版本6在冒号后面修改成6即可
[root@docker ~]# docker pull centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
[root@docker ~]#因为我前面已经部署了阿里云的镜像所以下载比较快,下载慢的话可以部署一下
下载好之后进行查看一下本地镜像:
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 eeb6ee3f44bd 16 months ago 204MB
[root@docker ~]#已经显示了
3,修改镜像名和版本标签:
[root@docker ~]#例如把centos修改为mycentos版本7修改成1
[root@docker ~]# docker tag centos:7 mycentos:1
进行查看:
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 eeb6ee3f44bd 16 months ago 204MB
mycentos 1 eeb6ee3f44bd 16 months ago 204MB
[root@docker ~]# 可以看到已经多出一条修改成功了但是id没变可以理解为重命名
4,删除镜像:
[root@docker ~]# docker rmi eeb6ee3f44bd
Error response from daemon: conflict: unable to delete eeb6ee3f44bd (must be forced) - image is referenced in multiple repositories
[root@docker ~]#像上述那样重命名一个有两个一样的id直接删除id会导致系统不知道要删除哪一个
可以直接指定名称删除:
[root@docker ~]# docker rmi centos:7
Untagged: centos:7
Untagged: centos@sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4
[root@docker ~]#
进行查看:(已经删除成功)
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 1 eeb6ee3f44bd 16 months ago 204MB
5,配置阿里云镜像加速:
进入阿里云官网:
https://cn.aliyun.com/
6,容器的构建基本操作
-i以交互模式去运行这个容器
-t表示为尾数表示一个输入终端
通常-i和t一起组合运行
[root@docker ~]# docker run -it eeb6ee3f44bd
[root@4f7f394c7b56 /]#
[root@4f7f394c7b56 /]#
[root@4f7f394c7b56 /]# exit
exit
[root@docker ~]#
查看运行的容器:
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker ~]#无容器运行
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4f7f394c7b56 eeb6ee3f44bd "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago pensive_chatterjee
[root@docker ~]#加上-a表示所有不管正在运行还是没有运行的都显示出来
启动运行的容器:
[root@docker ~]# docker run -itd centos:7
41e2126bc593bfd902080668294ef294301e81c3f2802518189983c1b6eacca4
[root@docker ~]#查看是否运行成功
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
41e2126bc593 centos:7 "/bin/bash" 34 seconds ago Up 34 seconds admiring_bohr
[root@docker ~]#运行成功
#注意如果上述运行centos:7的时候不加:7的执行结果为会从镜像里面拉去最新的下载执行
停止运行的容器:
[root@docker ~]# docker stop 41e21 #可以不输入全部ID前四五位即可
41e21
[root@docker ~]# docker ps #查看状态已经没有在运行
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker ~]#
#注意(还可以在启动时设置一个名字停止运行时执行名字即可如下)
[root@docker ~]# docker run -itd --name=mycentos centos:7 名字设置为mycentos
fd4223709f41cd45d19817c69b8dbac4b887fdfc3a04da332a6fe4ab04973d01
[root@docker ~]# docker ps 查看是否运行(运行中)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fd4223709f41 centos:7 "/bin/bash" 11 seconds ago Up 10 seconds mycentos
[root@docker ~]# docker stop mycentos 使用命名关闭容器
mycentos
[root@docker ~]# docker ps 查看是否运行(关闭状态)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker ~]# docker restart mycentos 对命名容器进行重启
mycentos
[root@docker ~]# docker ps 查看状态(启动成功运行中)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fd4223709f41 centos:7 "/bin/bash" 2 minutes ago Up 7 seconds mycentos
[root@docker ~]# docker ps -a 查看全部
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fd4223709f41 centos:7 "/bin/bash" 4 minutes ago Up 2 minutes mycentos
41e2126bc593 centos:7 "/bin/bash" 13 minutes ago Exited (137) 8 minutes ago admiring_bohr
4f7f394c7b56 eeb6ee3f44bd "/bin/bash" 18 minutes ago Exited (0) 18 minutes ago pensive_chatterjee
[root@docker ~]#
删除容器:
[root@docker ~]# docker rm 41e2126bc593 4f7f394c7b56 输入容器ID(可以多个)
41e2126bc593
4f7f394c7b56
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fd4223709f41 centos:7 "/bin/bash" 9 minutes ago Up 6 minutes mycentos
[root@docker ~]#假如要删除正在运行的容器
[root@docker ~]# docker rm fd4223709f41 会出现报错
Error response from daemon: You cannot remove a running container fd4223709f41cd45d19817c69b8dbac4b887fdfc3a04da332a6fe4ab04973d01. Stop the container before attempting removal or force remove
[root@docker ~]#只需加上-f属性就可以进行强制删除
[root@docker ~]# docker rm -f fd4223709f41
fd4223709f41
[root@docker ~]# docker ps -a 查看(没有容器)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker ~]#
查看容器详情信息:
[root@docker ~]# docker run -itd --name=mycentos centos:7 启动容器命名为mycentos
47bf21e86920a03780e6916147f25b10e2f263101633fee30fbb0ee738b5846e
[root@docker ~]# docker ps 查看运行
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
47bf21e86920 centos:7 "/bin/bash" 24 seconds ago Up 23 seconds mycentos
[root@docker ~]# docker inspect mycentos 查看详情信息
[
{
"Id": "47bf21e86920a03780e6916147f25b10e2f263101633fee30fbb0ee738b5846e",
"Created": "2023-02-03T07:12:35.675093246Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 11586,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-02-03T07:12:35.90377691Z",
进入容器:
[root@docker ~]# docker exec -it mycentos /bin/bash 后缀表示是以什么解释器进入
[root@47bf21e86920 /]# ps -ef 查看当前运行用户
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 07:12 pts/0 00:00:00 /bin/bash
root 15 0 0 07:16 pts/1 00:00:00 /bin/bash
root 29 15 0 07:17 pts/1 00:00:00 ps -ef
[root@47bf21e86920 /]# uname 基于linux
Linux
[root@47bf21e86920 /]# exit 退出
exit
[root@docker ~]#
同时停止运行多个容器:
[root@docker ~]# docker run -itd centos:7 启动一个
475d493ec77156d9576d017b1bc3919c957d21acefdbc331409a5250f5964298
[root@docker ~]# docker run -itd centos:7 启动两个
91b5ba8dfd78c35f674db12f228b5a6b597b228f1d7fd3902811ad88afb98a9e
[root@docker ~]# docker run -itd centos:7 启动三个
3b0d1f2fb104722c3002a3c1af57da7f36902a70915754954b4840b517a03290
[root@docker ~]# docker ps 进行查看
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3b0d1f2fb104 centos:7 "/bin/bash" 7 seconds ago Up 7 seconds epic_hoover
91b5ba8dfd78 centos:7 "/bin/bash" 9 seconds ago Up 8 seconds lucid_carver
475d493ec771 centos:7 "/bin/bash" 11 seconds ago Up 11 seconds gallant_jackson
47bf21e86920 centos:7 "/bin/bash" 8 minutes ago Up 8 minutes mycentos
[root@docker ~]# docker stop $(docker ps -a -q) 停止正在运行的多个容器
3b0d1f2fb104
91b5ba8dfd78
475d493ec771
47bf21e86920
[root@docker ~]# docker ps 查看已停止
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker ~]# docker ps -a -q 查看ID
3b0d1f2fb104
91b5ba8dfd78
475d493ec771
47bf21e86920
[root@docker ~]# docker restart $(docker ps -a -q) 进行同时启动多个容器
3b0d1f2fb104
91b5ba8dfd78
475d493ec771
47bf21e86920
[root@docker ~]# docker ps 查看启动状态
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3b0d1f2fb104 centos:7 "/bin/bash" 3 minutes ago Up 7 seconds epic_hoover
91b5ba8dfd78 centos:7 "/bin/bash" 3 minutes ago Up 7 seconds lucid_carver
475d493ec771 centos:7 "/bin/bash" 3 minutes ago Up 7 seconds gallant_jackson
47bf21e86920 centos:7 "/bin/bash" 11 minutes ago Up 7 seconds mycentos
[root@docker ~]#
7,容器的文件复制与挂载
[root@docker ~]# docker run -itd --name=mycentos centos:7 启动命名为mycentos
e37b96055fd48a317cf995213671b4ebc6cac86c9215067d7839f284e972f41b
[root@docker ~]#
[root@docker ~]#
[root@docker ~]# docker ps 查看运行
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e37b96055fd4 centos:7 "/bin/bash" 5 seconds ago Up 4 seconds mycentos
本机传文件进容器:
[root@docker ~]# pwd 查看当前目录
/root
[root@docker ~]# cat > 123.txt 新建一个为123.txt的文档输入lyx内容
lyx
^C
[root@docker ~]# cat 123.txt 查看内容
lyx
[root@docker ~]#将本机root目录下123.txt文档复制到容器/home/目录下
[root@docker ~]# docker cp /root/123.txt mycentos:/home/
Successfully copied 2.048kB to mycentos:/home/
[root@docker ~]# docker exec -it mycentos /bin/bash 进入容器
[root@e37b96055fd4 /]# cd /home/ 进入home目录下
[root@e37b96055fd4 home]# ls 查看文档
123.txt
[root@e37b96055fd4 home]# cat 123.txt 查看内容
lyx
[root@e37b96055fd4 home]#
容器传文件到本机:
[root@e37b96055fd4 home]# cat > 456.txt 创建456.txt文档输入内容456
456
^C
[root@e37b96055fd4 home]# cat 456.txt 查看内容
456
[root@e37b96055fd4 home]# exit 退出
[root@docker ~]# docker cp mycentos:/home/456.txt /root/ 进行复制
Successfully copied 2.048kB to /root/
[root@docker ~]# ls 本机查看文档
123.txt 456.txt anaconda-ks.cfg
[root@docker ~]# cat 456.txt 本机查看文档内容
456
[root@docker ~]#
挂载(本机挂载到容器)
[root@docker ~]# mkdir lyx 创建一个为lyx的文件夹
[root@docker ~]# docker run -itd -v /root/lyx/:/home centos:7 -v表示挂载
1b054a890054cd307069c992eef14f58f0d9bad39460f0d252ca46ff32dad837
[root@docker ~]# docker ps 查看运行
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b054a890054 centos:7 "/bin/bash" 8 seconds ago Up 7 seconds admiring_euler
e37b96055fd4 centos:7 "/bin/bash" 19 minutes ago Up 19 minutes mycentos
[root@docker ~]# docker exec -it 1b054a890054 /bin/bash 进入容器
[root@1b054a890054 /]# cd /home 进入目录
[root@1b054a890054 home]# touch 666.txt 创建666.txt文档
[root@1b054a890054 home]# ls 查看
666.txt
[root@docker ~]# cd lyx/ 本机进入lyx目录
[root@docker lyx]# ls 进行查看同步显示有666.txt文档
666.txt
[root@1b054a890054 home]# cat > 666.txt 容器书写两行6
666666666666
666666666666
^C
[root@1b054a890054 home]# cat 666.txt 进行查看
666666666666
666666666666
[root@1b054a890054 home]#
[root@docker lyx]# cat 666.txt 本机查看同步输出内容一致
666666666666
666666666666
创建数据卷容器
启动名为mycentos1的容器,要求此容器包含两上数据卷/opt/volume1和/opt/volume2。
[root@docker ~]# docker run -it -v /opt/volume1 -v /opt/volume2 --name mycentos1 centos:7 /bin/bash
[root@74890abe0c22 /]# cd /opt/
[root@74890abe0c22 opt]# ll
total 0
drwxr-xr-x 2 root root 6 Feb 28 02:21 volume1
drwxr-xr-x 2 root root 6 Feb 28 02:21 volume2
[root@74890abe0c22 opt]# 容器内数据卷目录vloume1和volume2已经创建
在宿主机上查看与数据卷对应的目录路径。
[root@docker ~]# docker inspect mycentos1|grep /var/lib/docker/volumes
"Source": "/var/lib/docker/volumes/32c4edd9e4e960db559390298dce1da0743a52148a8181a74dc27db4d269c3d8/_data",
"Source": "/var/lib/docker/volumes/253422197ec80ea80c352df6d703e7f254a3d7764e05a0592cb8583ebcef707f/_data",
[root@docker ~]#
从显示结果可以看到与数据卷对应的目录路径
创建mycentos2容器,挂载mycentos1容器种的数据卷。
[root@docker ~]# docker run -it --rm --volumes-from mycentos1 --name mycentos2 centos:7 /bin/bash
[root@b63a62d07d5d /]# cd /opt/
[root@b63a62d07d5d opt]# ll
total 0
drwxr-xr-x 2 root root 6 Feb 28 02:21 volume1
drwxr-xr-x 2 root root 6 Feb 28 02:21 volume2
[root@b63a62d07d5d opt]# 从显示结果上可以看到volume1和volume2数据卷都存在