深入了解Docker基础,对Docker有更详细的理解

这是一个Docker初学者学习Docker时的笔记。

$ docker run 在做什么?

结论:跑步 = 创造 + 开始

我会逐个查看。

跑步行为

$ docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

运行程序后,将执行Hello World的默认命令,输出文本的程序。

创建

$ docker create hello-world
4ef9a26766a189e365085710cb0c2fd71aecd60816f46d111287ccae8136c07b

$ docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                   PORTS     NAMES
4ef9a26766a1   hello-world   "/hello"   7 seconds ago   Created                            brave_panini

从STATUS中可以看出只需执行create即可创建容器。

开始

$ docker start 4ef9a26766a1
4ef9a26766a1

$ docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                      PORTS     NAMES
4ef9a26766a1   hello-world   "/hello"   6 minutes ago   Exited (0) 13 seconds ago             brave_panini

startだけを実行すると何も起きないがコンテナはexitになっているのが分かる。
startコマンドだけだと実際にデフォルトコマンドは実行されているが出力結果はターミナル上から見れない。
-aオプションをつけることで実行されるデフォルトコマンドの出力結果が見れる。

$ docker start -a 4ef9a26766a1

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

总结起来,create是用来创建容器的,start是用来执行默认命令的,而run则是执行这两个操作。

指令的替换

$ docker run -it ubuntu bash

上述命令的最后一部分bash是默认命令。
换句话说,这指的是在启动时执行的程序,而命令部分是可以更改的。

$ docker run -it ubuntu ls
bin   dev  home  lib32  libx32  mnt  proc  run   srv  tmp  var
boot  etc  lib   lib64  media   opt  root  sbin  sys  usr

この場合はlsを指定しているのでrunするとlsが実行されている。
ちなみにこのコマンド部分を指定しないとどうなるかというと、

$ docker run -it ubuntu
root@a4e426ab6c07:/#

由于已在容器启动时设置了执行命令,bash会按照指定的时间启动,与最初设定的时候相同。

你可以使用 $ docker ps -a 命令来查看默认设置的命令。

$ docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED              STATUS                      PORTS     NAMES
a4e426ab6c07   ubuntu        "bash"     About a minute ago   Exited (0) 6 seconds ago              nifty_meninsky
8c15dc625d7d   hello-world   "/hello"   34 minutes ago       Exited (0) 34 minutes ago             quirky_merkle

对于Ubuntu来说,“bash”是默认设置的命令,对于hello-world来说,“/hello”是默认设置的命令。

-itは何をしているのか?

$ docker run -it ubuntu bash

のときおまじないとして-itを入力していたが-itは何をしているのか?
-itは-iと-tオプションを繋げた形で、
-iはインプットを可能にする、-tは表示をきれいにするためのものです。

如果没有i呢? (If there is no i?)

$ docker run -t ubuntu bash
root@a4e426ab6c07:/# ls

可以将东西放入容器中,但无法将在容器内输入的内容接受。

没有T吗?

$ docker run -i ubuntu bash
ls
ls
bin
boot
dev
etc
home

由于-t选项能够让输出更整洁,所以没有-t选项的话,显示会变得不整洁。同时,也无法使用制表符进行补全。

删除容器

由于基本上不会使用退出状态的容器,因此需要定期删除它们。

    コンテナを単体で削除するコマンド
$ docker rm <container>
    Up状態のコンテナは削除できないのでUp状態のコンテナをexitにするコマンド
$ docker stop <container>
    exit状態のコンテナを全て削除するコマンド(便利)
$ docker system prune

WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all dangling images
  - all dangling build cache

Are you sure you want to continue? [y/N] 

“dangling images” 這個詞是指從 Dockerfile 創建的新鮮映像檔。
這些 dangling images 沒有名稱,以 “” 表示。

容器与文件系统的独立性

无论从相同的Docker镜像创建容器,所创建的容器都是相互独立的。
它们之间不会相互干扰。

$ docker run -it ubuntu bash
root@8143d007d614:/# touch test1
root@8143d007d614:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  test1  tmp  usr  var
$ docker run -it ubuntu bash
root@068c95f2344f:/# touch test2
root@068c95f2344f:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  test2  tmp  usr  var

给容器起一个名字并运行它 qǐ yí ge tā)

コンテナに名前をつけるのはどんな時か?

    • 起動させ続けるコンテナを立てる時

 

    • 共有サーバーを立てる時

 

    他のプログラムで使用する時
$ docker run --name <name> <image>

在容器中可以给它们起名字。

$ docker run --name sample_container ubuntu

$ docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED             STATUS                         PORTS     NAMES
9c359b2e2182   ubuntu        "bash"     7 seconds ago       Exited (0) 6 seconds ago                 sample_container

不能使用与已经存在的容器相同的名称。

分离模式和前景模式

离线模式

容器启动后在后台运行

$ docker run -d <image>

前景模式

コンテナexit後に削除 (一回きりのコンテナ)

$ docker run --rm <image>

请参考