DOCKER 相关的一些用法
Posted ssaylo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DOCKER 相关的一些用法相关的知识,希望对你有一定的参考价值。
这就是 docker 官网 tutorial 的一个流程
my first docker app
- Create a file named Dockerfile with the following contents.
FROM node:12-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "/app/src/index.js"]
- Build the container image using the docker build command.
// build 打上一个 tag
docker build -t getting-started
- Start your container using the docker run command and specify the name of the image we just created:
// 把 docker 里面的 3000 端口暴露到 3000 端口上
docker run -dp 3000:3000 getting-started
然后可以从 dashboard 里面看到 docker container 里的 image 的运行情况
Updating our App
- adding and save your code
- Let‘s build our updated version of the image, using the same command we used before.
// 重新打个标记
docker build -t getting-started
- Let‘s start a new container using the updated code.
// 启动 docker
docker run -dp 3000:3000 getting-started
Uh oh! You probably saw an error like this (the IDs will be different):
// 出错啦
docker: Error response from daemon: driver failed programming external connectivity on endpoint laughing_burnell
(bb242b2ca4d67eba76e79474fb36bb5125708ebdabd7f45c8eaf16caaabde9dd): Bind for 0.0.0.0:3000 failed: port is already allocated.
so,Replacing our Old Container? or using an another a port
- Get the ID of the container by using the docker ps command.
// 查看 docker 正在跑的进程与端口
docker ps
- Use the docker stop command to stop the container.
//Swap out <the-container-id> with the ID from docker ps
docker stop <the-container-id>
- Once the container has stopped, you can remove it by using the docker rm command
docker rm <the-container-id>
- finally:
docker run -dp 3000:3000 getting-started
your new app is launching
Docker操作
在 Docker 中删除 image 时有时会遇到类似
Error response from daemon: conflict: unable to delete 6ec9a5a0fc9f (cannot be forced) - image has dependent child images
这样的错误,原因是有另外的 image FROM 了这个 image,可以使用下面的命令列出所有在指定 image 之后创建的 image 的父 image
docker image inspect --format=‘{{.RepoTags}} {{.Id}} {{.Parent}}‘ $(docker image ls -q --filter since=xxxxxx)
其中 xxxxxx 是报错 image 的 id,在文章开头的例子中就是 6ec9a5a0fc9f。从列表中查找到之后就可以核对并删除这些 image。
示例
1、查看我的镜像列表。
? ~ docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
tingfeng/dockerfile_build_demo latest 6586e000b464 8 hours ago 179MB
tingfeng/commit_test latest 58fac7144497 31 hours ago 234MB
tomcat latest 61205f6444f9 2 weeks ago 467MB
ubuntu latest 113a43faa138 2 weeks ago 81.2MB
nginx latest cd5239a0906a 2 weeks ago 109MB
hello-world latest e38bc07ac18e 2 months ago 1.85kB
2、删除none的镜像(删不掉)
? ~ docker rmi b707620d204c
Error response from daemon: conflict: unable to delete b707620d204c (cannot be forced) - image has dependent child images
? ~ docker rmi 97ea9f11c94f
Error response from daemon: conflict: unable to delete 97ea9f11c94f (cannot be forced) - image has dependent child images
? ~ docker rmi -f 54f305491871
Error response from daemon: conflict: unable to delete 54f305491871 (cannot be forced) - image has dependent child images
3、查找出所有在指定 image 之后创建的 image 的父 image,本示例看得出是同一个依赖镜像 tingfeng/dockerfile_build_demo
? ~ docker image inspect --format=‘{{.RepoTags}} {{.Id}} {{.Parent}}‘ $(docker image ls -q --filter since=b707620d204c)
[tingfeng/dockerfile_build_demo:latest] sha256:6586e000b464654f420b0aa9cf6c3c61cc29edfbbe7cc5cb5d6e0fe037efaf87 sha256:b707620d204ca475f13394b14614e1f2fde986931c925cd8cc8e8bb3de7debe3
? ~ docker image inspect --format=‘{{.RepoTags}} {{.Id}} {{.Parent}}‘ $(docker image ls -q --filter since=54f305491871)
[tingfeng/dockerfile_build_demo:latest] sha256:6586e000b464654f420b0aa9cf6c3c61cc29edfbbe7cc5cb5d6e0fe037efaf87 sha256:b707620d204ca475f13394b14614e1f2fde986931c925cd8cc8e8bb3de7debe3
? ~ docker image inspect --format=‘{{.RepoTags}} {{.Id}} {{.Parent}}‘ $(docker image ls -q --filter since=97ea9f11c94f)
[tingfeng/dockerfile_build_demo:latest] sha256:6586e000b464654f420b0aa9cf6c3c61cc29edfbbe7cc5cb5d6e0fe037efaf87 sha256:b707620d204ca475f13394b14614e1f2fde986931c925cd8cc8e8bb3de7debe3
4、删除关联的依赖镜像,关联的none镜像也会被删除
? ~ docker rmi 6586e000b464
Untagged: tingfeng/dockerfile_build_demo:latest
Deleted: sha256:6586e000b464654f420b0aa9cf6c3c61cc29edfbbe7cc5cb5d6e0fe037efaf87
Deleted: sha256:b707620d204ca475f13394b14614e1f2fde986931c925cd8cc8e8bb3de7debe3
Deleted: sha256:c241c7f781a3176d395b38a7e96eb2e0b7e031e622ad9d14eaa9098de1a063d1
Deleted: sha256:54f305491871f5609295cd6c59f304401761c7fa96bdda8a74968358c54ba402
Deleted: sha256:be4d80c4407bde1fe700983ad805a0237a148d7af04e8bf2197fc040ae654acb
Deleted: sha256:97ea9f11c94fb8f76288361e37f884d639c6ea918bc6142feee2409e7ff43791
5、再次查看镜像列表
? ~ docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
tingfeng/commit_test latest 58fac7144497 31 hours ago 234MB
tomcat latest 61205f6444f9 2 weeks ago 467MB
ubuntu latest 113a43faa138 2 weeks ago 81.2MB
nginx latest cd5239a0906a 2 weeks ago 109MB
hello-world latest e38bc07ac18e 2 months ago 1.85kB
其他操作
停止所有容器
? ~ docker ps -a | grep "Exited" | awk ‘{print $1 }‘|xargs docker stop
删除所有容器
? ~ docker ps -a | grep "Exited" | awk ‘{print $1 }‘|xargs docker rm
删除所有none容器
? ~ docker images|grep none|awk ‘{print $3 }‘|xargs docker rmi
相关阅读
删除docker-register的镜像& none无效镜像讲解:https://segmentfault.com/a/1190000011153919
以上是关于DOCKER 相关的一些用法的主要内容,如果未能解决你的问题,请参考以下文章