欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

镜像与容器常用指令: 镜像常用指令练习 容器常用指令练习

程序员文章站 2022-03-14 09:57:24
...

3.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:镜像常用命令

1)查看后台运行的容器

[aaa@qq.com docker]# docker run -d docker.io/nginx  //启动nginx的镜像
[aaa@qq.com docker]# docker ps    //查看后台运行的容器
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
81458156f6e8        docker.io/nginx               "nginx -g 'daemon off"   9 seconds ago       Up 8 seconds        80/tcp              pedantic_goldberg

2)只显示容器ID

[aaa@qq.com docker]# docker ps -q
81458156f6e8

3)显示所有的容器,包括没有启动的

 [aaa@qq.com docker]# docker ps -a

4)显示所有的容器ID

[aaa@qq.com docker]# docker ps -qa
81458156f6e8
3656f1978967

5)查看centos镜像历史(制作过程),如图-2所示:

 [aaa@qq.com docker_images]# docker history docker.io/centos

镜像与容器常用指令: 镜像常用指令练习 容器常用指令练习

图-2

7)删除镜像,启动容器时删除镜像会失败,先删除容器,再删除镜像

格式:docker rmi 镜像名

[aaa@qq.com docker]# docker rmi nginx  //nginx为镜像名
Error response from daemon: conflict: unable to remove repository reference "nginx" (must force) - container 81458156f6e8 is using its referenced image 42b4762643dc  
//删除时报错
  
[aaa@qq.com docker]# docker stop 81
81
[aaa@qq.com docker]# docker rm 81
81
[aaa@qq.com ~]# docker rmi docker.io/nginx   //删除nginx镜像
Untagged: docker.io/nginx:latest
Deleted: sha256:42b4762643dcc9bf492b08064b55fef64942f055f0da91289a8abf93c6d6b43c
Deleted: sha256:e0e55dd2303b3e3ec852acae267d1f8a3eea27a22c64a5829304ecee4d3f559c
Deleted: sha256:4062cf272cdd99e83b1c21f712e5e1359c91ecf92925e56c62133c3324b84e45
Deleted: sha256:3c816b4ead84066ec2cadec2b943993aaacc3fe35fcd77ada3d09dc4f3937313

8)修改镜像的名称和标签,默认标签为latest

 [aaa@qq.com docker_images]# docker tag  docker.io/centos:latest docker.io/cen:v1
[aaa@qq.com ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos     latest              42b4762643dc        4 weeks ago         109.2 MB
docker.io/cen     v1                  42b4762643dc        4 weeks ago         109.2 MB

9)查看镜像的底层信息,如图-3所示:

[aaa@qq.com docker_images]# docker inspect docker.io/centos

镜像与容器常用指令: 镜像常用指令练习 容器常用指令练习

图-3

10)修改镜像的标签

[aaa@qq.com docker_images]# docker tag  docker.io/centos:latest docker.io/cen:v1 
[aaa@qq.com docker_images]# docker images
REPOSITORY   TAG            IMAGE ID          CREATED          SIZE
    docker.io/cen        v1             e934aafc2206   5 months ago    198.6 MB 
[aaa@qq.com docker_images]# docker rmi docker.io/centos    //删除centos
[aaa@qq.com ~]# docker run -it centos   
//启动的时候,因为是用标签标签启动的,所以会重新通过ID下载
[aaa@qq.com docker]# docker run -it docker.io/centos
Unable to find image 'centos:latest' locally
Trying to pull repository registry.access.redhat.com/centos ... 
Trying to pull repository docker.io/library/centos ... 
latest: Pulling from docker.io/library/centos
a02a4930cb5d: Pull complete 
Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426
[aaa@qq.com /]#
[aaa@qq.com ~]# docker run -it docker.io/cen:v1   
//通过新建的标签启动docker.io/cen:v1

步骤二:容器命令

1)关闭容器

命令:docker stop 容器ID

[aaa@qq.com docker]# docker stop 0f   //0f为容器ID
0f

2)启动容器

 [aaa@qq.com docker]# docker start 0f
0f

3)重启容器

 [aaa@qq.com docker]# docker restart 0f
0f

4)删除容器

运行中删除不掉,先关闭容器

[aaa@qq.com docker]# docker rm 0f  //删除失败
Error response from daemon: You cannot remove a running container 0f63706692e15134a8f07655a992771b312b8eb01554fc37e1a39b03b28dd05c. Stop the container before attempting removal or use -f
[aaa@qq.com docker]# docker stop 0f   //关闭容器
0f
[aaa@qq.com docker]# docker rm 0f         //删除成功
0f
[aaa@qq.com docker]#

5)连接容器attach|exec

[aaa@qq.com docker]# docker attach  0f
[aaa@qq.com docker]# docker ps    //容器关闭
CONTAINER ID  IMAGE  COMMAND  CREATED  STATUS  PORTS  NAMES
[aaa@qq.com docker]#  docker exec -it 0f /bin/bash
[aaa@qq.com docker]# docker ps        //容器不会关闭
CONTAINER ID  IMAGE         COMMAND        CREATED          STATUS          PORTS     NAMES
0b3c50284a1c  docker.io/centos      "/bin/bash"   15 minutes ago   Up 15 minutes             tiny_lamarr
[aaa@qq.com docker]# docker top f7        //查看容器进程列表 
[aaa@qq.com docker]# docker run -itd docker.io/centos:latest
76e7577cff5d8a3ec877d3ea564fed2fb502bd4a0447705bec269a646d414d07
[aaa@qq.com docker]# ps
  PID TTY          TIME CMD
 5552 pts/0    00:00:00 bash
 6739 pts/0    00:00:00 ps
[aaa@qq.com docker]# docker exec -it 76 /bin/bash
[aaa@qq.com /]# sleep 50 &
[1] 30
[aaa@qq.com /]# exit
exit
[aaa@qq.com docker]# docker top 76
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                6708                6691                0                   17:00               pts/1               00:00:00            /bin/bash
UID    PID  PPID C   STIME     TTY         TIME      CMD
root  2744 2729  0   18:01    pts/4  00:00:00   /bin/bash

6)过滤查看mac和ip地址

[aaa@qq.com docker]# docker inspect -f '{{.NetworkSettings.MacAddress}}' 76
02:42:ac:11:00:04
[aaa@qq.com docker]# docker inspect -f '{{.NetworkSettings.IPAddress}}' 76
172.17.0.4

7)修改nginx的显示内容

[aaa@qq.com docker_images]# docker run -it docker.io/nginx:latest 

镜像与容器常用指令: 镜像常用指令练习 容器常用指令练习

[aaa@qq.com docker_images]# docker exec -it 56 /bin/bash
aaa@qq.com:/# nginx -T /usr/share/nginx/html/
nginx: invalid option: "/usr/share/nginx/html/"   //查找并显示结果
aaa@qq.com:/# echo  aaa > /usr/share/nginx/html/index.html 
//修改主页显示的内容
aaa@qq.com:/# nginx -T
aaa@qq.com:/# cat /usr/share/nginx/html/index.html 
aaa

8)过滤查看nginx的ip地址

[aaa@qq.com docker]# docker inspect -f '{{.NetworkSettings.IPAddress}}' a6
172.17.0.3
[aaa@qq.com docker]# curl 172.17.0.3
aaa
相关标签: CLOUD