Docker学习3 - Hello World

本文介绍了使用Docker的基本步骤,从安装环境开始,通过运行预设的Hello World实例,逐步深入到创建和管理自定义Image的过程。包括下载和加载Image,展示Docker如何简化容器化应用的部署。同时,提供了如何利用Dockerfile构建自定义Image的方法,以及如何查看和管理本地已存储的Image。最后,通过实际操作和例子,演示了如何在本地构建一个包含特定功能的Image。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hello-World

所有环境准备就绪,试着运行一下hello world吧(做为程序员, 你懂的)

参考

http://docs.docker.com/mac/step_two/

[root@centos7-docker ~]# docker run hello-world

Unable to find image 'hello-world:latest' locally
Trying to pull repository docker.io/hello-world ...
91c95931e552: Download complete
a8219747be10: Download complete
Status: Downloaded newer image for docker.io/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.
    (Assuming it was not already locally available.)
 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


For more examples and ideas, visit:
 http://docs.docker.com/userguide/

Usage of loopback devices is strongly discouraged for production use. Either use `--storage-opt dm.thinpooldev` or use `--storage-opt dm.no_warn_on_loop_devices=true` to suppress this warning.


从提示信息我们可以看出

1. 检查hello-world image

2. 发现没有,从Docker Hub 中下载

3. 加载并运行run



Whalesay

按照quickstart中的示例,我们进入

hub.docker.com

输入whalesay(鲸鱼说,和docker logo匹配 ^_^),能看到


在information中我们能看到这个是用来展示docker的一个sample,并且展示了用法


执行命令


由于国内万恶的墙,所以比较慢,多试几次,或者自己去买个VPN

这次执行完了之后,相当于whalesay已经存储在本机了,再次执行就快多了


我们可以查看和管理我们的image

[root@centos7-docker ~]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
docker.io/docker/whalesay   latest              fb434121fc77        8 weeks ago         247 MB
docker.io/hello-world       latest              91c95931e552        3 months ago        910 B

可以看到我们有2个image


Build you own image

http://docs.docker.com/mac/step_four/

如果我们想对之前的whalesay进行一些改动怎么呢?

[root@centos7-docker ~]# mkdir -p mydockerbuild

[root@centos7-docker ~]# cd mydockerbuild/

[root@centos7-docker mydockerbuild]# vim Dockerfile

FROM docker/whalesay:latest
RUN apt-get -y update && apt-get -y install fortunes
CMD /usr/games/fortune -a | cowsay

这段逻辑文档中有解释, 没太仔细去看,也不需要仔细去看

其实就是一些执行脚本,像shell脚本一样

这里是安装fortunes, 然后fortune -a命令的输出结果做为cowsay命令的输入

执行命令如下

[root@centos7-docker mydockerbuild]# docker build -t docker-whale .
Sending build context to Docker daemon 158.8 MB
...snip...
Removing intermediate container a8e6faa88df3
Successfully built 7d9495d03763

-t 代表启动instance名字是docker-whale, 在启动这个instance的时候会自动调用Dockerfile脚本

这个时候我们再去查看我们有几个images了

[root@centos7-docker mydockerbuild]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
docker-whale                latest              beda7d481894        44 minutes ago      255.5 MB
docker.io/docker/whalesay   latest              fb434121fc77        8 weeks ago         247 MB
docker.io/hello-world       latest              91c95931e552        3 months ago        910 B

我们看见新增了docker-whale了

运行



Help

每个命令我们不可能记得这么多,所以,寻找帮助很重要

[root@centos7-docker mydockerbuild]#

[root@centos7-docker ~]# 

[root@centos7-docker ~]#  docker run --help

[root@centos7-docker ~]# docker help run

也可以找男人^_^

[root@centos7-docker ~]#  man docker

[root@centos7-docker ~]#  man docker run


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值