列出镜像
cbc ~ # docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos 7 fd44297e2ddb 8 days ago 229.6 MB
centos centos7 fd44297e2ddb 8 days ago 229.6 MB
centos latest fd44297e2ddb 8 days ago 229.6 MB
<none> <none> ae0c2d0bdc10 5 months ago 224 MB
#REPOSITORY 镜像来源仓库
#TAG 镜像标记
#IMAGE ID 镜像唯一 ID
#CREATED 镜像创建时间
#VIRTUAL SIZE 镜像大小
查找镜像
这里我们是找一个名为tengine的镜像,tengine是淘宝版的nginx
cbc ~ # docker search tengine
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mimosa/tengine 1 [OK]
netengine/renderer-nginx 1 [OK]
utgard/tengine 0 [OK]
sugamele/tengine Tengine 0
vpanton/docker-tengine-dyups 0 [OK]
hyperboloide/tengine 0
guilhem30/updatengine 0 [OK]
获取镜像
cbc ~ # docker pull mimosa/tengine
Pulling repository mimosa/tengine
f614dc8befa6: Pulling dependent layers
511136ea3c5a: Download complete
5e66087f3ffe: Downloading 17.96 MB
# 下载过程中,会输出获取镜像的每一层信息。
# 你可以按照下面的格式从指定注册服务器的仓库下载相应标记的镜像:
docker pull 注册服务器地址[:端口号]/仓库名:TAG名称
删除镜像
docker rmi ae0c2d0bdc10
# 移除镜像前,先移除依赖该镜像的容器
使用镜像创建一个容器
格式 : docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
cbc ~ # docker run -itd --name tengine01 mimosa/tengine /bin/bash -v /tmp:/mnt
ca98201350dd0c30bc4f37992b22d70594ae0fabe1992457776e5cc5b113c9e7
# -t 让docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入
# -i 让容器的标准输入保持打开
# -d 让容器在后台以守护进程(Daemonized)形式运行
# --name 给容器起个名儿
# /bin/bash 启动一个bash终端,允许用户进行交互
# -v 把一个本地主机的目录当做数据卷挂载在容器上, 格式 : [host-dir]:[container-dir]:[rw|ro],
导出镜像到本地文件
cbc ~ # docker save -o tengine.tar mimosa/tengine
从本地文件导入镜像
docker load --input tengine.tar
或者
docker load < tengine.tar
查看容器状态
cbc ~ # docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ca98201350dd mimosa/tengine:latest "/bin/bash" 19 minutes ago Up 9 minutes 443/tcp, 80/tcp grave_albattani
b02c528c3cad centos:7 "/bin/bash" 2 hours ago Exited (127) 2 hours ago furious_einstein
cf769788d09e centos:7 "/bin/bash" 2 hours ago Exited (-1) 2 hours ago agitated_mclean
f706bbb258a1 ae0c2d0bdc10 "/bin/bash" 3 hours ago Exited (130) 3 hours ago goofy_yonath
574871d84dc2 ae0c2d0bdc10 "/bin/bash" 5 months ago Exited (127) 5 months ago cocky_wilson
47381902cd50 ae0c2d0bdc10 "/bin/bash" 5 months ago Exited (0) 5 months ago mad_feynman
ee19f69f978c ae0c2d0bdc10 "/bin/bash" 5 months ago Exited (0) 5 months ago high_bohr
a7e4a6d0c994 ae0c2d0bdc10 "/usr/bin/" 5 months ago drunk_pike
ad418b05441f ae0c2d0bdc10 "/bin/bash" 5 months ago sick_colden
dffb9f3f6677 ae0c2d0bdc10 "/bin/bash" 5 months ago Exited (0) 5 months ago determined_shockley
e9297baee616 ae0c2d0bdc10 "/bin/bash" 5 months ago Exited (0) 5 months ago thirsty_pare
从终止状态启动一个容器
cbc ~ # docker start ca98201350dd
ca98201350dd
停止容器
cbc ~ # docker stop ca98201350dd
重启容器
cbc ~ # docker restart ca98201350dd
ca98201350dd
进入一个运行状态的容器
cbc ~ # docker attach ca98201350dd
[ root@ca98201350dd:/etc/nginx ]$
删除容器
cbc ~ # docker rm 4d72d9a0066d
4d72d9a0066d
导出容器
docker export ca98201350dd > tengine.dock.tar
导入容器快照
cbc ~ # cat tengine.dock.tar | docker import - cbc009/tengine
67e225223716ff791c78bcc546695e530a37358e0c86b72a7a5bff2fbfbb6228
或从URL导入
docker import URL REPOSITORY
查看一下结果:
cbc ~ # docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
cbc009/tengine latest 67e225223716 2 minutes ago 593.5 MB
外部访问容器,对容器网络端口映射
cbc ~ # docker run -itdP mimosa/tengine /bin/bash
ca98201350dd0c30bc4f37992b22d70594ae0fabe1992457776e5cc5b113c9e7
# -P Docker会随机映射一个49000~49900的端口到内部容器开放的网络端口
# -p 参数小写的p可以手动指定要映射的端口。一个指定端口只可以绑定一个容器,也可以指定端口类型。支持的格式有:ip:hostPort:containerPort[/protocol] | ip::containerPort[/protocol] | hostPort:containerPort[/protocol]
# /bin/bash 启动一个bash终端,允许用户进行交互
# 用 docker ps -l查看最后一个被创建的容器,访问49154端口可以看到 tengine 的欢迎页。
# 现在我可以通过工作电脑访问容器的物理机:http://192.168.11.185:49154/
cbc ~ # docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1dd4e24df270 mimosa/tengine:latest "nginx" 7 minutes ago Up 7 minutes 0.0.0.0:49153->443/tcp, 0.0.0.0:49154->80/tcp naughty_hypatia
查看网络端口映射情况
docker port 1dd4e24df270 80
至此镜像和容器的基本操作学习完了~