Hello world in a container

本文介绍了如何使用Docker运行简单的Hello World应用,包括交互式容器、后台守护进程等基本操作,并探讨了Docker命令如docker run、docker ps、docker logs及docker stop的功能。

Hello world in a container

So what’s this Docker thing all about?

Docker allows you to run applications, worlds you create, inside containers.Running an application inside a container takes a single command: docker run.

Note: Depending on your Docker system configuration, you may be required topreface each docker command on this page with sudo. To avoid this behavior,your system administrator can create a Unix group called docker and add usersto it.

Run a Hello world

Let’s try it now.

$ docker run ubuntu:14.04 /bin/echo 'Hello world'
Hello world

And you just launched your first container!

So what just happened? Let’s step through what the docker run commanddid.

First we specified the docker binary and the command we wanted toexecute, run. The docker run combination runs containers.

Next we specified an image: ubuntu:14.04. This is the source of the containerwe ran. Docker calls this an image. In this case we used an Ubuntu 14.04operating system image.

When you specify an image, Docker looks first for the image on yourDocker host. If it can’t find it then it downloads the image from the publicimage registry: Docker Hub.

Next we told Docker what command to run inside our new container:

/bin/echo 'Hello world'

When our container was launched Docker created a new Ubuntu 14.04environment and then executed the /bin/echo command inside it. We sawthe result on the command line:

Hello world

So what happened to our container after that? Well Docker containersonly run as long as the command you specify is active. Here, as soon asHello world was echoed, the container stopped.

An interactive container

Let’s try the docker run command again, this time specifying a newcommand to run in our container.

$ docker run -t -i ubuntu:14.04 /bin/bash
root@af8bae53bdd3:/#

Here we’ve again specified the docker run command and launched anubuntu:14.04 image. But we’ve also passed in two flags: -t and -i.The -t flag assigns a pseudo-tty or terminal inside our new containerand the -i flag allows us to make an interactive connection bygrabbing the standard in (STDIN) of the container.

We’ve also specified a new command for our container to run:/bin/bash. This will launch a Bash shell inside our container.

So now when our container is launched we can see that we’ve got acommand prompt inside it:

root@af8bae53bdd3:/#

Let’s try running some commands inside our container:

root@af8bae53bdd3:/# pwd
/
root@af8bae53bdd3:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

You can see we’ve run the pwd to show our current directory and cansee we’re in the / root directory. We’ve also done a directory listingof the root directory which shows us what looks like a typical Linuxfile system.

You can play around inside this container and when you’re done you canuse the exit command or enter Ctrl-D to finish.

root@af8bae53bdd3:/# exit

As with our previous container, once the Bash shell process hasfinished, the container is stopped.

A daemonized Hello world

Now a container that runs a command and then exits has some uses butit’s not overly helpful. Let’s create a container that runs as a daemon,like most of the applications we’re probably going to run with Docker.

Again we can do this with the docker run command:

$ docker run -d ubuntu:14.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"
1e5535038e285177d5214659a068137486f96ee5c2e85a4ac52dc83f2ebe4147

Wait, what? Where’s our “hello world” output? Let’s look at what we’ve run here.It should look pretty familiar. We ran docker run but this time wespecified a flag: -d. The -d flag tells Docker to run the containerand put it in the background, to daemonize it.

We also specified the same image: ubuntu:14.04.

Finally, we specified a command to run:

/bin/sh -c "while true; do echo hello world; sleep 1; done"

This is the (hello) world’s silliest daemon: a shell script that echoeshello world forever.

So why aren’t we seeing any hello world’s? Instead Docker has returneda really long string:

1e5535038e285177d5214659a068137486f96ee5c2e85a4ac52dc83f2ebe4147

This really long string is called a container ID. It uniquelyidentifies a container so we can work with it.

Note:The container ID is a bit long and unwieldy. A bit later,we’ll see a shorter ID and ways to name our containers to makeworking with them easier.

We can use this container ID to see what’s happening with our hello world daemon.

Firstly let’s make sure our container is running. We cando that with the docker ps command. The docker ps command queriesthe Docker daemon for information about all the containers it knowsabout.

$ docker ps
CONTAINER ID  IMAGE         COMMAND               CREATED        STATUS       PORTS NAMES
1e5535038e28  ubuntu:14.04  /bin/sh -c 'while tr  2 minutes ago  Up 1 minute        insane_babbage

Here we can see our daemonized container. The docker ps has returned some usefulinformation about it, starting with a shorter variant of its container ID:1e5535038e28.

We can also see the image we used to build it, ubuntu:14.04, the command itis running, its status and an automatically assigned name,insane_babbage.

Note:Docker automatically generates names for any containers started.We’ll see how to specify your own names a bit later.

