一 镜像操作
Docker镜像类似于虚拟机镜像,可以将它理解为一个只读的模板
查看镜像相关:
1使用docker images
列出镜像
root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
microsoft/dotnet 2.1-sdk 6e1c48456450 9 days ago 1.73GB
microsoft/dotnet latest 389d91a8617b 9 days ago 1.74GB
microsoft/dotnet 2.1-aspnetcore-runtime e56d8091e0bf 9 days ago 253MB
hello-world latest 4ab4c602aa5e 3 months ago 1.84kB
microsoft/aspnetcore latest db030c19e94b 4 months ago 347MB
- REPOSITORY :镜像来自哪个仓库microsoft/dotnet表示microsoft/dotnet系列的基础镜像
- TAG :镜像的标签信息,表示镜像的不同版本信息,比如2.1-sdk,latest
- IMAGE ID :镜像的ID,如果两个镜像的ID相同,说明他们实际指向同一个镜像,只是具有不同的标签名而已
- CREATED :镜像的创建时间
- SIZE :镜像的大小,优秀的镜像往往体积比较小
2使用docker [images] inspect microsoft/dotnet:latest
:查看镜像的详细信息
3查看镜像历史
镜像文件是由多个层组成,用history可以查看各个层的具体内容
docker history microsoft/dotnet:latest
创建镜像相关:
docker build
命令用于使用 Dockerfile 创建镜像。
删除镜像相关:
1 删除镜像:
docker rmi -f webapplication1:dev
-f:强制删除
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
PS E:\发布\DockerPub> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
webapplication1 dev d285cd20ce0c 22 minutes ago 253MB
mycore 1.0 0d832cd9f63a 25 minutes ago 255MB
<none> <none> 29d4f7375466 22 hours ago 253MB
microsoft/dotnet 2.1-aspnetcore-runtime e56d8091e0bf 7 days ago 253MB
microsoft/dotnet-samples aspnetapp d20323c39641 2 weeks ago 263MB
nginx latest 568c4670fa80 4 weeks ago 109MB
ubuntu latest 93fd78260bd1 5 weeks ago 86.2MB
docker4w/nsenter-dockerd latest 2f1c802f322f 2 months ago 187kB
hello-world latest 4ab4c602aa5e 3 months ago 1.84kB
PS E:\发布\DockerPub> docker rmi webapplication1
Error: No such image: webapplication1
PS E:\发布\DockerPub> docker rmi webapplication1:dev
Error response from daemon: conflict: unable to remove repository reference "webapplication1:dev" (must force) - container 14838a2485dc is using its referenced image d285cd20ce0c
PS E:\发布\DockerPub> docker rmi -f webapplication1:dev
Untagged: webapplication1:dev
Deleted: sha256:d285cd20ce0cedd77e1c4ebd427394850c591561eb23dd9304f9b16056e3fd5c
Deleted: sha256:d8bd39ea0171d99a9e2c244f1925ac1951ded0b4c3ac7386f3e25eb7b313556c
Deleted: sha256:ddd7a08677dcb7975c572dda7e22ddced463d5d84231d9efca64a1e6008be703
PS E:\发布\DockerPub> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycore 1.0 0d832cd9f63a 29 minutes ago 255MB
<none> <none> 29d4f7375466 22 hours ago 253MB
microsoft/dotnet 2.1-aspnetcore-runtime e56d8091e0bf 7 days ago 253MB
microsoft/dotnet-samples aspnetapp d20323c39641 2 weeks ago 263MB
nginx latest 568c4670fa80 4 weeks ago 109MB
ubuntu latest 93fd78260bd1 5 weeks ago 86.2MB
docker4w/nsenter-dockerd latest 2f1c802f322f 2 months ago 187kB
hello-world latest 4ab4c602aa5e 3 months ago 1.84kB
2 删除none镜像:
docker rmi $(docker images -f "dangling=true" -q)
查找新镜像
docker search +镜像名
二 容器操作:
Docker容器类似于一个轻量级的沙箱,Docker利用容器来运行和隔离应用。
容器是从镜像创建的应用运行实例。它可以启动、开始、停止、删除。这些容器都是彼此相互隔离、互不可见的。
可以把容器看作一个简易版的Linux系统环境(包括root用户权限、进程空间、用户空间及网络设置等)以及运行在其中的应用程序打包而成的盒子。
1停止所有的container:docker stop $(docker ps -a -q)
启动容器:docker start + ContainerName
如: docker start fcjcontiner3
2列出所有的容器,包括运行的容器和没有运行的容器:docker container ls --all
:
PS C:\WINDOWS\system32> docker container ls --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
df17c013fd04 ubuntu "bash" 6 minutes ago Exited (0) 13 seconds ago confident_bohr
73a030242922 ubuntu "/bin/bash" 20 minutes ago Exited (0) 20 minutes ago reverent_babbage
9ee167ad1eb0 hello-world "/hello" 17 hours ago Exited (0) 17 hours ago cranky_mcnulty
b2ac573fc296 hello-world "/hello" 17 hours ago Exited (0) 17 hours ago jovial_bassi
3查看运行中的容器
使用 docker ps
或者 docker ps -l
root@ubuntu:~# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
39e5dce75509 qwemvc:1.0 "dotnet WebApplicati…" 9 days ago Up 12 minutes 0.0.0.0:60005->60005/tcp fcjcontiner3
root@ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
39e5dce75509 qwemvc:1.0 "dotnet WebApplicati…" 9 days ago Up 13 minutes 0.0.0.0:60005->60005/tcp fcjcontiner3
4 查看容器的详细信息:docker logs +容器名
如:查看容器: fcjcontiner3的信息
root@ubuntu:~# docker logs fcjcontiner3
warn:
Hosting environment: Production
Content root path: /app
Now listening on: http://0.0.0.0:60005
Application started. Press Ctrl+C to shut down.
Application is shutting down...
5进入到容器中
docker exec -it 39e5dce75509 /bin/bash
39e5dce75509
:容器的id
/bin/bash
:进入容器后打开一个新的bash终端
如:
root@ubuntu:~# docker exec -it 39e5dce75509 /bin/bash
root@39e5dce75509:/app# dotnet --info
Host (useful for support):
Version: 2.1.6
Commit: 3f4f8eebd8
.NET Core SDKs installed:
No SDKs were found.
.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.6 [/usr/share/dotnet/shared/Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.6 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.6 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
To install additional .NET Core runtimes or SDKs:
https://aka.ms/dotnet-download
推出:
exit
root@39e5dce75509:/app# exit
exit
6 删除容器
可以使用"docker rm 容器id"来删除一个终止状态的容器;若要删除一个运行中的容器,需要加-f参数。
镜像自身是可读的。容器从镜像启动的时候,会在镜像的最上层创建一个可写层。
网友评论