基于Docker的基础知识,尝试使用Docker

在学习有关Docker时的笔记。

Docker简介

「Docker是什么?」

在计算机上创建一个被称为容器的盒子,并在其中安装操作系统和其他软件,以使其像另一台计算机一样运作。

使用Docker有什么好处?

由於在容器內進行環境設置,可以輕鬆方便地在他人的電腦上建立相同的環境,包括開發環境、測試環境和生產環境。換言之,使用Docker可以避免麻煩且負擔沉重的環境設置錯誤困擾!

Docker的基本工作流程

スクリーンショット 2021-10-15 11.06.31.png

基本流程是根据Dockerfile创建Docker镜像,然后根据Docker镜像创建各个容器。

尝试使用Docker

我打算从Docker Hub拉取docker镜像(下载到自己的电脑),对下载的docker镜像进行更改,并重新保存为Docker Hub的存储库。

从Docker Hub拉取Docker镜像

请使用以下URL注册并登录Docker Hub。
https://hub.docker.com/

$ docker login

一旦成功登录,就开始将Docker镜像拉取到主机(我的个人电脑)上。

$ docker pull hello-world

Using default tag: latest
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:37a0b92b08d4919615c3ee023f7ddb068d12b8387475d64c622ac30f45c29c51
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest

如果成功获取到完整的信息,就代表成功获取。

$ docker images

REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    feb5d9fea6a5   3 weeks ago   13.3kB

可以使用 “docker images” 命令查看主机上的镜像列表。

使用Docker镜像创建容器

使用拉取下来的docker镜像启动容器。

$ docker run hello-world

ello 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/

可以通过运行命令`run`来从Docker镜像创建容器。
顺便提一下,输出的是一个名为hello-world的Docker镜像的默认命令。

$ docker ps
$ docker ps -a

CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                     PORTS     NAMES
6d4f7a8029be   hello-world   "/hello"   4 minutes ago   Exited (0) 4 minutes ago             dazzling_gagarin

可以使用docker ps命令查看容器。
ps可以查看活动容器,而ps -a可以查看所有容器。
顺便说一下,ps是进程状态的缩写。
在本例中,由于hello-world处于退出状态,所以可以使用docker ps -a来确认。

スクリーンショット 2021-10-16 8.03.01.png

运行Ubuntu的Docker镜像

尝试运行在执行hello-world时输出的命令。

$ docker run -it ubuntu bash

-ubuntu是Linux派生的操作系统,其中的bash部分是用于指定在容器启动时要执行的程序选项。

尝试实际执行一下。

$ docker run -it ubuntu bash

Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
f3ef4ff62e0d: Pull complete
Digest: sha256:a0d9e826ab87bd665cfc640598a871b748b4b70a01a4f3d174d4fb02adad07a9
Status: Downloaded newer image for ubuntu:latest
root@8650a53129ac:/#

目前的情况是从Docker Hub拉取了Ubuntu镜像,并在Ubuntu环境下启动了容器内的bash程序,此时处于容器内部的状态。

在这里创建一个名为”test”的文件,然后更新容器并确认更新成功后退出。

root@8650a53129ac:/# touch test
root@8650a53129ac:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  test  tmp  usr  var

root@8650a53129ac:/# exit
exit

退出和分离

当退出并离开容器时,容器的状态将变为退出。

$ docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED       STATUS                     PORTS     NAMES
8650a53129ac   ubuntu        "bash"     2 hours ago   Exited (0) 3 minutes ago             kind_shannon

通过使用`detach`命令,您可以在退出时不使其处于`exit`状态。
从根本上讲,`exit`是指将之前运行的进程关闭并退出容器。
相反,`detach`则可以在保留进程的情况下退出。

让我们实际看一看。
我们将再次进入之前退出的容器。
为此,首先我们需要重新启动容器。容器的指定是通过使用ID来指定的。

$ docker restart 8650a53129ac

确认处于上方的位置。

$ docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED       STATUS                   PORTS     NAMES
8650a53129ac   ubuntu        "bash"     2 hours ago   Up 13 seconds                      kind_shannon

为了重新进入容器,您可以使用exec命令。

$ docker exec -it <container> bash

我们会尝试指定容器名称并实际执行。

$ docker exec -it 8650a53129ac bash
root@8650a53129ac:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  test  tmp  usr  var

我可以进入容器,并且之前创建的test文件也在那里。
上一次我是用exit退出的,但这次我会用detach退出。
用detach退出的方法是通过ctrl+p+q完成的。
我会实际操作退出,并确认容器的状态。

root@8650a53129ac:/# read escape sequence
$ docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED       STATUS                   PORTS     NAMES
8650a53129ac   ubuntu        "bash"     2 hours ago   Up 9 minutes                       kind_shannon

我能够以Up状态退出。
貌似通常使用exit来退出。
我认为在目前阶段记住这种方法就足够了。

将以下内容在中文中进行解释,只需要一种选项:

Docker提交

我们将添加一个名为“test”的文件的docker image,并将其提交到原始的docker image以进行更新。

提交的命令

$ docker commit <container> <new image>

我会试着实际操作一下。
你可以用冒号给图像添加标签。

$ docker commit 8650a53129ac ubuntu:updated

sha256:6f8a0c2aee370356b0ef80aee365575901c90684a8ca303619eeb1b8069481ff
$ docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
ubuntu        updated   6f8a0c2aee37   2 minutes ago   72.8MB
ubuntu        latest    597ce1600cf4   2 weeks ago     72.8MB
スクリーンショット 2021-10-16 9.02.35.png

Docker镜像采用层次结构,当向现有镜像中添加新文件时,它将作为新的层添加进去。在这种情况下,添加了一个包含测试文件的层,并通过提交操作将其反映在新的Docker镜像中。

在Docker Hub上创建代码库。

スクリーンショット 2021-10-16 11.53.41.png

将内容推送到Docker Hub。

在将镜像推送到Docker Hub之前,需要将当前镜像名称与Docker Hub仓库名称相匹配。
通过使用tag命令可以进行修改。

$ docker tag <今の名前> <更新する名前> 

这次的情况下,我们要通过写出docker images来确认名称是否已经变了。

$ docker tag ubuntu:updated <username>/my-first-repo
$ docker images
REPOSITORY               TAG       IMAGE ID       CREATED          SIZE
aaaasahi/my-first-repo   latest    6f8a0c2aee37   39 minutes ago   72.8MB

可以确认新设置的图像已经完成了。

最后会将其推送到Docker Hub上。

$ docker push aaaasahi/my-first-repo
スクリーンショット 2021-10-16 9.02.35.png

可以使用rmi命令删除不再需要的Docker镜像。

$ docker rmi <image>

请参考

bannerAds