docker 以一个例子 hello-world

我们现在学习一下 docker 怎么来实现一个 hello-world 的例子吧。例子很简单,首先需要拉取 docker hub 官方镜像仓库中的 hello-world 仓库到自己的计算机,让后使用本地的 hello-world 镜像创建并运行容器。

1 拉取 hello-world 镜像

拉取镜像很简单 docker pull “image_name”

docker pull hello-world 即可把 docker 官方仓库中的 hello-world 仓库拉取到本地计算机。hello-world 仓库地址:https://hub.docker.com/_/hello-world

2 运行命令 docker run

接下来是运行hello-world 容器

zsf90@ub:~$ 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/

查看容器

其实我们执行完 docker run hello-world 命令后就已经创建了一个容器,只不过执行完这条命令后就立即退出了,我们可以使用命令 docker ps -a 命令查看,不添加 -a 参数是看不到的,如果我们直接使用 docker rmi 命令是无法直接删除 hello-world 镜像的,因为有使用该镜像创建的容器,把容器删除后就可以删除镜像了。

zsf90@ub:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
53fac0b288d4        hello-world         "/hello"            4 minutes ago       Exited (0) 4 minutes ago                       cool_pasteur

此条目发表在Code分类目录,贴了, , 标签。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用*标注