3 - Docker 容器常用操作

 

1.获取镜像

如果我们本地没有 ubuntu 镜像,我们可以使用 docker pull 命令来载入 ubuntu 镜像:

docker pull ubuntu

2.启动容器

使用 ubuntu 镜像启动一个容器,参数为以命令行模式进入该容器:

[root@VM-4-13-centos /]# docker run -it ubuntu /bin/bash
root@b53077e08d9d:/# 

参数说明:

  • -i: 交互式操作。
  • -t: 终端。
  • ubuntu: ubuntu 镜像。
  • /bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash。

退出终端:

root@b53077e08d9d:/# exit
exit
[root@VM-4-13-centos /]#

3.启动已停止运行的容器

  • 查看所有的容器:
[root@VM-4-13-centos /]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS                      PORTS     NAMES
b53077e08d9d   ubuntu         "/bin/bash"              4 minutes ago   Exited (0) 2 minutes ago              tender_solomon
e534d2229a24   httpd          "httpd-foreground"       18 hours ago    Exited (0) 18 hours ago               happy_ptolemy
[root@VM-4-13-centos /]# 
  • 启动一个已经停止的容器
[root@VM-4-13-centos /]# docker start e534d2229a24
e534d2229a24
[root@VM-4-13-centos /]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS                      PORTS     NAMES
b53077e08d9d   ubuntu         "/bin/bash"              5 minutes ago   Exited (0) 3 minutes ago              tender_solomon
e534d2229a24   httpd          "httpd-foreground"       18 hours ago    Up 2 seconds                80/tcp    happy_ptolemy
  • 输出详情介绍

CONTAINER ID: 容器 ID。

IMAGE: 使用的镜像。

COMMAND: 启动容器时运行的命令。

CREATED: 容器的创建时间。

STATUS: 容器状态。

状态有7种:

    • created(已创建)
    • restarting(重启中)
    • running 或 Up(运行中)
    • removing(迁移中)
    • paused(暂停)
    • exited(停止)
    • dead(死亡)

      PORTS: 容器的端口信息和使用的连接类型(tcp\udp)。

      NAMES: 自动分配的容器名称。

 

4.运行容器

  • -t: 在新容器内指定一个伪终端或终端
  • -i: 允许对容器内的标准输入 (STDIN) 进行交互
  • -d: 指定容器的运行模式,在大部分的场景下,我们希望 docker 的服务是在后台运行的
[root@VM-4-13-centos /]# docker run -itd --name ubuntu-test ubuntu /bin/bash
ff7077c6eb9dfb4e74dee36f729f8d642a3c5beb96aad3b372651be3c3532f84
[root@VM-4-13-centos /]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                      PORTS     NAMES
ff7077c6eb9d   ubuntu         "/bin/bash"              10 seconds ago   Up 9 seconds                          ubuntu-test
b53077e08d9d   ubuntu         "/bin/bash"              8 minutes ago    Exited (0) 6 minutes ago              tender_solomon
e534d2229a24   httpd          "httpd-foreground"       18 hours ago     Up 2 minutes                80/tcp    happy_ptolemy
940589c9a637   ubuntu:15.10   "/bin/sh -c 'while t…"   21 hours ago     Exited (137) 21 hours ago             brave_almeida
4b67411b6735   ubuntu:15.10   "/bin/sh -c 'while t…"   22 hours ago     Exited (137) 21 hours ago             admiring_colden
955f76e8f4df   ubuntu:15.10   "/bin/bash"              22 hours ago     Exited (0) 22 hours ago               blissful_stonebraker
e815a2157b62   ubuntu:15.10   "/bin/echo 'Hello wo…"   22 hours ago     Exited (0) 22 hours ago               gifted_mestorf
4eaae4408a00   hello-world    "/hello"                 22 hours ago     Exited (0) 22 hours ago               eager_borg
[root@VM-4-13-centos /]# 

注:加了 -d 参数默认不会进入容器,想要进入容器需要使用指令 docker exec

5.退出容器

可以通过运行 exit 命令或者使用 CTRL+D 来退出容器


root@ff7077c6eb9d:# exit
exit

[root@VM-4-13-centos /]#

注意第三行中 [root@VM-4-13-centos /]# 表明我们已经退出了当前的容器,返回到当前的主机中

6.停止一个容器

[root@VM-4-13-centos /]# docker stop b53077e08d9d
b53077e08d9d
[root@VM-4-13-centos /]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                      PORTS     NAMES
ff7077c6eb9d   ubuntu         "/bin/bash"              2 minutes ago    Up 2 minutes                          ubuntu-test
b53077e08d9d   ubuntu         "/bin/bash"              10 minutes ago   Exited (0) 8 minutes ago              tender_solomon

7.进入容器

