【Docker II】Docker运行第一个容器-Hello World

万事俱备,只欠HelloWorld。

一、替换默认Docker register源

由于默认的docker镜像仓库是在国外,访问不便,因此为了便于实际使用,咱们需要先将镜像仓库源替换为国内仓库源,这里我使用的阿里云的docker镜像仓库源。

  1. 进入目录/etc/docker/,如果存在daemon.json文件时,直接将如下内容增加到文件中。如果不存在daemon.json文件,创建文件,并将如下内容增加到文件中。创建完成的文件需要为其赋予权限,这里我就简单的设置了chmod 777 /etc/docker/daemon.json
{
 "registry-mirrors": ["https://alzgoonw.mirror.aliyuncs.com"]
}
  1. 执行命令systemctl restart docker,重启docker服务,使其重新加载配置生效。
  2. 执行命令docker info,可以看到增加了一项Registry Mirrors,已经设置为了 alzgoonw.mirror.aliyuncs.com,此时就已经替换为了国内的镜像仓库源。
  3. 如果启动docker服务的过程中出现无法启动的问题,可以查看docker日志/var/log/messages来定位有何问题。

二、Hello World

执行命令docker run hello-world,从仓库中拉取我们的第一个docker镜像并创建容器启动,可以看到如下的输出:

[root@localhost log]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:8e3114318a995a1ee497790535e7b88365222a21771ae7e53687ad76563e8e76
Status: Downloaded newer image for hello-world:latest

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/

解释说明:

  1. 首先看看docker run命令,docker run命令提供了docker容器从创建到启动的功能,我们执行docker run hello-world后,Docker首先会从我们本地的镜像仓库中查找hello-world这个镜像,如果找到了就直接用此镜像创建容器。如果在本地仓库中没有找到对应的镜像,那么就会前往已配置的远端的镜像仓库中进行查找,找到后下载到本地镜像仓库中,创建容器并启动。
  2. 根据命令输出,可以看到命令的执行进行了本地查找Unable to find image 'hello-world:latest' locally,远端拉取latest: Pulling from library/hello-world,启动运行容器Hello from Docker!的操作。
  3. 可以看到,上述输出中有一句docker run -it ubuntu bash,执行这行命令后,会从镜像仓库中拉取一个ubuntu操作系统的镜像创建出相应的容器,并且创建出一个伪tty终端,咱们进入终端后,执行命令cat /etc/issue,可以看到输出了Ubuntu 18.04.4 LTS \n \l信息,由于我是在CentOS上安装的docker,因此这行输出就说明咱们操作的是容器中的服务。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值