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

Docker的基本操作

程序员文章站 2024-03-12 11:13:32
...

首先启动一个容器。 
使用的命令是:

 $ docker run [OPTIONS] IMAGE [COMMAND][ARG...]
  • 1

查看并使用已有的镜像:

[[email protected] ~]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
hub.c.163.com/library/ubuntu   latest              f49eec89601e        7 weeks ago         129.5 MB
  • 1
  • 2
  • 3

运行这个镜像执行输出hello docker:

[root@localhost ~]# docker run hub.c.163.com/library/ubuntu echo 'hello docker'
hello docker
  • 1
  • 2

输出后,启动的容器也就停止了。这是单词启动容器。

我们一般会启动交互式容器,那么我们一般用的命令是:

[root@localhost ~]# docker run -i -t IMAGE /bin/bash
  • 1

其中 
-i, –interactive=false 打开STDIN,用于控制台交互 
-t, –tty=false 分配tty设备,该可以支持终端登录,默认为false 
我们运行一下:

[root@localhost ~]# docker run -i -t hub.c.163.com/library/ubuntu /bin/bash
root@b4f32bbe4a34:/# 
root@b4f32bbe4a34:/# 
root@b4f32bbe4a34:/# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 11:49 ?        00:00:00 /bin/bash
root        11     1  0 11:49 ?        00:00:00 ps -ef
root@b4f32bbe4a34:/# exit
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

可以看到我们就启动了一个容器。 
我们也可以通过ctrl+p,ctrl+q来退出容器。这样容器不会停止。 
通过docker ps可以查看现在运行的容器。

[[email protected] ~]# docker ps
CONTAINER ID        IMAGE                          COMMAND                  CREATED             STATUS              PORTS                   NAMES
b4f32bbe4a34        hub.c.163.com/library/ubuntu   "/bin/bash"              2 minutes ago       Up 2 minutes                                loving_brattain
  • 1
  • 2
  • 3

其中CONTAINER ID是容器id,NAMES是容器名字。

使用docker ps -a 列出所有的容器,包括关闭的和运行的。 
使用docker ps -l列出最新启动的容器。 
演示如下:

[[email protected] ~]# docker ps -a
CONTAINER ID        IMAGE                          COMMAND                  CREATED             STATUS                     PORTS                   NAMES
b4f32bbe4a34        hub.c.163.com/library/ubuntu   "/bin/bash"              5 minutes ago       Up 5 minutes                                       loving_brattain
debf6ec8991e        hub.c.163.com/library/ubuntu   "echo 'hello docker'"    9 minutes ago       Exited (0) 9 minutes ago                           trusting_kilby
d75a2d8c7822        xingguo/df_test1               "nginx -g 'daemon off"   8 hours ago         Up 8 hours                 0.0.0.0:32770->80/tcp   df_nginx_web
481b893670bc        f1e3e0a96d20                   "/bin/sh -c 'apt-get "   8 hours ago         Exited (100) 8 hours ago                           angry_hoover
959c0fc5d903        xingguo/commit_test1           "nginx -g 'daemon off"   24 hours ago        Up 24 hours                0.0.0.0:32769->80/tcp   nginx_test
80864d42dd23        hub.c.163.com/library/ubuntu   "/bin/bash"              4 days ago          Exited (0) 8 hours ago                             web2
243c9309dcb0        hub.c.163.com/library/centos   "/bin/bash"              4 days ago          Exited (0) 4 days ago                              web1
0acfe489665c        hub.c.163.com/library/redis    "docker-entrypoint.sh"   4 days ago          Exited (0) 4 days ago                              web
609127d35d8a        hub.c.163.com/library/redis    "docker-entrypoint.sh"   4 days ago          Exited (0) 4 days ago                              dc1
f0cc2b641202        hub.c.163.com/library/redis    "docker-entrypoint.sh"   4 days ago          Exited (0) 4 days ago                              agitated_mcnulty
13c3e18e54f0        hub.c.163.com/library/redis    "docker-entrypoint.sh"   4 days ago          Exited (0) 4 days ago                              container01
11de631f7f2a        hub.c.163.com/library/redis    "docker-entrypoint.sh"   4 days ago          Exited (0) 4 days ago                              suspicious_yalow
517efd4f989e        hub.c.163.com/library/redis    "docker-entrypoint.sh"   5 days ago          Exited (0) 5 days ago                              high_panini
[[email protected] ~]# docker ps -l
CONTAINER ID        IMAGE                          COMMAND             CREATED             STATUS              PORTS               NAMES
b4f32bbe4a34        hub.c.163.com/library/ubuntu   "/bin/bash"         5 minutes ago       Up 5 minutes                            loving_brattain
[[email protected] ~]# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

使用docker inspect 容器名/容器id来查看容器的详细信息。由于docker给默认起的名字难以记忆,所以我们在启动一个容器时会自定义一个名字。 
使用命令为:

[root@localhost ~]# docker run --name=自定义名 -i -t hub.c.163.com/library/ubuntu /bin/bash
  • 1

演示如下:

root@localhost ~]# docker run --name=container_name_test -i -t hub.c.163.com/library/ubuntu /bin/bash
root@9ee1e70972f1:/#  
root@9ee1e70972f1:/# [root@localhost ~]# 
[root@localhost ~]# docker ps -l
CONTAINER ID        IMAGE                          COMMAND             CREATED             STATUS              PORTS               NAMES
9ee1e70972f1        hub.c.163.com/library/ubuntu   "/bin/bash"         38 seconds ago      Up 33 seconds                           container_name_test
[root@localhost ~]# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

可以看到容器名为自定义的名字。 
当容器停止后,可以使用以下命令启动

docker start [-i] NAMES
  • 1

当退出容器后,可以使用以下命令进入容器:

[root@localhost ~]# docker attach 容器名/容器id
  • 1
[root@localhost ~]# docker attach container_name_test
root@9ee1e70972f1:/# 
root@9ee1e70972f1:/#
  • 1
  • 2
  • 3

使用docker rm 容器名/容器id可以删除一个容器。这里的删除只能删除已经停止运行的容器,不能删除正在运行的容器。

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                          COMMAND                  CREATED             STATUS                      PORTS                   NAMES
9ee1e70972f1        hub.c.163.com/library/ubuntu   "/bin/bash"              6 minutes ago       Exited (0) 13 seconds ago                           container_name_test
b4f32bbe4a34        hub.c.163.com/library/ubuntu   "/bin/bash"              17 minutes ago      Up 17 minutes                                       loving_brattain
[root@localhost ~]# docker rm loving_brattain
Error response from daemon: You cannot remove a running container b4f32bbe4a34692ad881481029659d2de91d92c4e0ba1117584fc6162a949d64. Stop the container before attempting removal or use -f
[root@localhost ~]# docker rm container_name_test
container_name_test
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                          COMMAND                  CREATED             STATUS                      PORTS                   NAMES
b4f32bbe4a34        hub.c.163.com/library/ubuntu   "/bin/bash"              19 minutes ago      Up 19 minutes                                       loving_brattain

[root@localhost ~]#