在使用 -d 参数时,容器启动后会进入后台。此时想要进入容器,可以通过以下指令进入:

  • docker attach

  • docker exec:推荐大家使用 docker exec 命令,因为此命令会退出容器终端,但不会导致容器的停止。

1) attach命令:  如果从这个容器退出,会导致容器的停止

[root@VM-4-13-centos /]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                      PORTS     NAMES
ff7077c6eb9d   ubuntu         "/bin/bash"              4 minutes ago    Up 4 minutes                          ubuntu-test
[root@VM-4-13-centos /]# docker attach ff7077c6eb9d
root@ff7077c6eb9d:/# exit
exit
[root@VM-4-13-centos /]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                      PORTS     NAMES
ff7077c6eb9d   ubuntu         "/bin/bash"              6 minutes ago    Exited (0) 4 seconds ago              ubuntu-test

2)exec命令:(如果从这个容器退出,容器不会停止,这就是为什么推荐大家使用 docker exec 的原因)

[root@VM-4-13-centos /]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                      PORTS     NAMES
[root@VM-4-13-centos /]# docker run -itd --name ubuntu-test1 ubuntu /bin/bash
97f3fefb651c1556a7596041d3401c3e1fba2b8fbe1f3d35efb998d9ac70977b
[root@VM-4-13-centos /]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                      PORTS     NAMES
97f3fefb651c   ubuntu         "/bin/bash"              2 seconds ago    Up 2 seconds                          ubuntu-test1
[root@VM-4-13-centos /]# docker exec -it 97f3fefb651c  /bin/bash
root@97f3fefb651c:/# exit
exit
[root@VM-4-13-centos /]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                      PORTS     NAMES
97f3fefb651c   ubuntu         "/bin/bash"              54 seconds ago   Up 53 seconds                         ubuntu-test1

8.导出和导入容器

  • 导出容器 (导出本地某个容器: 导出容器 97f3fefb651c 的快照到本地文件 ubuntu1.tar)
[root@VM-4-13-centos docker]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS          PORTS     NAMES
97f3fefb651c   ubuntu    "/bin/bash"          7 minutes ago   Up 7 minutes              ubuntu-test1
e534d2229a24   httpd     "httpd-foreground"   18 hours ago    Up 19 minutes   80/tcp    happy_ptolemy
[root@VM-4-13-centos docker]# docker export 97f3fefb651c > ubuntu1.tar
[root@VM-4-13-centos docker]# ll
total 73400
-rw-r--r-- 1 root root 75158528 Jul 26 16:57 ubuntu1.tar
[root@VM-4-13-centos docker]# pwd
/usr/local/docker
  • 导入容器快照

1) 将本地文件中的容器快照再导入为镜像,以下实例将快照文件 ubuntu1.tar 导入到镜像 test/ubuntu:v1:

[root@VM-4-13-centos /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
httpd         latest    dabbfbe0c57b   7 months ago    144MB
ubuntu        latest    ba6acccedd29   9 months ago    72.8MB
hello-world   latest    feb5d9fea6a5   10 months ago   13.3kB
ubuntu        15.10     9b9cb95443b5   6 years ago     137MB
[root@VM-4-13-centos docker]# ll
total 73400
-rw-r--r-- 1 root root 75158528 Jul 26 16:57 ubuntu1.tar
[root@VM-4-13-centos docker]# pwd
/usr/local/docker

[root@VM-4-13-centos docker]# cat ubuntu1.tar | docker import - test/ubuntu:v1

sha256:e982a4fe13919aa875b6c9d2b7fa09c3088abe83f609645022b6213650ce4fe5

[root@VM-4-13-centos docker]# docker images

REPOSITORY    TAG       IMAGE ID       CREATED              SIZE
test/ubuntu   v1        e982a4fe1391   3 minutes ago        72.8MB
httpd         latest    dabbfbe0c57b   7 months ago         144MB

2) 可以通过指定 URL 或者某个目录来导入,例如:

docker import http://example.com/exampleimage.tgz example/imagerepo

9.删除容器

[root@VM-4-13-centos docker]# docker ps 
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS          PORTS     NAMES
97f3fefb651c   ubuntu    "/bin/bash"          27 minutes ago   Up 27 minutes             ubuntu-test1
e534d2229a24   httpd     "httpd-foreground"   19 hours ago     Up 38 minutes   80/tcp    happy_ptolemy
[root@VM-4-13-centos docker]# docker rm -f e534d2229a24
e534d2229a24
[root@VM-4-13-centos docker]# docker ps 
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
97f3fefb651c   ubuntu    "/bin/bash"   28 minutes ago   Up 28 minutes             ubuntu-test1

 10.清理掉所有处于终止状态的容器

docker container prune

 

posted @ 2022-08-27 21:30  xiaoyanhahaha  阅读(111)  评论(0编辑  收藏  举报