Docker
Docker概述
Docker为什么出现?
一款产品:开发–>上线
分别使用两套环境;应用环境、应用配置
开发 – 运维。问题:我在我的电脑上可以运行!他别人电脑环境不一样就运行不了
每一台机器都要部署环境(Redis集群、ES、Hadoop…)费时费力
发布一个项目(jar+(Redis MySQL jdk ES)),项目能不能都带上环境安装打包
之前在服务器配置一个应用环境 Redis MySQL jdk ES Hadoop,配置超麻烦,不能跨平台
Window开发,最后发布到Linux上出现环境问题
传统:开发jar --> 运维来做
现在:开发打包部署上线,一套流程做完
java – apk – 发布(应用商店)— 张三使用apk — 安装即可
java – jar(环境) – 打包项目带上环境(镜像)— (Docker仓库:商店) – 下载我们发布的镜像 – 直接运行即可
Docker给以上的问题,提出了解决方案!!!
Docker的思想就来源于集装箱
JRE – 多个应用(端口冲突) – 原来都是交叉的
隔离:Docker核心思想,打包装箱,每个箱子都是相互隔离的
Docker 通过隔离机制,可以将服务器利用到极致
Docker历史
2010年,几个的年轻人,就在美国成立了一家公司 dotcloud
做一些pass的云计算服务!LXC(Linux Container容器)有关的容器技术!
Linux Container容器是一种内核虚拟化技术,可以提供轻量级的虚拟化,以便隔离进程和资源。
他们将自己的技术(容器化技术)命名就是 Docker
Docker刚刚延生的时候,没有引起行业的注意!dotCloud
,就活不下去!
开源
2013年,Docker开源!
越来越多的人发现docker的优点!火了。Docker每个月都会更新一个版本!
2014年4月9日,Docker1.0发布!
docker为什么这么火?十分的轻巧!
在容器技术出来之前,我们都是使用虚拟机技术!
虚拟机:在window中装一个VMware,通过这个软件我们可以虚拟出来一台或者多台电脑!笨重!
虚拟机也属于虚拟化技术,Docker容器技术,也是一种虚拟化技术!
vm : linux centos 原生镜像(一个电脑!) 隔离、需要开启多个虚拟机! 几个G 几分钟
docker: 隔离,镜像(最核心的环境 4m + jdk + mysql)十分的小巧,运行镜像就可以了!小巧! 几个M 秒级启动!
聊聊Docker
Docker基于Go语言开发的!开源项目!
docker官网:https://www.docker.com/
文档:https://docs.docker.com/ Docker的文档是超级详细的!
仓库:https://hub.docker.com/
Docker能干嘛
之前的虚拟机技术!
虚拟机技术缺点:
1、 资源占用十分多
2、 冗余步骤多
3、 启动很慢!
容器化技术
容器化技术不是模拟一个完整的操作系统
比较Docker和虚拟机技术的不同:
- 传统虚拟机,虚拟出一条硬件,运行一个完整的操作系统,然后在这个系统上安装和运行软件
- 容器内的应用直接运行在宿主机的内容,
容器是没有自己的内核的
,也没有虚拟我们的硬件,所以就轻便
了 每个容器间是互相隔离
,每个容器内都有一个属于自己的文件系统,互不影响- 虚拟机是操作系统级,容器是应用级
DevOps(开发、运维)
应用更快速的交付和部署
传统:一对帮助文档,安装程序。
Docker:打包镜像发布测试一键运行。
更便捷的升级和扩缩容
使用了 Docker之后,我们部署应用就和搭积木一样
项目打包为一个镜像,扩展服务器A!服务器B
更简单的系统运维
在容器化之后,我们的开发,测试环境都是高度一致的
更高效的计算资源利用
Docker是内核级别的虚拟化,可以在一个物理机上可以运行很多的容器实例!服务器的性能可以被压榨到极致。
Docker安装
Docker的基本组成
镜像(image):
docker镜像就好比是一个目标,可以通过这个目标来创建容器服务,tomcat镜像==>run==>容器(提供服务器),通过这个镜像可以创建多个容器(最终服务运行或者项目运行就是在容器中的)。
容器(container):
Docker利用容器技术,独立运行一个或者一组应用,通过镜像来创建的.
启动,停止,删除,基本命令
目前就可以把这个容器理解为就是一个简易的 Linux系统
。
仓库(repository):
仓库就是存放镜像的地方!
仓库分为公有仓库和私有仓库。(很类似git)
Docker Hub(默认是国外的)。
阿里云…都有容器服务器(配置镜像加速!)
镜像好比一个类,容器就是一个实例
安装Docker
环境准备
1、Linux要求内核3.0以上
2、CentOS 7
3、使用连接远程服务器工具:Xshell、SecureCRT等…
# 系统内核是 3.10 以上的
[root@izbp1h9ynqgizuhp85e2xkz /]# uname -r
3.10.0-514.26.2.el7.x86_64
# 系统版本
[root@izbp1h9ynqgizuhp85e2xkz /]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
安装
帮助文档:https://docs.docker.com/engine/install/
#1.卸载旧版本
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
#3.设置镜像的仓库
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
#默认是从国外的,不推荐
#推荐使用国内的
yum-config-manager \
--add-repo \
https://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
#5、启动docker
systemctl start docker
#6. 使用docker version查看是否按照成功
docker version
#7. 测试
docker run hello-world
#7. 测试
➜ ~ docker run hello-world
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/
了解:卸载docker
#1. 卸载依赖
yum remove docker-ce docker-ce-cli containerd.io
#2. 删除资源
rm -rf /var/lib/docker
# /var/lib/docker 是docker的默认工作路径!
阿里云镜像加速
1、登录阿里云找到容器服务
2、找到镜像加速器
3、配置使用
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://wrxo0rap.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
回顾HelloWorld流程
docker run 流程图
底层原理
Docker是怎么工作的?
Docker是一个Client-Server结构的系统,Docker的守护进程运行在主机上。通过Socket从客户端访问!
Docker-Server接收到Docker-Client的指令,就会执行这个命令!
为什么Docker比Vm快
1、docker有着比虚拟机更少的抽象层。由于docker不需要Hypervisor实现硬件资源虚拟化,运行在docker容器上的程序直接使用的都是实际物理机的硬件资源。因此在CPU、内存利用率上docker将会在效率上有明显优势。
2、docker利用的是宿主机的内核,而不需要Guest OS。
GuestOS: VM(虚拟机)里的的系统(OS);
HostOS:物理机里的系统(OS);
因此,当新建一个 容器时,docker不需要和虚拟机一样重新加载一个操作系统内核。仍而避免引导、加载操作系统内核返个比较费时费资源的过程,当新建一个虚拟机时,虚拟机软件需要加载GuestOS,返个新建过程是分钟级别的。而docker由于直接利用宿主机的操作系统,则省略了这个复杂的过程,因此新建一个docker容器只需要几秒钟。
Docker的常用命令
帮助命令
docker version # 显示docker的版本信息
docker info # 显示docker的系统信息,包括镜像和容器数量
docker 命令 --help #帮助命令
帮助文档的地址:https://docs.docker.com/engine/reference/commandline/build/
镜像命令
docker images #查看所有本地主机上的镜像 可以使用docker image ls代替
docker search 搜索镜像
docker pull 下载镜像 docker image pull
docker rmi 删除镜像 docker image rm
docker images 查看所有本地的主机上的镜像
➜ ~ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 e73346bdf465 24 hours ago 448MB
# 解释
#REPOSITORY # 镜像的仓库源
#TAG # 镜像的标签
#IMAGE ID # 镜像的id
#CREATED # 镜像的创建时间
#SIZE # 镜像的大小
# 可选项
Options:
-a, --all Show all images (default hides intermediate images) #列出所有镜像
-q, --quiet Only show numeric IDs # 只显示镜像的id
➜ ~ docker images -aq #显示所有镜像的id
e73346bdf465
d03312117bb0
d03312117bb0
602e111c06b6
2869fc110bf7
470671670cac
bf756fb1ae65
5acf0e8da90b
docker search 搜索镜像
➜ docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10435 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3874 [OK]
# 可选项,通过收藏来过滤
--filter=STARS=3000 #搜索出来的镜像就是STARS大于3000的
➜ docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10435 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3874 [OK]
docker pull 下载镜像
# 下载镜像 docker pull 镜像名[:tag]
➜ ~ docker pull tomcat:8
8: Pulling from library/tomcat #如果不写tag,默认就是latest[最新版]
90fe46dd8199: Already exists #分层下载: docker image 的核心 联合文件系统
35a4f1977689: Already exists
bbc37f14aded: Already exists
74e27dc593d4: Already exists
93a01fbfad7f: Already exists
1478df405869: Pull complete
64f0dd11682b: Pull complete
68ff4e050d11: Pull complete
f576086003cf: Pull complete
3b72593ce10e: Pull complete
Digest: sha256:0c6234e7ec9d10ab32c06423ab829b32e3183ba5bf2620ee66de866df640a027 # 签名 防伪
Status: Downloaded newer image for tomcat:8
docker.io/library/tomcat:8 #真实地址
#等价于
docker pull tomcat:8
docker pull docker.io/library/tomcat:8
docker rmi 删除镜像
➜ ~ docker rmi -f 镜像id #删除指定的镜像
➜ ~ docker rmi -f 镜像id 镜像id 镜像id 镜像id#删除指定的镜像
➜ ~ docker rmi -f $(docker images -aq) #删除全部的镜像
容器命令
docker run 镜像id 新建容器并启动
docker ps 列出所有运行的容器 docker container list
docker rm 容器id 删除指定容器
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前正在运行的容器
docker kill 容器id #强制停止当前容器
说明:我们有了镜像才可以创建容器,Linux,下载centos镜像来学习
docker pull centos
新建容器并启动
docker run [可选参数] image | docker container run [可选参数] image
#参书说明
--name="Name" 容器名字 tomcat01 tomcat02 用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080(宿主机):8080(容器)
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
容器端口
-P(大写) 随机指定端口
# 测试、启动并进入容器 --> 进入云服务器里面的centos虚拟服务器
[root@izbp1h9ynqgizuhp85e2xkz ~]# docker run -it centos /bin/bash
[root@fbe3c94a5115 /]# ls # 查看容器内的centos
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@fbe3c94a5115 /]# exit #从容器退回主机
exit
➜ ~ ls
shell user.txt
列出所有运行的容器
#docker ps命令
-a # 显示所有容器,包括未运行的
-n=?, # 显示最近创建的容器 【可指定数量】
-q, # 只显示容器的编号
➜ ~ docker ps #列出当前正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
68729e9654d4 portainer/portainer "/portainer" 14 hours ago Up About a minute 0.0.0.0:8088->9000/tcp funny_curie
d506a017e951 nginx "nginx -g 'daemon of…" 15 hours ago Up 15 hours 0.0.0.0:3344->80/tcp nginx01
➜ ~ docker ps -a # 查看之前运行过的docker容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fbe3c94a5115 centos "/bin/bash" 4 minutes ago Exited (130) About a minute ago vigilant_elion
d5530524f89e hello-world "/hello" About an hour ago Exited (0) About an hour ago charming_pike
b2e51d3ee58d hello-world "/hello" 15 hours ago Exited (0) 15 hours ago jolly_cartwright
51af1d799f8e hello-world "/hello" 4 weeks ago Exited (0) 4 weeks ago stoic_jemison ecstatic_sutherland
➜ ~ docker ps -aq # 显示所有容器的id
95039813da8d
1e46a426a5ba
14bc9334d1b2
退出容器
exit # 容器直接退出
ctrl +Q +P # 容器不停止退出
删除容器
docker rm 容器id # 删除指定的容器,【不能删除正在运行的容器】,如果要强制删除 rm -rf
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 镜像名
➜ ~ docker run -d centos
a8f922c255859622ac45ce3a535b7a0e8253329be4756ed6e32265d2dd2fac6c
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 问题docker ps. 发现centos 停止了
# 常见的坑,docker容器使用后台运行,就必须要有要一个前台进程,docker发现没有应用,就会自动停止
# nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
查看日志
docker logs --help
Options:
--details Show extra details provided to logs
* -f, --follow Follow log output
--since string Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
* --tail string Number of lines to show from the end of the logs (default "all")
* -t, --timestamps Show timestamps
--until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
➜ ~ docker run -d centos /bin/sh -c "while true;do echo 6666;sleep 1;done" #模拟日志
#显示日志
-tf #显示日志信息(一直更新)
--tail number # number为需要显示日志条数
docker logs -t --tail n 容器id #查看n行日志
docker logs -ft 容器id #跟着日志
查看容器中进程信息 ps
# 命令 docker top 容器id
查看镜像的元数据
# 命令
docker inspect 容器id
#测试
# 查看历史容器
[root@izbp1h9ynqgizuhp85e2xkz /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
29089cc99506 centos "/bin/bash" 46 minutes ago Exited (0) 46 minutes ago quizzical_engelbart
dd22cc61ed6a centos "/bin/bash" 53 minutes ago Exited (0) 53 minutes ago wizardly_pike
99c06a553974 centos "/bin/bash" About an hour ago Exited (0) 52 minutes ago laughing_dhawan
fbe3c94a5115 centos "/bin/bash" About an hour ago Exited (130) About an hour ago vigilant_elion
b2e51d3ee58d hello-world "/hello" 16 hours ago Exited (0) 16 hours ago jolly_cartwright
51af1d799f8e hello-world "/hello" 4 weeks ago Exited (0) 4 weeks ago stoic_jemison
# 查看镜像的元数据
[root@izbp1h9ynqgizuhp85e2xkz /]# docker inspect 29089cc99506
[
{
"Id": "29089cc9950687d47c33efe312a40add951f0230aac193a5e52b24122fe188b1",
"Created": "2021-01-29T04:25:10.105487811Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-01-29T04:25:10.508781696Z",
"FinishedAt": "2021-01-29T04:25:10.508117569Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/29089cc9950687d47c33efe312a40add951f0230aac193a5e52b24122fe188b1/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/29089cc9950687d47c33efe312a40add951f0230aac193a5e52b24122fe188b1/hostname",
"HostsPath": "/var/lib/docker/containers/29089cc9950687d47c33efe312a40add951f0230aac193a5e52b24122fe188b1/hosts",
"LogPath": "/var/lib/docker/containers/29089cc9950687d47c33efe312a40add951f0230aac193a5e52b24122fe188b1/29089cc9950687d47c33efe312a40add951f0230aac193a5e52b24122fe188b1-json.log",
"Name": "/quizzical_engelbart",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {
}
},
"NetworkMode": "default",
"PortBindings": {
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/e98d368cc2047b2d1afffaebd738d6276d373b2068275774caadd5ecd5d5b763-init/diff:/var/lib/docker/overlay2/84a6babaf277418e01f693cbb9abe181e0f97149fff1179d9894d13802d64335/diff",
"MergedDir": "/var/lib/docker/overlay2/e98d368cc2047b2d1afffaebd738d6276d373b2068275774caadd5ecd5d5b763/merged",
"UpperDir": "/var/lib/docker/overlay2/e98d368cc2047b2d1afffaebd738d6276d373b2068275774caadd5ecd5d5b763/diff",
"WorkDir": "/var/lib/docker/overlay2/e98d368cc2047b2d1afffaebd738d6276d373b2068275774caadd5ecd5d5b763/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "29089cc99506",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20201204",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "7ba41fbec8e2b25fedf2c1b2942ddcdb95644229c3fa7e4f91ce6bfda5f14abb",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
},
"SandboxKey": "/var/run/docker/netns/7ba41fbec8e2",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "5ddc0658a32a058064f39d7cca1570f7c0f83fdfc048567121ecac1466b3833b",
"EndpointID": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]
进入当前正在运行的容器
# 我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置
# 方式一
# 命令
docker exec -it 容器id /bin/bash
#测试
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
55321bcae33d centos "/bin/sh -c 'while t…" 10 minutes ago Up 10 minutes bold_bell
a7215824a4db centos "/bin/sh -c 'while t…" 13 minutes ago Up 13 minutes zen_kepler
55a31b3f8613 centos "/bin/bash" 15 minutes ago Up 15 minutes lucid_clarke
➜ ~ docker exec -it 55321bcae33d /bin/bash
# 方式二
docker attach 容器id
#测试
docker attach 55321bcae33d
正在执行当前的代码...
区别
#docker exec #进入当前容器后开启一个新的终端,可以在里面操作。(常用)
#docker attach # 进入容器正在执行的终端
exec 与 attach 的区别
# docker exec # 进入容器后开启一个新的终端,可以在里面操作(常用)
# docker attach # 进入容器正在执行的终端,不会启动新的进程
从容器内拷贝到主机上
docker cp 容器id:容器内路径 主机目的路径
[root@izbp1h9ynqgizuhp85e2xkz home]# ls
- achang1 admin p redis www
[root@izbp1h9ynqgizuhp85e2xkz home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b26213219eee 300e315adb2f "/bin/bash" 2 minutes ago Up 2 minutes gracious_grothendieck
# 进入容器中
[root@izbp1h9ynqgizuhp85e2xkz home]# docker attach b26213219eee
[root@b26213219eee /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@b26213219eee /]# cd home
[root@b26213219eee home]# ls
# 在容器中,新建一个test.java文件
[root@b26213219eee home]# touch test.java
[root@b26213219eee home]# ls
test.java
# 退出容器
[root@b26213219eee home]# exit
exit
[root@izbp1h9ynqgizuhp85e2xkz home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@izbp1h9ynqgizuhp85e2xkz home]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b26213219eee 300e315adb2f "/bin/bash" 2 minutes ago Exited (0) 12 seconds ago gracious_grothendieck
# 将容器中的/home/test.java文件拷贝到 主机的/home下
[root@izbp1h9ynqgizuhp85e2xkz home]# docker cp b26213219eee:/home/test.java /home
#检查拷贝成功
[root@izbp1h9ynqgizuhp85e2xkz home]# ls
- achang1 admin p redis test.java www
学习方式:将我的所有笔记敲一遍,自己记录笔记!
小结:
attach Attach local standard input, output, and error streams to a running container
#当前shell下 attach连接指定运行的镜像
build Build an image from a Dockerfile # 通过Dockerfile定制镜像
commit Create a new image from a container's changes #提交当前容器为新的镜像
cp Copy files/folders between a container and the local filesystem #拷贝文件
create Create a new container #创建一个新的容器
diff Inspect changes to files or directories on a container's filesystem #查看docker容器的变化
events Get real time events from the server # 从服务获取容器实时时间
exec Run a command in a running container # 在运行中的容器上运行命令
export Export a container's filesystem as a tar archive #导出容器文件系统作为一个tar归档文件[对应import]
history Show the history of an image # 展示一个镜像形成历史
images List images #列出系统当前的镜像
import Import the contents from a tarball to create a filesystem image #从tar包中导入内容创建一个文件系统镜像
info Display system-wide information # 显示全系统信息
inspect Return low-level information on Docker objects #查看容器详细信息
kill Kill one or more running containers # kill指定docker容器
load Load an image from a tar archive or STDIN #从一个tar包或标准输入中加载一个镜像[对应save]
login Log in to a Docker registry #
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
感谢狂神