第一节 Docker的入门
1 什么是docker
不必赘述,自己可以去百度下,哈哈
docker必须要有的东西: 镜像 容器以及仓库
2 Docker组件
Docker Client: Docker 的客户端
Docker Server:接收Docker Client发送的请求,并按照相应的路由规则实现路由分发
Docker 镜像:Docker镜像运行之后变成容器
Docker Registry: 这里更多是个仓库管理器
3 docker相对于传统的虚拟机的优势
1 启动秒级
2 资源利用率高,主机上可以运行数以千个容器
3 基本不消耗系统资源,实现运行在docker里面的应用性能很高
4 相对于传统的虚拟化技术,优势有哪些?
a 快速支付和部署
b 高效虚拟化 容器的运行不需要额外的hypervisor支持,docker属于内核级别的虚拟化,所以其性能更高
c 更轻松的钱和扩展。Docker容器可以在任意的平台上运行
d 高效的管理 只需要小小的修改就可以替代以往大量的更新工作
5 Docker 基本概念
1 Image 镜像
2 Container 容器 可以理解成简化版的Linux系统
3 Repository 仓库 存放镜像的地方。仓库注册服务器是用来管理镜像的,里面存放多个仓库,仓库里面有很多镜像,每个镜像都有不同的tag。
6 Docker的安装
1 在centos7中可以直接使用yum install docker
2 设置启动docker服务 systemctl start docker或者systemctl start docker.service
3 设置开机自启动 system enable docker.service
7 基本操作
1 怎样查询镜像 docker search mysql -s 100 查询mysql的镜像,默认查询其starts大于100的
2 拉取镜像 docker pull mysql
3 怎样运行镜像 docker run -it centos /bin/bash, 退出是exit, 选项-t 为容器分配一个伪终端 -i 表示交互
8 怎样创建镜像
1 修改已经有的镜像,然后使用commit提交就可以产生一个新的镜像
a) docker run -it centos /bin/bash
b) 修改镜像
c) docker commit -m "This is a description" -a "This is inforation of auther" 1f8b8eb61355 haoshuliang/centos:test #这里是生成镜像到本地的仓库
实操
[root@VM_0_15_centos docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
69dff30b6322 nginx "/bin/bash" 24 hours ago Exited (0) 25 minutes ago boring_brown
8e9313993a0a kinogmt/centos-ssh "/bin/bash" 24 hours ago Up 11 minutes 0.0.0.0:1234->22/tcp youthful_lichterman
6295c6878dcb kinogmt/centos-ssh "/bin/bash" 25 hours ago Up 10 minutes 22/tcp musing_meninsky
6a1d463e4346 4cbe7aa905e7 "/bin/bash" 25 hours ago Exited (127) 25 hours ago cool_curie
[root@VM_0_15_centos docker]# docker commit -m "This is a centos with ssh" -a "haoshuliang" 6295c6878dcb haoshuliang/centos-ssh-passwd
sha256:0c43616661009e8a72dd08d4bf6dffc05f24d50b3e47824b967be465037c75a7
[root@VM_0_15_centos docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haoshuliang/centos-ssh-passwd latest 0c4361666100 8 seconds ago 931MB
haoshuliang/centos test ee87dab68e9d 33 hours ago 250MB
nginx latest 540a289bab6c 3 weeks ago 126MB
centos latest 0f3e07c0138f 6 weeks ago 220MB
kinogmt/centos-ssh latest dc8713dad282 3 years ago 773MBx
1
[root@VM_0_15_centos docker]# docker ps -a2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3
69dff30b6322 nginx "/bin/bash" 24 hours ago Exited (0) 25 minutes ago boring_brown4
8e9313993a0a kinogmt/centos-ssh "/bin/bash" 24 hours ago Up 11 minutes 0.0.0.0:1234->22/tcp youthful_lichterman5
6295c6878dcb kinogmt/centos-ssh "/bin/bash" 25 hours ago Up 10 minutes 22/tcp musing_meninsky6
6a1d463e4346 4cbe7aa905e7 "/bin/bash" 25 hours ago Exited (127) 25 hours ago cool_curie7
[root@VM_0_15_centos docker]# docker commit -m "This is a centos with ssh" -a "haoshuliang" 6295c6878dcb haoshuliang/centos-ssh-passwd8
sha256:0c43616661009e8a72dd08d4bf6dffc05f24d50b3e47824b967be465037c75a79
[root@VM_0_15_centos docker]# docker images10
REPOSITORY TAG IMAGE ID CREATED SIZE11
haoshuliang/centos-ssh-passwd latest 0c4361666100 8 seconds ago 931MB12
haoshuliang/centos test ee87dab68e9d 33 hours ago 250MB13
nginx latest 540a289bab6c 3 weeks ago 126MB14
centos latest 0f3e07c0138f 6 weeks ago 220MB15
kinogmt/centos-ssh latest dc8713dad282 3 years ago 773MB2 利用Dockfile 创建镜像
1)每一个命令都会进行封装一层,并且层数有限制,最高127
2)在目录/app/docker/centos/dockfile下编写Dockerfile,如下
# 这里就是备注
FROM centos
MAINTAINER REGAN haoshuliang
RUN yum install mysql -qqy4
1
# 这里就是备注2
FROM centos3
MAINTAINER REGAN haoshuliang4
RUN yum install mysql -qqy3)执行命令
执行创建镜像的命令
[root@localhost dockfile]# docker build -t runoob/ubuntu:v1 .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM centos
---> 0f3e07c0138f
Step 2/3 : MAINTAINER REGAN haoshuliang
---> Using cache
---> 00974c980554
Step 3/3 : RUN echo "hello"
---> [Warning] IPv4 forwarding is disabled. Networking will not work.
---> Running in 13d33e2ff867
hello
Removing intermediate container 13d33e2ff867
---> 5fe67db864a9
Successfully built 5fe67db864a9
Successfully tagged runoob/ubuntu:v1 # 这里就是表示成功15
1
[root@localhost dockfile]# docker build -t runoob/ubuntu:v1 .2
Sending build context to Docker daemon 2.048kB3
Step 1/3 : FROM centos4
---> 0f3e07c0138f5
Step 2/3 : MAINTAINER REGAN haoshuliang6
---> Using cache7
---> 00974c9805548
Step 3/3 : RUN echo "hello"9
---> [Warning] IPv4 forwarding is disabled. Networking will not work.10
---> Running in 13d33e2ff86711
hello12
Removing intermediate container 13d33e2ff86713
---> 5fe67db864a914
Successfully built 5fe67db864a915
Successfully tagged runoob/ubuntu:v1 # 这里就是表示成功查看下所有的镜像
[root@localhost dockfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
runoob/ubuntu v1 5fe67db864a9 About a minute ago 220MB
centos latest 0f3e07c0138f 6 weeks ago 220MB
hello-world latest fce289e99eb9 10 months ago 1.84kB5
1
[root@localhost dockfile]# docker images2
REPOSITORY TAG IMAGE ID CREATED SIZE3
runoob/ubuntu v1 5fe67db864a9 About a minute ago 220MB4
centos latest 0f3e07c0138f 6 weeks ago 220MB5
hello-world latest fce289e99eb9 10 months ago 1.84kB9 怎样修改tag
执行命令
docker tag <imgae_id> haoshuliang/centos:v11
1
docker tag <imgae_id> haoshuliang/centos:v1下面是实际操作
# 下面是执行修改tag的操作
[root@localhost dockfile]# docker tag 5fe67db864a9 haoshuliang/centos_with_modiy:v1
# 下面列出所有的镜像,发现只是copy了一个
[root@localhost dockfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haoshuliang/centos_with_modiy v1 5fe67db864a9 2 minutes ago 220MB
runoob/ubuntu v1 5fe67db864a9 2 minutes ago 220MB
centos latest 0f3e07c0138f 6 weeks ago 220MB
hello-world latest fce289e99eb9 10 months ago 1.84kB1
# 下面是执行修改tag的操作2
[root@localhost dockfile]# docker tag 5fe67db864a9 haoshuliang/centos_with_modiy:v1 3
# 下面列出所有的镜像,发现只是copy了一个4
[root@localhost dockfile]# docker images5
REPOSITORY TAG IMAGE ID CREATED SIZE6
haoshuliang/centos_with_modiy v1 5fe67db864a9 2 minutes ago 220MB7
runoob/ubuntu v1 5fe67db864a9 2 minutes ago 220MB8
centos latest 0f3e07c0138f 6 weeks ago 220MB9
hello-world latest fce289e99eb9 10 months ago 1.84kB10 怎样导入镜像
方法1:
下面是规范
cat alibaba-rocketmq-3.2.6.tar.gz | docker import - rocketmq:3.2.61
1
cat alibaba-rocketmq-3.2.6.tar.gz | docker import - rocketmq:3.2.6实操:
[root@localhost dockfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haoshuliang/centos_with_modiy v1 5fe67db864a9 17 minutes ago 220MB
centos latest 0f3e07c0138f 6 weeks ago 220MB
hello-world latest fce289e99eb9 10 months ago 1.84kB
[root@localhost dockfile]# docker rmi 5fe67db864a9
Untagged: haoshuliang/centos_with_modiy:v1
Deleted: sha256:5fe67db864a920c6d142ff4cb1ade6fb919e60ef31b1c906b18ea4a50d553476
[root@localhost dockfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 0f3e07c0138f 6 weeks ago 220MB
hello-world latest fce289e99eb9 10 months ago 1.84kB
[root@localhost dockfile]# cat centos.tar.gz | docker import - haoshuliang/centos_with_modiy:v1
sha256:ee42fdec73fa6b17994a1c46a45e73b7abba6c7d2658358f0bbb332e9e0db606
[root@localhost dockfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haoshuliang/centos_with_modiy v1 ee42fdec73fa 9 seconds ago 227MB
centos latest 0f3e07c0138f 6 weeks ago 220MB
hello-world latest fce289e99eb9 10 months ago 1.84kB19
1
[root@localhost dockfile]# docker images2
REPOSITORY TAG IMAGE ID CREATED SIZE3
haoshuliang/centos_with_modiy v1 5fe67db864a9 17 minutes ago 220MB4
centos latest 0f3e07c0138f 6 weeks ago 220MB5
hello-world latest fce289e99eb9 10 months ago 1.84kB6
[root@localhost dockfile]# docker rmi 5fe67db864a9 7
Untagged: haoshuliang/centos_with_modiy:v18
Deleted: sha256:5fe67db864a920c6d142ff4cb1ade6fb919e60ef31b1c906b18ea4a50d5534769
[root@localhost dockfile]# docker images10
REPOSITORY TAG IMAGE ID CREATED SIZE11
centos latest 0f3e07c0138f 6 weeks ago 220MB12
hello-world latest fce289e99eb9 10 months ago 1.84kB13
[root@localhost dockfile]# cat centos.tar.gz | docker import - haoshuliang/centos_with_modiy:v114
sha256:ee42fdec73fa6b17994a1c46a45e73b7abba6c7d2658358f0bbb332e9e0db60615
[root@localhost dockfile]# docker images16
REPOSITORY TAG IMAGE ID CREATED SIZE17
haoshuliang/centos_with_modiy v1 ee42fdec73fa 9 seconds ago 227MB18
centos latest 0f3e07c0138f 6 weeks ago 220MB19
hello-world latest fce289e99eb9 10 months ago 1.84kB方法二:
规范
docker load --input <镜像名字.tar.gz>1
1
docker load --input <镜像名字.tar.gz>实操:
[root@localhost dockfile]# docker load --input centos.tar.gz
Loaded image: haoshuliang/centos_with_modiy:v1
[root@localhost dockfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haoshuliang/centos_with_modiy v1 5fe67db864a9 17 minutes ago 220MB
centos latest 0f3e07c0138f 6 weeks ago 220MB
hello-world latest fce289e99eb9 10 months ago 1.84kB
8
1
[root@localhost dockfile]# docker load --input centos.tar.gz 2
Loaded image: haoshuliang/centos_with_modiy:v13
[root@localhost dockfile]# docker images4
REPOSITORY TAG IMAGE ID CREATED SIZE5
haoshuliang/centos_with_modiy v1 5fe67db864a9 17 minutes ago 220MB6
centos latest 0f3e07c0138f 6 weeks ago 220MB7
hello-world latest fce289e99eb9 10 months ago 1.84kB8
note: 镜像必须是tar.gz 结尾的
11 怎样将自己的镜像上传到共享仓库
docker push haoshuliang/centos:v1.11
1
docker push haoshuliang/centos:v1.112 怎样将镜像保存到本地
规范
docker save -o <镜像的名字.tar.gz> <仓库的镜像名称>1
1
docker save -o <镜像的名字.tar.gz> <仓库的镜像名称>实操:
[root@localhost dockfile]# docker save -o centos.tar.gz haoshuliang/centos_with_modiy
[root@localhost dockfile]# ls
centos.tar.gz Dockerfile3
1
[root@localhost dockfile]# docker save -o centos.tar.gz haoshuliang/centos_with_modiy2
[root@localhost dockfile]# ls3
centos.tar.gz Dockerfile13 怎样删除镜像
规范
docker rmi <镜像的id>1
1
docker rmi <镜像的id>实操
[root@localhost dockfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haoshuliang/centos_with_modiy v1 5fe67db864a9 12 minutes ago 220MB
runoob/ubuntu v1 5fe67db864a9 12 minutes ago 220MB
centos latest 0f3e07c0138f 6 weeks ago 220MB
hello-world latest fce289e99eb9 10 months ago 1.84kB
[root@localhost dockfile]# docker rmi 5fe67db864a9
Error response from daemon: conflict: unable to delete 5fe67db864a9 (must be forced) - image is referenced in multiple repositories
# 下面是强制删除,因为上面的报错中已经显示该镜像id已经被多个repositories 绑定,所以要想删除的话就必须执行强制执行
[root@localhost dockfile]# docker rmi -f 5fe67db864a9
Untagged: haoshuliang/centos_with_modiy:v1
Untagged: runoob/ubuntu:v1
Deleted: sha256:5fe67db864a920c6d142ff4cb1ade6fb919e60ef31b1c906b18ea4a50d55347613
1
[root@localhost dockfile]# docker images2
REPOSITORY TAG IMAGE ID CREATED SIZE3
haoshuliang/centos_with_modiy v1 5fe67db864a9 12 minutes ago 220MB4
runoob/ubuntu v1 5fe67db864a9 12 minutes ago 220MB5
centos latest 0f3e07c0138f 6 weeks ago 220MB6
hello-world latest fce289e99eb9 10 months ago 1.84kB7
[root@localhost dockfile]# docker rmi 5fe67db864a98
Error response from daemon: conflict: unable to delete 5fe67db864a9 (must be forced) - image is referenced in multiple repositories9
# 下面是强制删除,因为上面的报错中已经显示该镜像id已经被多个repositories 绑定,所以要想删除的话就必须执行强制执行10
[root@localhost dockfile]# docker rmi -f 5fe67db864a911
Untagged: haoshuliang/centos_with_modiy:v112
Untagged: runoob/ubuntu:v113
Deleted: sha256:5fe67db864a920c6d142ff4cb1ade6fb919e60ef31b1c906b18ea4a50d55347614 docker 容器的操作
启动方式一:docker run centos
操作过程
- a) 检查本地是否存在镜像,如果没有该镜像,则默认需要去远程仓库进行拉取
- b) 使用镜像创建并启动容器
- c) 内核给该容器分配一个文件系统,并在只读的镜像层挂载一层可读写的层
- d) 从宿主机主机配置的网桥接口中桥接一个虚拟接口到容器中去
- e) 从地址池分配一个ip地址给容器
- f) 执行程序
- g) 容器被终止
启动方式二: docker start <容器id>
实操
[root@localhost dockfile]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9700691b7c7d centos "cal" 15 minutes ago Exited (0) 15 minutes ago epic_banach
3ec1dd341997 centos "/bin/bash" 40 minutes ago Up 15 seconds gifted_williamson
6b4199286ff9 centos "/bin/bash" 41 minutes ago Exited (127) 41 minutes ago cranky_haslett
686d4162c34d 00974c980554 "/bin/sh -c 'yum -qq…" 44 minutes ago Exited (1) 43 minutes ago elastic_solomon
bf04a2613883 00974c980554 "/bin/sh -c 'yum -qq…" 52 minutes ago Exited (1) 51 minutes ago nervous_hypatia
ce534c08afce 00974c980554 "/bin/sh -c 'yum ins…" 59 minutes ago Exited (1) 58 minutes ago unruffled_feynman
5031361bf654 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago adoring_roentgen
0a8bba509332 hello-world "/hello" 13 days ago Exited (0) 13 days ago peaceful_kowalevski
[root@localhost dockfile]# docker stop 3ec1dd341997
3ec1dd341997
[root@localhost dockfile]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9700691b7c7d centos "cal" 16 minutes ago Exited (0) 16 minutes ago epic_banach
3ec1dd341997 centos "/bin/bash" 41 minutes ago Exited (0) 6 seconds ago gifted_williamson
6b4199286ff9 centos "/bin/bash" 42 minutes ago Exited (127) 41 minutes ago cranky_haslett
686d4162c34d 00974c980554 "/bin/sh -c 'yum -qq…" 44 minutes ago Exited (1) 43 minutes ago elastic_solomon
bf04a2613883 00974c980554 "/bin/sh -c 'yum -qq…" 53 minutes ago Exited (1) 52 minutes ago nervous_hypatia
ce534c08afce 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) 59 minutes ago unruffled_feynman
5031361bf654 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago adoring_roentgen
0a8bba509332 hello-world "/hello" 13 days ago Exited (0) 13 days ago peaceful_kowalevski
[root@localhost dockfile]# docker start 3ec1dd341997
3ec1dd341997
[root@localhost dockfile]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9700691b7c7d centos "cal" 16 minutes ago Exited (0) 16 minutes ago epic_banach
3ec1dd341997 centos "/bin/bash" 41 minutes ago Up 4 seconds gifted_williamson
6b4199286ff9 centos "/bin/bash" 42 minutes ago Exited (127) 41 minutes ago cranky_haslett
686d4162c34d 00974c980554 "/bin/sh -c 'yum -qq…" 45 minutes ago Exited (1) 44 minutes ago elastic_solomon
bf04a2613883 00974c980554 "/bin/sh -c 'yum -qq…" 53 minutes ago Exited (1) 52 minutes ago nervous_hypatia
ce534c08afce 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) 59 minutes ago unruffled_feynman
5031361bf654 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago adoring_roentgen
0a8bba509332 hello-world "/hello" 13 days ago Exited (0) 13 days ago peaceful_kowalevski34
1
[root@localhost dockfile]# docker ps -a2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3
9700691b7c7d centos "cal" 15 minutes ago Exited (0) 15 minutes ago epic_banach4
3ec1dd341997 centos "/bin/bash" 40 minutes ago Up 15 seconds gifted_williamson5
6b4199286ff9 centos "/bin/bash" 41 minutes ago Exited (127) 41 minutes ago cranky_haslett6
686d4162c34d 00974c980554 "/bin/sh -c 'yum -qq…" 44 minutes ago Exited (1) 43 minutes ago elastic_solomon7
bf04a2613883 00974c980554 "/bin/sh -c 'yum -qq…" 52 minutes ago Exited (1) 51 minutes ago nervous_hypatia8
ce534c08afce 00974c980554 "/bin/sh -c 'yum ins…" 59 minutes ago Exited (1) 58 minutes ago unruffled_feynman9
5031361bf654 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago adoring_roentgen10
0a8bba509332 hello-world "/hello" 13 days ago Exited (0) 13 days ago peaceful_kowalevski11
[root@localhost dockfile]# docker stop 3ec1dd34199712
3ec1dd34199713
[root@localhost dockfile]# docker ps -a14
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES15
9700691b7c7d centos "cal" 16 minutes ago Exited (0) 16 minutes ago epic_banach16
3ec1dd341997 centos "/bin/bash" 41 minutes ago Exited (0) 6 seconds ago gifted_williamson17
6b4199286ff9 centos "/bin/bash" 42 minutes ago Exited (127) 41 minutes ago cranky_haslett18
686d4162c34d 00974c980554 "/bin/sh -c 'yum -qq…" 44 minutes ago Exited (1) 43 minutes ago elastic_solomon19
bf04a2613883 00974c980554 "/bin/sh -c 'yum -qq…" 53 minutes ago Exited (1) 52 minutes ago nervous_hypatia20
ce534c08afce 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) 59 minutes ago unruffled_feynman21
5031361bf654 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago adoring_roentgen22
0a8bba509332 hello-world "/hello" 13 days ago Exited (0) 13 days ago peaceful_kowalevski23
[root@localhost dockfile]# docker start 3ec1dd34199724
3ec1dd34199725
[root@localhost dockfile]# docker ps -a26
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES27
9700691b7c7d centos "cal" 16 minutes ago Exited (0) 16 minutes ago epic_banach28
3ec1dd341997 centos "/bin/bash" 41 minutes ago Up 4 seconds gifted_williamson29
6b4199286ff9 centos "/bin/bash" 42 minutes ago Exited (127) 41 minutes ago cranky_haslett30
686d4162c34d 00974c980554 "/bin/sh -c 'yum -qq…" 45 minutes ago Exited (1) 44 minutes ago elastic_solomon31
bf04a2613883 00974c980554 "/bin/sh -c 'yum -qq…" 53 minutes ago Exited (1) 52 minutes ago nervous_hypatia32
ce534c08afce 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) 59 minutes ago unruffled_feynman33
5031361bf654 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago adoring_roentgen34
0a8bba509332 hello-world "/hello" 13 days ago Exited (0) 13 days ago peaceful_kowalevskiNote: 适用于已经创建的容器,并得到id的名称
启动方式三:保护态运行
规范
docker run -d centos /bin/bash -c "while true; do echo "hello"; sleep 1; done"1
1
docker run -d centos /bin/bash -c "while true; do echo "hello"; sleep 1; done"实际操作:
# 执行ps -a 查看docker容器的运行状态,并获取容器的id
[root@localhost dockfile]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c86e12b7c04a centos "/bin/bash -c 'while…" 2 minutes ago Up 2 minutes bold_bhabha
9700691b7c7d centos "cal" 21 minutes ago Exited (0) 21 minutes ago epic_banach
3ec1dd341997 centos "/bin/bash" 46 minutes ago Up 4 minutes gifted_williamson
6b4199286ff9 centos "/bin/bash" 47 minutes ago Exited (127) 46 minutes ago cranky_haslett
686d4162c34d 00974c980554 "/bin/sh -c 'yum -qq…" 49 minutes ago Exited (1) 48 minutes ago elastic_solomon
bf04a2613883 00974c980554 "/bin/sh -c 'yum -qq…" 58 minutes ago Exited (1) 57 minutes ago nervous_hypatia
ce534c08afce 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago unruffled_feynman
5031361bf654 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago adoring_roentgen
0a8bba509332 hello-world "/hello" 13 days ago Exited (0) 13 days ago peaceful_kowalevski
# 根据前面获取的容器id,查看打印的日志
[root@localhost dockfile]# docker logs c86e12b7c04a
hello
hello
hello
hello
...
# 下面是再执行一次,可以按键这个容器又重新创建了一个
[root@localhost dockfile]# docker run -d centos /bin/bash -c "while true; do echo "hello"; sleep 1; done"
WARNING: IPv4 forwarding is disabled. Networking will not work.
0c7c5e3d4b89996dd1d9f2db3ad8db3445178e71252773bf256de203937394d5
[root@localhost dockfile]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0c7c5e3d4b89 centos "/bin/bash -c 'while…" 4 seconds ago Up 3 seconds tender_mahavira
c86e12b7c04a centos "/bin/bash -c 'while…" 3 minutes ago Up 3 minutes bold_bhabha
9700691b7c7d centos "cal" 23 minutes ago Exited (0) 23 minutes ago epic_banach
3ec1dd341997 centos "/bin/bash" 48 minutes ago Up 6 minutes gifted_williamson
6b4199286ff9 centos "/bin/bash" 48 minutes ago Exited (127) 48 minutes ago cranky_haslett
686d4162c34d 00974c980554 "/bin/sh -c 'yum -qq…" 51 minutes ago Exited (1) 50 minutes ago elastic_solomon
bf04a2613883 00974c980554 "/bin/sh -c 'yum -qq…" About an hour ago Exited (1) 58 minutes ago nervous_hypatia
ce534c08afce 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago unruffled_feynman
5031361bf654 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago adoring_roentgen
0a8bba509332 hello-world "/hello" 13 days ago Exited (0) 13 days ago peaceful_kowalevski
36
1
# 执行ps -a 查看docker容器的运行状态,并获取容器的id2
[root@localhost dockfile]# docker ps -a3
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES4
c86e12b7c04a centos "/bin/bash -c 'while…" 2 minutes ago Up 2 minutes bold_bhabha5
9700691b7c7d centos "cal" 21 minutes ago Exited (0) 21 minutes ago epic_banach6
3ec1dd341997 centos "/bin/bash" 46 minutes ago Up 4 minutes gifted_williamson7
6b4199286ff9 centos "/bin/bash" 47 minutes ago Exited (127) 46 minutes ago cranky_haslett8
686d4162c34d 00974c980554 "/bin/sh -c 'yum -qq…" 49 minutes ago Exited (1) 48 minutes ago elastic_solomon9
bf04a2613883 00974c980554 "/bin/sh -c 'yum -qq…" 58 minutes ago Exited (1) 57 minutes ago nervous_hypatia10
ce534c08afce 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago unruffled_feynman11
5031361bf654 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago adoring_roentgen12
0a8bba509332 hello-world "/hello" 13 days ago Exited (0) 13 days ago peaceful_kowalevski13
# 根据前面获取的容器id,查看打印的日志14
[root@localhost dockfile]# docker logs c86e12b7c04a15
hello16
hello17
hello18
hello19
...20
# 下面是再执行一次,可以按键这个容器又重新创建了一个21
[root@localhost dockfile]# docker run -d centos /bin/bash -c "while true; do echo "hello"; sleep 1; done"22
WARNING: IPv4 forwarding is disabled. Networking will not work.23
0c7c5e3d4b89996dd1d9f2db3ad8db3445178e71252773bf256de203937394d524
[root@localhost dockfile]# docker ps -a25
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES26
0c7c5e3d4b89 centos "/bin/bash -c 'while…" 4 seconds ago Up 3 seconds tender_mahavira27
c86e12b7c04a centos "/bin/bash -c 'while…" 3 minutes ago Up 3 minutes bold_bhabha28
9700691b7c7d centos "cal" 23 minutes ago Exited (0) 23 minutes ago epic_banach29
3ec1dd341997 centos "/bin/bash" 48 minutes ago Up 6 minutes gifted_williamson30
6b4199286ff9 centos "/bin/bash" 48 minutes ago Exited (127) 48 minutes ago cranky_haslett31
686d4162c34d 00974c980554 "/bin/sh -c 'yum -qq…" 51 minutes ago Exited (1) 50 minutes ago elastic_solomon32
bf04a2613883 00974c980554 "/bin/sh -c 'yum -qq…" About an hour ago Exited (1) 58 minutes ago nervous_hypatia33
ce534c08afce 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago unruffled_feynman34
5031361bf654 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago adoring_roentgen35
0a8bba509332 hello-world "/hello" 13 days ago Exited (0) 13 days ago peaceful_kowalevski36
怎样删除容呢?
规范:
docker rm -f <容器id>1
1
docker rm -f <容器id>实操
[root@localhost dockfile]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0c7c5e3d4b89 centos "/bin/bash -c 'while…" 2 minutes ago Up 2 minutes tender_mahavira
c86e12b7c04a centos "/bin/bash -c 'while…" 6 minutes ago Up 6 minutes bold_bhabha
9700691b7c7d centos "cal" 25 minutes ago Exited (0) 25 minutes ago epic_banach
3ec1dd341997 centos "/bin/bash" 50 minutes ago Up 9 minutes gifted_williamson
6b4199286ff9 centos "/bin/bash" 51 minutes ago Exited (127) 50 minutes ago cranky_haslett
686d4162c34d 00974c980554 "/bin/sh -c 'yum -qq…" 53 minutes ago Exited (1) 52 minutes ago elastic_solomon
bf04a2613883 00974c980554 "/bin/sh -c 'yum -qq…" About an hour ago Exited (1) About an hour ago nervous_hypatia
ce534c08afce 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago unruffled_feynman
5031361bf654 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago adoring_roentgen
0a8bba509332 hello-world "/hello" 13 days ago Exited (0) 13 days ago peaceful_kowalevski
[root@localhost dockfile]# docker rm 0c7c5e3d4b89
Error response from daemon: You cannot remove a running container 0c7c5e3d4b89996dd1d9f2db3ad8db3445178e71252773bf256de203937394d5. Stop the container before attempting removal or force remove
[root@localhost dockfile]# docker stop 0c7c5e3d4b89
0c7c5e3d4b89
[root@localhost dockfile]# docker rm 0c7c5e3d4b89
0c7c5e3d4b89
[root@localhost dockfile]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c86e12b7c04a centos "/bin/bash -c 'while…" 6 minutes ago Up 6 minutes bold_bhabha
9700691b7c7d centos "cal" 26 minutes ago Exited (0) 26 minutes ago epic_banach
3ec1dd341997 centos "/bin/bash" 51 minutes ago Up 9 minutes gifted_williamson
6b4199286ff9 centos "/bin/bash" 51 minutes ago Exited (127) 51 minutes ago cranky_haslett
686d4162c34d 00974c980554 "/bin/sh -c 'yum -qq…" 54 minutes ago Exited (1) 53 minutes ago elastic_solomon
bf04a2613883 00974c980554 "/bin/sh -c 'yum -qq…" About an hour ago Exited (1) About an hour ago nervous_hypatia
ce534c08afce 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago unruffled_feynman
5031361bf654 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago adoring_roentgen
0a8bba509332 hello-world "/hello" 13 days ago Exited (0) 13 days ago peaceful_kowalevski
[root@localhost dockfile]# docker rm c86e12b7c04a
Error response from daemon: You cannot remove a running container c86e12b7c04ac5829ee48ce978dcccd9557ee0fa216444b8f01501f24e3560d8. Stop the container before attempting removal or force remove
[root@localhost dockfile]# docker rm -f c86e12b7c04a
c86e12b7c04a
[root@localhost dockfile]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9700691b7c7d centos "cal" 26 minutes ago Exited (0) 26 minutes ago epic_banach
3ec1dd341997 centos "/bin/bash" 51 minutes ago Up 10 minutes gifted_williamson
6b4199286ff9 centos "/bin/bash" 52 minutes ago Exited (127) 51 minutes ago cranky_haslett
686d4162c34d 00974c980554 "/bin/sh -c 'yum -qq…" 55 minutes ago Exited (1) 54 minutes ago elastic_solomon
bf04a2613883 00974c980554 "/bin/sh -c 'yum -qq…" About an hour ago Exited (1) About an hour ago nervous_hypatia
ce534c08afce 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago unruffled_feynman
5031361bf654 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago adoring_roentgen
0a8bba509332 hello-world "/hello" 13 days ago Exited (0) 13 days ago peaceful_kowalevski43
1
[root@localhost dockfile]# docker ps -a2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3
0c7c5e3d4b89 centos "/bin/bash -c 'while…" 2 minutes ago Up 2 minutes tender_mahavira4
c86e12b7c04a centos "/bin/bash -c 'while…" 6 minutes ago Up 6 minutes bold_bhabha5
9700691b7c7d centos "cal" 25 minutes ago Exited (0) 25 minutes ago epic_banach6
3ec1dd341997 centos "/bin/bash" 50 minutes ago Up 9 minutes gifted_williamson7
6b4199286ff9 centos "/bin/bash" 51 minutes ago Exited (127) 50 minutes ago cranky_haslett8
686d4162c34d 00974c980554 "/bin/sh -c 'yum -qq…" 53 minutes ago Exited (1) 52 minutes ago elastic_solomon9
bf04a2613883 00974c980554 "/bin/sh -c 'yum -qq…" About an hour ago Exited (1) About an hour ago nervous_hypatia10
ce534c08afce 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago unruffled_feynman11
5031361bf654 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago adoring_roentgen12
0a8bba509332 hello-world "/hello" 13 days ago Exited (0) 13 days ago peaceful_kowalevski13
[root@localhost dockfile]# docker rm 0c7c5e3d4b8914
Error response from daemon: You cannot remove a running container 0c7c5e3d4b89996dd1d9f2db3ad8db3445178e71252773bf256de203937394d5. Stop the container before attempting removal or force remove15
[root@localhost dockfile]# docker stop 0c7c5e3d4b8916
0c7c5e3d4b8917
[root@localhost dockfile]# docker rm 0c7c5e3d4b8918
0c7c5e3d4b8919
[root@localhost dockfile]# docker ps -a20
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES21
c86e12b7c04a centos "/bin/bash -c 'while…" 6 minutes ago Up 6 minutes bold_bhabha22
9700691b7c7d centos "cal" 26 minutes ago Exited (0) 26 minutes ago epic_banach23
3ec1dd341997 centos "/bin/bash" 51 minutes ago Up 9 minutes gifted_williamson24
6b4199286ff9 centos "/bin/bash" 51 minutes ago Exited (127) 51 minutes ago cranky_haslett25
686d4162c34d 00974c980554 "/bin/sh -c 'yum -qq…" 54 minutes ago Exited (1) 53 minutes ago elastic_solomon26
bf04a2613883 00974c980554 "/bin/sh -c 'yum -qq…" About an hour ago Exited (1) About an hour ago nervous_hypatia27
ce534c08afce 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago unruffled_feynman28
5031361bf654 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago adoring_roentgen29
0a8bba509332 hello-world "/hello" 13 days ago Exited (0) 13 days ago peaceful_kowalevski30
[root@localhost dockfile]# docker rm c86e12b7c04a31
Error response from daemon: You cannot remove a running container c86e12b7c04ac5829ee48ce978dcccd9557ee0fa216444b8f01501f24e3560d8. Stop the container before attempting removal or force remove32
[root@localhost dockfile]# docker rm -f c86e12b7c04a33
c86e12b7c04a34
[root@localhost dockfile]# docker ps -a35
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES36
9700691b7c7d centos "cal" 26 minutes ago Exited (0) 26 minutes ago epic_banach37
3ec1dd341997 centos "/bin/bash" 51 minutes ago Up 10 minutes gifted_williamson38
6b4199286ff9 centos "/bin/bash" 52 minutes ago Exited (127) 51 minutes ago cranky_haslett39
686d4162c34d 00974c980554 "/bin/sh -c 'yum -qq…" 55 minutes ago Exited (1) 54 minutes ago elastic_solomon40
bf04a2613883 00974c980554 "/bin/sh -c 'yum -qq…" About an hour ago Exited (1) About an hour ago nervous_hypatia41
ce534c08afce 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago unruffled_feynman42
5031361bf654 00974c980554 "/bin/sh -c 'yum ins…" About an hour ago Exited (1) About an hour ago adoring_roentgen43
0a8bba509332 hello-world "/hello" 13 days ago Exited (0) 13 days ago peaceful_kowalevski如果一个容器已经切换到后台,该如何才能重新进入容器呢
规范
#可以使用attach的命令
docker attach <容器id>2
1
#可以使用attach的命令2
docker attach <容器id>实操
[root@VM_0_15_centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5e87fb906732 nginx "/bin/bash" 2 hours ago Exited (0) 2 minutes ago admiring_shamir
1d334f409ffd 86e716b77ca5 "/bin/sh -c 'yum ins…" 2 hours ago Exited (1) 2 hours ago amazing_cartwright
c7c9bb281a16 haoshuliang/centos:test "/bin/bash" 4 hours ago Exited (0) 4 hours ago thirsty_albattani
1f8b8eb61355 centos "/bin/bash" 4 hours ago Exited (127) 4 hours ago optimistic_fermi
6585b34bd420 centos "/bin/bash" 4 hours ago Exited (0) 4 hours ago charming_margulis
76d49dc677c0 hello-world "/hello" 4 hours ago Exited (0) 4 hours ago hopeful_lichterman
a8b5c077336c centos "/bin/bash" 2 days ago Exited (127) 2 days ago loving_joliot
497dc2195148 centos "/bin/bash" 2 days ago Exited (0) 2 days ago goofy_bouman
5ed8f689edf9 hello-world "/hello" 5 days ago Exited (0) 5 days ago adoring_edison
4ac14a9991c4 hello-world "/hello" 5 days ago Exited (0) 5 days ago sweet_chatterjee
[root@VM_0_15_centos ~]# docker start 5e87fb906732
5e87fb906732
[root@VM_0_15_centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5e87fb906732 nginx "/bin/bash" 2 hours ago Up 3 seconds 80/tcp admiring_shamir
1d334f409ffd 86e716b77ca5 "/bin/sh -c 'yum ins…" 2 hours ago Exited (1) 2 hours ago amazing_cartwright
c7c9bb281a16 haoshuliang/centos:test "/bin/bash" 4 hours ago Exited (0) 4 hours ago thirsty_albattani
1f8b8eb61355 centos "/bin/bash" 4 hours ago Exited (127) 4 hours ago optimistic_fermi
6585b34bd420 centos "/bin/bash" 4 hours ago Exited (0) 4 hours ago charming_margulis
76d49dc677c0 hello-world "/hello" 4 hours ago Exited (0) 4 hours ago hopeful_lichterman
a8b5c077336c centos "/bin/bash" 2 days ago Exited (127) 2 days ago loving_joliot
497dc2195148 centos "/bin/bash" 2 days ago Exited (0) 2 days ago goofy_bouman
5ed8f689edf9 hello-world "/hello" 5 days ago Exited (0) 5 days ago adoring_edison
4ac14a9991c4 hello-world "/hello" 5 days ago Exited (0) 5 days ago sweet_chatterjee
[root@VM_0_15_centos ~]# docker attach 5e87fb906732
root@5e87fb906732:/# hostname
5e87fb906732
root@5e87fb906732:/# exit
exit
[root@VM_0_15_centos ~]# docker attach 5e87fb906732
You cannot attach to a stopped container, start it first
# 只要以attach进入的容器,则exit退出后容器也就会退出
35
1
[root@VM_0_15_centos ~]# docker ps -a2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3
5e87fb906732 nginx "/bin/bash" 2 hours ago Exited (0) 2 minutes ago admiring_shamir4
1d334f409ffd 86e716b77ca5 "/bin/sh -c 'yum ins…" 2 hours ago Exited (1) 2 hours ago amazing_cartwright5
c7c9bb281a16 haoshuliang/centos:test "/bin/bash" 4 hours ago Exited (0) 4 hours ago thirsty_albattani6
1f8b8eb61355 centos "/bin/bash" 4 hours ago Exited (127) 4 hours ago optimistic_fermi7
6585b34bd420 centos "/bin/bash" 4 hours ago Exited (0) 4 hours ago charming_margulis8
76d49dc677c0 hello-world "/hello" 4 hours ago Exited (0) 4 hours ago hopeful_lichterman9
a8b5c077336c centos "/bin/bash" 2 days ago Exited (127) 2 days ago loving_joliot10
497dc2195148 centos "/bin/bash" 2 days ago Exited (0) 2 days ago goofy_bouman11
5ed8f689edf9 hello-world "/hello" 5 days ago Exited (0) 5 days ago adoring_edison12
4ac14a9991c4 hello-world "/hello" 5 days ago Exited (0) 5 days ago sweet_chatterjee13
[root@VM_0_15_centos ~]# docker start 5e87fb90673214
5e87fb90673215
[root@VM_0_15_centos ~]# docker ps -a16
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES17
5e87fb906732 nginx "/bin/bash" 2 hours ago Up 3 seconds 80/tcp admiring_shamir18
1d334f409ffd 86e716b77ca5 "/bin/sh -c 'yum ins…" 2 hours ago Exited (1) 2 hours ago amazing_cartwright19
c7c9bb281a16 haoshuliang/centos:test "/bin/bash" 4 hours ago Exited (0) 4 hours ago thirsty_albattani20
1f8b8eb61355 centos "/bin/bash" 4 hours ago Exited (127) 4 hours ago optimistic_fermi21
6585b34bd420 centos "/bin/bash" 4 hours ago Exited (0) 4 hours ago charming_margulis22
76d49dc677c0 hello-world "/hello" 4 hours ago Exited (0) 4 hours ago hopeful_lichterman23
a8b5c077336c centos "/bin/bash" 2 days ago Exited (127) 2 days ago loving_joliot24
497dc2195148 centos "/bin/bash" 2 days ago Exited (0) 2 days ago goofy_bouman25
5ed8f689edf9 hello-world "/hello" 5 days ago Exited (0) 5 days ago adoring_edison26
4ac14a9991c4 hello-world "/hello" 5 days ago Exited (0) 5 days ago sweet_chatterjee27
[root@VM_0_15_centos ~]# docker attach 5e87fb90673228
root@5e87fb906732:/# hostname 29
5e87fb90673230
root@5e87fb906732:/# exit31
exit32
[root@VM_0_15_centos ~]# docker attach 5e87fb90673233
You cannot attach to a stopped container, start it first34
# 只要以attach进入的容器,则exit退出后容器也就会退出35
怎样才能避免上述的问题呢?
ctrl + p
ctr + q2
1
ctrl + p2
ctr + q实操
[root@VM_0_15_centos app]# docker attach 69dff30b6322
root@69dff30b6322:/# read escape sequence
[root@VM_0_15_centos app]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
69dff30b6322 nginx "/bin/bash" 13 days ago Up 20 seconds 80/tcp boring_brown
8e9313993a0a kinogmt/centos-ssh "/bin/bash" 13 days ago Exited (137) 12 days ago youthful_lichterman
6295c6878dcb kinogmt/centos-ssh "/bin/bash" 13 days ago Exited (137) 12 days ago musing_meninsky
6a1d463e4346 4cbe7aa905e7 "/bin/bash" 13 days ago Exited (127) 13 days ago cool_curie8
1
[root@VM_0_15_centos app]# docker attach 69dff30b63222
root@69dff30b6322:/# read escape sequence 3
[root@VM_0_15_centos app]# docker ps -a4
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES5
69dff30b6322 nginx "/bin/bash" 13 days ago Up 20 seconds 80/tcp boring_brown6
8e9313993a0a kinogmt/centos-ssh "/bin/bash" 13 days ago Exited (137) 12 days ago youthful_lichterman7
6295c6878dcb kinogmt/centos-ssh "/bin/bash" 13 days ago Exited (137) 12 days ago musing_meninsky8
6a1d463e4346 4cbe7aa905e7 "/bin/bash" 13 days ago Exited (127) 13 days ago cool_curie补充1 查看指定关键词的镜像
规范:
docker images <关键词>
实操:
[root@VM_0_15_centos app]# docker images nginx
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 540a289bab6c 5 weeks ago 126MB3
1
[root@VM_0_15_centos app]# docker images nginx2
REPOSITORY TAG IMAGE ID CREATED SIZE3
nginx latest 540a289bab6c 5 weeks ago 126MB补充2 怎样实现在启动容器的时候,当该容器退出的时候需要把这个容器也删除
规范:
docker run -it --rm centos /bin/bash1
1
docker run -it --rm centos /bin/bash实操
# 下面是启动一个容器,该容器在退出后也会默认删除
[root@VM_0_15_centos appdeploy]# docker run -it --rm centos /bin/bash
# 下面默认进入容器的bash 交互界面,得到其容器的id是578ad1ecf59
[root@578ad1ecf594 /]#
# 执行ctrl+p 和ctrl+q下的后,查看所有的运行的容器状态
[root@VM_0_15_centos appdeploy]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
578ad1ecf594 centos "/bin/bash" 17 seconds ago Up 16 seconds romantic_dewdney
69dff30b6322 nginx "/bin/bash" 13 days ago Up About an hour 80/tcp boring_brown
8e9313993a0a kinogmt/centos-ssh "/bin/bash" 13 days ago Up About an hour 0.0.0.0:1234->22/tcp youthful_lichterman
6295c6878dcb kinogmt/centos-ssh "/bin/bash" 13 days ago Exited (137) 12 days ago musing_meninsky
6a1d463e4346 4cbe7aa905e7 "/bin/bash" 13 days ago Exited (127) 13 days ago cool_curie
# 过滤容器id是578ad1ecf59的容器,发现该容器还在,并且状态是运行中
[root@VM_0_15_centos appdeploy]# docker ps -a | grep 578ad1ecf594
578ad1ecf594 centos "/bin/bash" 27 seconds ago Up 25 seconds romantic_dewdney
# 然后进入到容器中,并执行exit退出容器
[root@VM_0_15_centos appdeploy]# docker attach 578ad1ecf594
[root@578ad1ecf594 /]# exit
exit
# 查看容器id为578ad1ecf59的容器已经没有了,这就是默认被删除了
[root@VM_0_15_centos appdeploy]# docker ps -a | grep 578ad1ecf594
[root@0f08da061098 /]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="8 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="8"38
1
# 下面是启动一个容器,该容器在退出后也会默认删除2
[root@VM_0_15_centos appdeploy]# docker run -it --rm centos /bin/bash 3
# 下面默认进入容器的bash 交互界面,得到其容器的id是578ad1ecf594
[root@578ad1ecf594 /]# 5
# 执行ctrl+p 和ctrl+q下的后,查看所有的运行的容器状态6
[root@VM_0_15_centos appdeploy]# docker ps -a7
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES8
578ad1ecf594 centos "/bin/bash" 17 seconds ago Up 16 seconds romantic_dewdney9
69dff30b6322 nginx "/bin/bash" 13 days ago Up About an hour 80/tcp boring_brown10
8e9313993a0a kinogmt/centos-ssh "/bin/bash" 13 days ago Up About an hour 0.0.0.0:1234->22/tcp youthful_lichterman11
6295c6878dcb kinogmt/centos-ssh "/bin/bash" 13 days ago Exited (137) 12 days ago musing_meninsky12
6a1d463e4346 4cbe7aa905e7 "/bin/bash" 13 days ago Exited (127) 13 days ago cool_curie13
# 过滤容器id是578ad1ecf59的容器,发现该容器还在,并且状态是运行中14
[root@VM_0_15_centos appdeploy]# docker ps -a | grep 578ad1ecf59415
578ad1ecf594 centos "/bin/bash" 27 seconds ago Up 25 seconds romantic_dewdney16
# 然后进入到容器中,并执行exit退出容器17
[root@VM_0_15_centos appdeploy]# docker attach 578ad1ecf59418
[root@578ad1ecf594 /]# exit19
exit20
# 查看容器id为578ad1ecf59的容器已经没有了,这就是默认被删除了21
[root@VM_0_15_centos appdeploy]# docker ps -a | grep 578ad1ecf59422
[root@0f08da061098 /]# cat /etc/os-release 23
NAME="CentOS Linux"24
VERSION="8 (Core)"25
ID="centos"26
ID_LIKE="rhel fedora"27
VERSION_ID="8"28
PLATFORM_ID="platform:el8"29
PRETTY_NAME="CentOS Linux 8 (Core)"30
ANSI_COLOR="0;31"31
CPE_NAME="cpe:/o:centos:centos:8"32
HOME_URL="https://www.centos.org/"33
BUG_REPORT_URL="https://bugs.centos.org/"34
35
CENTOS_MANTISBT_PROJECT="CentOS-8"36
CENTOS_MANTISBT_PROJECT_VERSION="8"37
REDHAT_SUPPORT_PRODUCT="centos"38
REDHAT_SUPPORT_PRODUCT_VERSION="8"补充3 与docker images 命令一样的命令
规范
docker image ls1
1
docker image ls实操
[root@VM_0_15_centos appdeploy]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hsl_centos_with_ssh v1.0.1 0c4361666100 12 days ago 931MB
haoshuliang/centos test ee87dab68e9d 2 weeks ago 250MB
nginx latest 540a289bab6c 5 weeks ago 126MB
centos latest 0f3e07c0138f 2 months ago 220MB
kinogmt/centos-ssh latest dc8713dad282 3 years ago 773MB
[root@VM_0_15_centos appdeploy]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
hsl_centos_with_ssh v1.0.1 0c4361666100 12 days ago 931MB
haoshuliang/centos test ee87dab68e9d 2 weeks ago 250MB
nginx latest 540a289bab6c 5 weeks ago 126MB
centos latest 0f3e07c0138f 2 months ago 220MB
kinogmt/centos-ssh latest dc8713dad282 3 years ago 773MB14
1
[root@VM_0_15_centos appdeploy]# docker images2
REPOSITORY TAG IMAGE ID CREATED SIZE3
hsl_centos_with_ssh v1.0.1 0c4361666100 12 days ago 931MB4
haoshuliang/centos test ee87dab68e9d 2 weeks ago 250MB5
nginx latest 540a289bab6c 5 weeks ago 126MB6
centos latest 0f3e07c0138f 2 months ago 220MB7
kinogmt/centos-ssh latest dc8713dad282 3 years ago 773MB8
[root@VM_0_15_centos appdeploy]# docker image ls9
REPOSITORY TAG IMAGE ID CREATED SIZE10
hsl_centos_with_ssh v1.0.1 0c4361666100 12 days ago 931MB11
haoshuliang/centos test ee87dab68e9d 2 weeks ago 250MB12
nginx latest 540a289bab6c 5 weeks ago 126MB13
centos latest 0f3e07c0138f 2 months ago 220MB14
kinogmt/centos-ssh latest dc8713dad282 3 years ago 773MB补充4 查看镜像、 容器、 数据卷所占用的空间
规范
# 查看镜像和容器的使用情况
docker system df
# 查看具体的镜像和容器的使用情况
docker sysetm df -v4
1
# 查看镜像和容器的使用情况2
docker system df3
# 查看具体的镜像和容器的使用情况4
docker sysetm df -v实操
# 下面是查看镜像和容器的使用情况
[root@VM_0_15_centos appdeploy]# docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 5 2 1.307GB 1.181GB (90%)
Containers 4 2 315.8MB 157.9MB (50%)
Local Volumes 0 0 0B 0B
Build Cache 0 0 0B 0B
# 下面是查看具体的镜像和容器的使用情况
[root@VM_0_15_centos appdeploy]# docker system df -v
Images space usage:
REPOSITORY TAG IMAGE ID CREATED SIZE SHARED SIZE UNIQUE SIZE CONTAINERS
hsl_centos_with_ssh v1.0.1 0c4361666100 12 days ago 931MB 773.1MB 157.9MB 0
haoshuliang/centos test ee87dab68e9d 2 weeks ago 249.5MB 219.6MB 29.93MB 0
nginx latest 540a289bab6c 5 weeks ago 126.2MB 0B 126.2MB 1
centos latest 0f3e07c0138f 2 months ago 219.6MB 219.6MB 0B 0
kinogmt/centos-ssh latest dc8713dad282 3 years ago 773.1MB 773.1MB 0B 2
Containers space usage:
CONTAINER ID IMAGE COMMAND LOCAL VOLUMES SIZE CREATED STATUS NAMES
69dff30b6322 nginx "/bin/bash" 0 377B 13 days ago Up 2 hours boring_brown
8e9313993a0a kinogmt/centos-ssh "/bin/bash" 0 158MB 13 days ago Up 2 hours youthful_lichterman
6295c6878dcb kinogmt/centos-ssh "/bin/bash" 0 158MB 13 days ago Exited (137) 12 days ago musing_meninsky
6a1d463e4346 4cbe7aa905e7 "/bin/bash" 0 2.55kB 13 days ago Exited (127) 13 days ago cool_curie
Local Volumes space usage:
VOLUME NAME LINKS SIZE
Build cache usage: 0B
CACHE ID CACHE TYPE SIZE CREATED LAST USED USAGE SHARED33
1
# 下面是查看镜像和容器的使用情况2
[root@VM_0_15_centos appdeploy]# docker system df3
TYPE TOTAL ACTIVE SIZE RECLAIMABLE4
Images 5 2 1.307GB 1.181GB (90%)5
Containers 4 2 315.8MB 157.9MB (50%)6
Local Volumes 0 0 0B 0B7
Build Cache 0 0 0B 0B8
# 下面是查看具体的镜像和容器的使用情况9
[root@VM_0_15_centos appdeploy]# docker system df -v10
Images space usage:11
12
REPOSITORY TAG IMAGE ID CREATED SIZE SHARED SIZE UNIQUE SIZE CONTAINERS13
hsl_centos_with_ssh v1.0.1 0c4361666100 12 days ago 931MB 773.1MB 157.9MB 014
haoshuliang/centos test ee87dab68e9d 2 weeks ago 249.5MB 219.6MB 29.93MB 015
nginx latest 540a289bab6c 5 weeks ago 126.2MB 0B 126.2MB 116
centos latest 0f3e07c0138f 2 months ago 219.6MB 219.6MB 0B 017
kinogmt/centos-ssh latest dc8713dad282 3 years ago 773.1MB 773.1MB 0B 218
19
Containers space usage:20
21
CONTAINER ID IMAGE COMMAND LOCAL VOLUMES SIZE CREATED STATUS NAMES22
69dff30b6322 nginx "/bin/bash" 0 377B 13 days ago Up 2 hours boring_brown23
8e9313993a0a kinogmt/centos-ssh "/bin/bash" 0 158MB 13 days ago Up 2 hours youthful_lichterman24
6295c6878dcb kinogmt/centos-ssh "/bin/bash" 0 158MB 13 days ago Exited (137) 12 days ago musing_meninsky25
6a1d463e4346 4cbe7aa905e7 "/bin/bash" 0 2.55kB 13 days ago Exited (127) 13 days ago cool_curie26
27
Local Volumes space usage:28
29
VOLUME NAME LINKS SIZE30
31
Build cache usage: 0B32
33
CACHE ID CACHE TYPE SIZE CREATED LAST USED USAGE SHARED补充5 怎样清理docker 的空间
规范:
docker system prune1
1
docker system prune实操
#下面是清理之前空间的使用情况
[root@VM_0_15_centos appdeploy]# docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 5 2 1.307GB 1.181GB (90%)
Containers 4 2 315.8MB 157.9MB (50%)
Local Volumes 0 0 0B 0B
Build Cache 0 0 0B 0B
# 下面是清理空间的命令
[root@VM_0_15_centos appdeploy]# docker system prune
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all dangling build cache
Are you sure you want to continue? [y/N] y
Deleted Containers:
6295c6878dcb1f39ff16accd7e9de4ef7603b6648a3831234c015ed758079eab
6a1d463e4346156667f8bcea2a9acee9d61e2204e0b5118f27719860d88cd18c
Total reclaimed space: 157.9MB
#下面是清理后的空间显示情况,相比于清理之前发现还是有变化的
[root@VM_0_15_centos appdeploy]# docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 5 2 1.307GB 1.181GB (90%)
Containers 2 2 157.9MB 0B (0%)
Local Volumes 0 0 0B 0B
Build Cache 0 0 0B 0B28
1
#下面是清理之前空间的使用情况2
[root@VM_0_15_centos appdeploy]# docker system df3
TYPE TOTAL ACTIVE SIZE RECLAIMABLE4
Images 5 2 1.307GB 1.181GB (90%)5
Containers 4 2 315.8MB 157.9MB (50%)6
Local Volumes 0 0 0B 0B7
Build Cache 0 0 0B 0B8
# 下面是清理空间的命令9
[root@VM_0_15_centos appdeploy]# docker system prune10
WARNING! This will remove:11
- all stopped containers12
- all networks not used by at least one container13
- all dangling images14
- all dangling build cache15
16
Are you sure you want to continue? [y/N] y17
Deleted Containers:18
6295c6878dcb1f39ff16accd7e9de4ef7603b6648a3831234c015ed758079eab19
6a1d463e4346156667f8bcea2a9acee9d61e2204e0b5118f27719860d88cd18c20
21
Total reclaimed space: 157.9MB22
#下面是清理后的空间显示情况,相比于清理之前发现还是有变化的23
[root@VM_0_15_centos appdeploy]# docker system df24
TYPE TOTAL ACTIVE SIZE RECLAIMABLE25
Images 5 2 1.307GB 1.181GB (90%)26
Containers 2 2 157.9MB 0B (0%)27
Local Volumes 0 0 0B 0B28
Build Cache 0 0 0B 0Bnote:该指令默认会清除所有如下资源:
- 已停止的容器(container)
- 未被任何容器所使用的卷(volume)
- 未被任何容器所关联的网络(network)
- 所有悬空镜像(image)。
补充6 怎样清理没有用的镜像
规范
docker image prune1
1
docker image prune实操
[root@VM_0_15_centos appdeploy]# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
# 这里是需要清理没有用的镜像,但是由于我的机器上没有这种镜像的,所以在这里的演示没有任何的效果5
1
[root@VM_0_15_centos appdeploy]# docker image prune2
WARNING! This will remove all dangling images.3
Are you sure you want to continue? [y/N] y4
Total reclaimed space: 0B5
# 这里是需要清理没有用的镜像,但是由于我的机器上没有这种镜像的,所以在这里的演示没有任何的效果note: 这里prune的意思是修剪的意思,顾名思义就是要把没用的数据进行“修剪”掉
补充7 虚悬镜像(dangling image)
概念: 在我们在使用docker images 或者dokcer image ls 查看镜像的时候会有显示仓库名和标签都有<None>的,我们把这样的镜像称之为虚悬镜像,英文是dangling image,这里dangling就是这个意思了
Q1: 怎样查看虚悬镜像呢?
实操,ps:但是由于我的本地环境没有虚悬镜像,所以下面的演示也就没有什么参考价值,如果遇见虚悬镜像的话,我再进行补充
[root@VM_0_15_centos appdeploy]# docker images dangling=true
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@VM_0_15_centos appdeploy]# docker image ls dangling=true
REPOSITORY TAG IMAGE ID CREATED 4
1
[root@VM_0_15_centos appdeploy]# docker images dangling=true2
REPOSITORY TAG IMAGE ID CREATED SIZE3
[root@VM_0_15_centos appdeploy]# docker image ls dangling=true4
REPOSITORY TAG IMAGE ID CREATED Q2: 虚悬镜像是什么原因造成的呢?
原因1:曾经执行过docker pull的命令,这个命令会对镜像进行更新,新的镜像名字和tag与旧的镜像名字和tag是一样的,新的镜像肯定会把旧的名字和tag占用,这就不得不造成旧的image的名字和tage为<None>
原因 2:曾经执行过docker build 命令,在build的过程中,可能会存在对旧镜像的更新,这样会和docker pull一样的效果
结果:这样会造成docker 的旧的镜像也就是虚悬镜像没有任何价值,可以使用docker image prune进行删除
补充8 怎样查看中间层的镜像
规范
docker image ls -a
docker images -a2
1
docker image ls -a2
docker images -a实操
[root@VM_0_15_centos appdeploy]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
hsl_centos_with_ssh v1.0.1 0c4361666100 12 days ago 931MB
haoshuliang/centos test ee87dab68e9d 2 weeks ago 250MB
nginx latest 540a289bab6c 5 weeks ago 126MB
centos latest 0f3e07c0138f 2 months ago 220MB
kinogmt/centos-ssh latest dc8713dad282 3 years ago 773MB
[root@VM_0_15_centos appdeploy]# docker image ls -a
REPOSITORY TAG IMAGE ID CREATED SIZE
hsl_centos_with_ssh v1.0.1 0c4361666100 12 days ago 931MB
haoshuliang/centos test ee87dab68e9d 2 weeks ago 250MB
nginx latest 540a289bab6c 5 weeks ago 126MB
centos latest 0f3e07c0138f 2 months ago 220MB
kinogmt/centos-ssh latest dc8713dad282 3 years ago 773MB14
1
[root@VM_0_15_centos appdeploy]# docker images -a2
REPOSITORY TAG IMAGE ID CREATED SIZE3
hsl_centos_with_ssh v1.0.1 0c4361666100 12 days ago 931MB4
haoshuliang/centos test ee87dab68e9d 2 weeks ago 250MB5
nginx latest 540a289bab6c 5 weeks ago 126MB6
centos latest 0f3e07c0138f 2 months ago 220MB7
kinogmt/centos-ssh latest dc8713dad282 3 years ago 773MB8
[root@VM_0_15_centos appdeploy]# docker image ls -a9
REPOSITORY TAG IMAGE ID CREATED SIZE10
hsl_centos_with_ssh v1.0.1 0c4361666100 12 days ago 931MB11
haoshuliang/centos test ee87dab68e9d 2 weeks ago 250MB12
nginx latest 540a289bab6c 5 weeks ago 126MB13
centos latest 0f3e07c0138f 2 months ago 220MB14
kinogmt/centos-ssh latest dc8713dad282 3 years ago 773MB补充9 docker ps -a 与docker container ls --all的区别
补充10 已经有个镜像在以保护态度进行运行,怎样进入终端输入命令呢?
思路一: 可以首先进入到容器中,然后在进行输入命令
实操
[root@VM_0_15_centos dockerfile]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
69dff30b6322 nginx "/bin/bash" 13 days ago Up 3 hours 80/tcp boring_brown
8e9313993a0a kinogmt/centos-ssh "/bin/bash" 13 days ago Up 2 hours 0.0.0.0:1234->22/tcp youthful_lichterman
[root@VM_0_15_centos dockerfile]# docker attach 69df
root@69dff30b6322:/# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"15
1
[root@VM_0_15_centos dockerfile]# docker ps -a 2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3
69dff30b6322 nginx "/bin/bash" 13 days ago Up 3 hours 80/tcp boring_brown4
8e9313993a0a kinogmt/centos-ssh "/bin/bash" 13 days ago Up 2 hours 0.0.0.0:1234->22/tcp youthful_lichterman5
[root@VM_0_15_centos dockerfile]# docker attach 69df6
root@69dff30b6322:/# cat /etc/os-release 7
PRETTY_NAME="Debian GNU/Linux 10 (buster)"8
NAME="Debian GNU/Linux"9
VERSION_ID="10"10
VERSION="10 (buster)"11
VERSION_CODENAME=buster12
ID=debian13
HOME_URL="https://www.debian.org/"14
SUPPORT_URL="https://www.debian.org/support"15
BUG_REPORT_URL="https://bugs.debian.org/"思路二:执行docker exec的命令
规范
docker exec -it <container-id | contain-name> /bin/bash1
1
docker exec -it <container-id | contain-name> /bin/bash实操
[root@VM_0_15_centos appdeploy]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa05dabe6c67 ubuntu "/bin/bash" 43 minutes ago Up 43 minutes romantic_matsumoto
69dff30b6322 nginx "/bin/bash" 13 days ago Up 3 hours 80/tcp boring_brown
8e9313993a0a kinogmt/centos-ssh "/bin/bash" 13 days ago Up 3 hours 0.0.0.0:1234->22/tcp youthful_lichterman
[root@VM_0_15_centos appdeploy]# docker exec -it aa05 /bin/bash
root@aa05dabe6c67:/# cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.3 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.3 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic1
[root@VM_0_15_centos appdeploy]# docker ps -a2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3
aa05dabe6c67 ubuntu "/bin/bash" 43 minutes ago Up 43 minutes romantic_matsumoto4
69dff30b6322 nginx "/bin/bash" 13 days ago Up 3 hours 80/tcp boring_brown5
8e9313993a0a kinogmt/centos-ssh "/bin/bash" 13 days ago Up 3 hours 0.0.0.0:1234->22/tcp youthful_lichterman6
[root@VM_0_15_centos appdeploy]# docker exec -it aa05 /bin/bash7
root@aa05dabe6c67:/# cat /etc/os-release 8
NAME="Ubuntu"9
VERSION="18.04.3 LTS (Bionic Beaver)"10
ID=ubuntu11
ID_LIKE=debian12
PRETTY_NAME="Ubuntu 18.04.3 LTS"13
VERSION_ID="18.04"14
HOME_URL="https://www.ubuntu.com/"15
SUPPORT_URL="https://help.ubuntu.com/"16
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"17
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"18
VERSION_CODENAME=bionic19
UBUNTU_CODENAME=bionic补充11 仓库的相关操作
[root@VM_0_15_centos appdeploy]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: jasonhaoshuliang
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
[root@VM_0_15_centos appdeploy]# docker logout
Removing login credentials for https://index.docker.io/v1/
[root@VM_0_15_centos appdeploy]# echo $?
013
1
[root@VM_0_15_centos appdeploy]# docker login2
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.3
Username: jasonhaoshuliang4
Password: 5
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.6
Configure a credential helper to remove this warning. See7
https://docs.docker.com/engine/reference/commandline/login/#credentials-store8
9
Login Succeeded10
[root@VM_0_15_centos appdeploy]# docker logout11
Removing login credentials for https://index.docker.io/v1/12
[root@VM_0_15_centos appdeploy]# echo $?13
0补充12 怎样解决docker机器和宿主机时间不同步的问题
方法1:复制主机的localtime
docker cp /etc/localtime <container-id>:/etc/1
1
docker cp /etc/localtime <container-id>:/etc/实操:
[jasonhao@cvm-172_16_30_7 ~]$ docker cp /etc/localtime 7142e502f1ae:/etc/
[jasonhao@cvm-172_16_30_7 ~]$ docker attach 714
[root@7142e502f1ae tapdbase]# date
Tue Dec 10 10:42:23 CST 2019x
1
[jasonhao@cvm-172_16_30_7 ~]$ docker cp /etc/localtime 7142e502f1ae:/etc/2
[jasonhao@cvm-172_16_30_7 ~]$ docker attach 7143
[root@7142e502f1ae tapdbase]# date 4
Tue Dec 10 10:42:23 CST 2019方法2:在创建container时就应该共享主机的时间
docker run -ti -d --name my-nginx -v /etc/localtime:/etc/localtime:ro docker.io/nginx /bin/bashx
1
docker run -ti -d --name my-nginx -v /etc/localtime:/etc/localtime:ro docker.io/nginx /bin/bash待实操
方法3:创建镜像的时候就应该设置时区
Dockerfile的文件如下:
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone1
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
浙公网安备 33010602011771号