Hello world in a container

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

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.

weixin073智慧旅游平台开发微信小程序+ssm后端毕业源码案例设计 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
python017基于Python贫困生资助管理系统带vue前后端分离毕业源码案例设计 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
weixin102旅游社交微信小程序+ssm后端毕业源码案例设计 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值