1.指定容器名字运行 (docker run --name)
# docker run --name dllhook -i -t -d piaoyun/ubuntu:16.04 /bin/sh fd293c88881d4405294658d0ea11c7f1eb173726cf27e0a7b16daff01374ba7b
2.启动/停止容器 (docker start/stop/restart)
# docker start dllhook // 启动 dllhook # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fd293c88881d piaoyun/ubuntu:16.04 "/bin/sh" 4 minutes ago Up 4 seconds dllhook # docker stop dllhook // 停止 dllhook # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3.外部访问容器 (docker run -p -P)
// 当使用 -P 标记时,Docker 会随机映射一个 49000~49900 的端口到内部容器开放的网络端口 # docker run -d -P training/webapp python app.py 9678e0e4500edf088f8a3fffbab28159d0456642063d5da7d55229edd24ddebf # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9678e0e4500e training/webapp "python app.py" 7 seconds ago Up 7 seconds 0.0.0.0:32769->5000/tcp stupefied_roentgen // 当使用-p(小写)标记时,则可以指定要映射的IP和端口,但是在一个指定端口上只可以绑定一个容器 # docker run -d -p 80:5000 training/webapp python app.py 65cfabd02c7286c12249ce173a86a70c04aae8ec81e9339174c534b362593cce # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 65cfabd02c72 training/webapp "python app.py" 5 seconds ago Up 4 seconds 0.0.0.0:80->5000/tcp adoring_mahavira
4.保存对容器的修改 (docker commit)
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] Create a new image from a container's changes // 先在home目录下添加一个test文件夹作为测试 # docker run -t -i piaoyun/ubuntu:16.04 /bin/bash root@851a5cbb9dbb:/# cd /home/ root@851a5cbb9dbb:/home# mkdir test root@851a5cbb9dbb:/home# ls -al total 12 drwxr-xr-x 4 root root 4096 Jun 27 08:25 . drwxr-xr-x 38 root root 4096 Jun 27 08:25 .. drwxr-xr-x 2 root root 4096 Jun 27 08:25 test // 另外开一个终端 # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 851a5cbb9dbb piaoyun/ubuntu:16.04 "/bin/bash" 2 minutes ago Up 2 minutes youthful_almeida # docker images REPOSITORY TAG IMAGE ID CREATED SIZE piaoyun/ubuntu 16.04 da5d00d78d23 8 minutes ago 114MB piaoyun/centos latest 6b9d49175255 3 hours ago 200MB ubuntu stack1 ffda2231bb83 27 hours ago 486MB ubuntu 16.04 5e8b97a2a082 3 weeks ago 114MB centos latest 49f7960eb7e4 3 weeks ago 200MB ubuntu 15.10 9b9cb95443b5 23 months ago 137MB training/webapp latest 6fae60ef3446 3 years ago 349MB # docker commit 851a5cbb9dbb piaoyun/ubuntu:16.04 // 提交更改 sha256:72b73d29380b86db697b00c0ccb644f4449abbde20e99aea14f442c6b83682dc root@piaopiao:/home/piaoyun# docker images REPOSITORY TAG IMAGE ID CREATED SIZE piaoyun/ubuntu 16.04 72b73d29380b 1 second ago 114MB // hash已改变 piaoyun/centos latest 6b9d49175255 3 hours ago 200MB ubuntu stack1 ffda2231bb83 27 hours ago 486MB ubuntu 16.04 5e8b97a2a082 3 weeks ago 114MB centos latest 49f7960eb7e4 3 weeks ago 200MB ubuntu 15.10 9b9cb95443b5 23 months ago 137MB training/webapp latest 6fae60ef3446 3 years ago 349MB
4.删除容器 (docker rm)
# docker rm -f dllhook
4.1.删除两个id相同的镜像 (docker rmi)
#docker images REPOSITORY TAG IMAGE ID CREATED SIZE piaoyun/centos latest 6b9d49175255 About an hour ago 200MB piaoyun/ubuntu 16.04 eb85479e6f47 2 hours ago 114MB registry.cn-hongkong.aliyuncs.com/chinapyg/ubuntu 16.04 eb85479e6f47 2 hours ago 114MB
有两个 镜像ID eb85479e6f47,删除一个
# docker rmi registry.cn-hongkong.aliyuncs.com/chinapyg/ubuntu:16.04 Untagged: registry.cn-hongkong.aliyuncs.com/chinapyg/ubuntu:16.04 Untagged: registry.cn-hongkong.aliyuncs.com/chinapyg/ubuntu@sha256:14d4516c31dd4fe56e33df344cd468ad323feca13f613b45fb7a48a09205b907
再次查看:
# docker images REPOSITORY TAG IMAGE ID CREATED SIZE piaoyun/centos latest 6b9d49175255 About an hour ago 200MB piaoyun/ubuntu 16.04 eb85479e6f47 2 hours ago 114MB
4.2.删除none镜像
# docker images REPOSITORY TAG IMAGE ID CREATED SIZE piaoyun/ubuntu 16.04 da5d00d78d23 About a minute ago 114MB <none> <none> 24dda671df60 2 minutes ago 114MB piaoyun/centos latest 6b9d49175255 3 hours ago 200MB // 此处有none镜像,直接删除会报错 // 按照下面步骤删除 # docker stop $(docker ps -a | grep "Exited" | awk '{print $1 }') // 停止容器 c791561c3361 d4cc476c6fc4 e1621bf62633 9f3e3bd661e2 # docker rm $(docker ps -a | grep "Exited" | awk '{print $1 }') // 删除容器 c791561c3361 d4cc476c6fc4 e1621bf62633 9f3e3bd661e2 64326e8e3531 # docker rmi $(docker images | grep "none" | awk '{print $3}') // 删除镜像 Deleted: sha256:24dda671df600d6833d5e47b9eccfebf7642c01f162c9bcbdd1259101a8a11a0 Deleted: sha256:f3ebd8e6ddf325e38a6f1af75065485ff9781555c76c8a6e0871c9d7fffcd740
5.修改TAG (docker tag)
docker tag IMAGEID(镜像id) REPOSITORY:TAG(仓库:标签) // 例子:docker tag eb85479e6f47 registry.cn-hongkong.aliyuncs.com/chinapyg/ubuntu:16.04
6.重命名容器名(先确保已经运行) (docker rename old new)
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a04d920e986d piaoyun/ubuntu:16.04 "/bin/sh" 13 seconds ago Up 12 seconds hopeful_liskov # docker rename hopeful_liskov piaoyun // 修改hopeful_liskov-->piaoyun # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a04d920e986d piaoyun/ubuntu:16.04 "/bin/sh" 33 seconds ago Up 32 seconds piaoyun
发表评论