Okay, so we now know it’s running. But is it doing what we asked it to do? Tosee this we’re going to look inside the container using the docker logscommand. Let’s use the container name Docker assigned.

$ docker logs insane_babbage
hello world
hello world
hello world
. . .

The docker logs command looks inside the container and returns its standardoutput: in this case the output of our command hello world.

Awesome! Our daemon is working and we’ve just created our firstDockerized application!

Now we’ve established we can create our own containers let’s tidy upafter ourselves and stop our detached container. To do this we use thedocker stop command.

$ docker stop insane_babbage
insane_babbage

The docker stop command tells Docker to politely stop the runningcontainer. If it succeeds it will return the name of the container ithas just stopped.

Let’s check it worked with the docker ps command.

$ docker ps
CONTAINER ID  IMAGE         COMMAND               CREATED        STATUS       PORTS NAMES

Excellent. Our container has been stopped.

Next steps

So far, you launched your first containers using the docker run command. Youran an interactive container that ran in the foreground. You also ran adetached container that ran in the background. In the process you learnedabout several Docker commands:

  • docker ps - Lists containers.
  • docker logs - Shows us the standard output of a container.
  • docker stop - Stops running containers.

Now, you have the basis learn more about Docker and how to do some more advancedtasks. Go to Run a simple application to actually build aweb application with the Docker client.

这是一个基于AI视觉识别与3D引擎技术打造的沉浸式交互圣诞装置。 简单来说,它是一棵通过网页浏览器运行的数字智慧圣诞树,你可以用真实的肢体动作来操控它的形态,并将自己的回忆照片融入其中。 1. 核心技术组成 这个作品是由三个尖端技术模块组成的: Three.js 3D引擎:负责渲染整棵圣诞树、动态落雪、五彩挂灯和树顶星。它创建了一个具备光影和深度感的虚拟3D空间。 MediaPipe AI手势识别:调用电脑摄像头,实时识别手部的21个关键点。它能读懂你的手势,如握拳、张开或捏合。 GSAP动画系统:负责处理粒子散开与聚合时的平滑过渡,让成百上千个物体在运动时保持顺滑。 2. 它的主要作用与功能 交互式情感表达: 回忆挂载:你可以上传本地照片,这些照片会像装饰品一样挂在树上,或者像星云一样环绕在树周围。 魔法操控:握拳时粒子迅速聚拢,构成一棵挺拔的圣诞树;张开手掌时,树会瞬间炸裂成星光和雪花,照片随之起舞;捏合手指时视线会拉近,让你特写观察某一张选中的照片。 节日氛围装饰: 在白色背景下,这棵树呈现出一种现代艺术感。600片雪花在3D空间里缓缓飘落,提供视觉深度。树上的彩色粒子和白色星灯会周期性地呼吸闪烁,模拟真实灯串的效果。 3. 如何使用 启动:运行代码后,允许浏览器开启摄像头。 装扮:点击上传照片按钮,选择温馨合照。 互动:对着摄像头挥动手掌可以旋转圣诞树;五指张开让照片和树化作满天星辰;攥紧拳头让它们重新变回挺拔的树。 4. 适用场景 个人纪念:作为一个独特的数字相册,在节日陪伴自己。 浪漫惊喜:录制一段操作手势让照片绽放的视频发给朋友。 技术展示:作为WebGL与AI结合的案例,展示前端开发的潜力。
【顶级EI复现】计及连锁故障传播路径的电力系统 N-k 多阶段双层优化及故障场景筛选模型(Matlab代码实现)内容概要:本文提出了一种计及连锁故障传播路径的电力系统N-k多阶段双层优化及故障场景筛选模型,并提供了基于Matlab的代码实现。该模型旨在应对复杂电力系统中可能发生的N-k故障(即多个元件相继失效),通过构建双层优化框架,上层优化系统运行策略,下层模拟故障传播过程,从而实现对关键故障场景的有效识别与筛选。研究结合多阶段动态特性,充分考虑故障的时序演化与连锁反应机制,提升了电力系统安全性评估的准确性与实用性。此外,模型具备良好的通用性与可扩展性,适用于大规模电网的风险评估与预防控制。; 适合人群:电力系统、能源互联网及相关领域的高校研究生、科研人员以及从事电网安全分析、风险评估的工程技术人员。; 使用场景及目标:①用于电力系统连锁故障建模与风险评估;②支撑N-k故障场景的自动化筛选与关键脆弱环节识别;③为电网规划、调度运行及应急预案制定提供理论依据和技术工具;④服务于高水平学术论文复现与科研项目开发。; 阅读建议:建议读者结合Matlab代码深入理解模型构建细节,重点关注双层优化结构的设计逻辑、故障传播路径的建模方法以及场景削减技术的应用,建议在实际电网数据上进行测试与验证,以提升对模型性能与适用边界的认知。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值