Docker架构(二)容器操作
容器是Docker的令一个可行概念,容器就是镜像的运行实例,是独立运行的一个或一组应用
(1)Docker容器的创建和启动
容器创建就是镜像加载过程
命令格式:docker create [选项] 镜像 运行程序
[[email protected] ~]# docker create -it httpd /bin/bash
4ca3aa48ebff4d01c01dcb6a90d72e66bf6f3a04b1849b08df89872a0677ed24
i:让容器的输入保持打开
t:让Docker分配一个伪终端
使用docker create 命令创建新容器会返回一个唯一id
用使用docker ps -a查看容器运行状态
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4ca3aa48ebff httpd "/bin/bash" 5 minutes ago Created dreamy_kare
fc26c0cf3ffc hello-world "--registry-mirror..." 2 hours ago Created adoring_bhabha
用docker start 容器的id启动容器status显示up开启状态
[[email protected] ~]# docker start 4ca3aa48ebff
4ca3aa48ebff
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4ca3aa48ebff httpd "/bin/bash" 6 minutes ago Up 7 seconds 80/tcp dreamy_kare
fc26c0cf3ffc hello-world "--registry-mirror..." 2 hours ago Created
如果要运行容器用docker run [容器id]
注:如果直接运行的话他只会运行一次如果你退出了把它就不再运行,看下列命令
[[email protected] ~]# docker run 4ca3aa48ebff
Unable to find image ‘4ca3aa48ebff:latest‘ locally
‘‘‘‘这边实在运行状态如果你不运行了可以看一下它的状态
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ad5636e15a45 httpd "httpd-foreground" 24 seconds ago Exited (0) 4 seconds ago trusting_babbage
现在状态处于推出状态exit
如果想一直运行的话
#1:加入shell语句死循环语句
[[email protected] ~]# docker run -d httpd /bin/bash -c "while true;do echo hello;done"
92f7c581850c6c0bca07e4cbc1ba3c0e9d3b2884de27b9370095c6d7f9f5e205
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
92f7c581850c httpd "/bin/bash -c ‘whi..." 4 seconds ago Up 3 seconds 80/tcp admiring_northcutt
#2:不加while语句加上端口
[[email protected] ~]# docker run -d -p 80:80 11426a19f1a2(后面不接容器id接镜像id)
41298e2d211b0c28df773a577587dc2ac3be06522d0a3776cb2bfdd9bb997b04
#终止容器运行用stop
删除运行镜像用docker rmi [进程号]
进入容器docker exec -it [容器id] /bin/bash
[[email protected] ~]# docker exec -it 4ca3aa48ebff /bin/bash
[email protected]:/usr/local/apache2# ls
bin build cgi-bin conf error htdocs icons include logs modules
#用户可以通过创建的终端来输入命令exit退出
容器的导入和导出
命令格式:docker export 容器ID/名称 > 文件名
例如:导出 546675d49307 容器到文件centod7tar
[[email protected] apache] docker images | grep 546675d49307
[[email protected] apache] ls -l centos7tar
-rw-r--r--. 1 root root 306354176 8月 13 11:49 centos7tar
[[email protected] apache] cat centos7tar | docker import - centos7:test
sha256:4d2e8905488cc688aa4e9f6d6df76d474ea25284902edd1f092b6b07c41041d0
[[email protected] apache] docker images | grep centos7
centos7 test 4d2e8905488c 22 seconds ago 298MB
容器的删除
命令格式:docker rm 容器id/名称
例如:删除ID号为70d968aa6ef5的容器
[[email protected] apache] docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0fcdbc61490c fa6224949e21 "/bin/sh -c ‘EXPOS..." 41 minutes ago Exited (127) 41 minutes ago quirky_newton
[[email protected] apache] docker rm 0fcdbc61490c
0fcdbc61490c
[[email protected] apache] docker ps -a | grep 0fcdbc61490c
原文地址:http://blog.51cto.com/13645280/2159029
时间: 2024-10-09 21:17:24