enough

----------------------------------------------------------------------------------

Working with Containers

-

In the last section of the Docker User Guide we launched our first containers. We launched two containers using the docker run command.

  • Containers we ran interactively in the foreground.
  • One container we ran daemonized in the background.

In the process we learned about several Docker commands:

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

Tip: Another way to learn about docker commands is our interactive tutorial.

The docker client is pretty simple. Each action you can take with Docker is a command and each command can take a series of flags and arguments.

# Usage:  [sudo] docker [flags] [command] [arguments] ..
# Example:
$ sudo docker run -i -t ubuntu /bin/bash

Let's see this in action by using the docker version command to return version information on the currently installed Docker client and daemon.

$ sudo docker version

This command will not only provide you the version of Docker client and daemon you are using, but also the version of Go (the programming language powering Docker).

Client version: 0.8.0
Go version (client): go1.2 Git commit (client): cc3a8c8
Server version: 0.8.0 Git commit (server): cc3a8c8
Go version (server): go1.2 Last stable version: 0.8.0

Seeing what the Docker client can do

We can see all of the commands available to us with the Docker client by running the docker binary without any options.

$ sudo docker

You will see a list of all currently available commands.

Commands:
attach Attach to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
. . .

Seeing Docker command usage

You can also zoom in and review the usage for specific Docker commands.

Try typing Docker followed with a [command] to see the usage for that command:

$ sudo docker attach
Help output . . .

Or you can also pass the --help flag to the docker binary.

$ sudo docker attach --help

This will display the help text and all available flags:

Usage: docker attach [OPTIONS] CONTAINER

Attach to a running container

  --no-stdin=false: Do not attach stdin
--sig-proxy=true: Proxify all received signal to the process (even in non-tty mode)

Note: You can see a full list of Docker's commands here.

Running a Web Application in Docker

So now we've learnt a bit more about the docker client let's move onto the important stuff: running more containers. So far none of the containers we've run did anything particularly useful though. So let's build on that experience by running an example web application in Docker.

For our web application we're going to run a Python Flask application. Let's start with a docker runcommand.

$ sudo docker run -d -P training/webapp python app.py

Let's review what our command did. We've specified two flags: -d and -P. We've already seen the -dflag which tells Docker to run the container in the background. The -P flag is new and tells Docker to map any required network ports inside our container to our host. This lets us view our web application.

We've specified an image: training/webapp. This image is a pre-built image we've created that contains a simple Python Flask web application.

Lastly, we've specified a command for our container to run: python app.py. This launches our web application.

Note: You can see more detail on the docker run command in the command reference and theDocker Run Reference.

Viewing our Web Application Container

Now let's see our running container using the docker ps command.

$ sudo docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc533791f3f5 training/webapp:latest python app.py 5 seconds ago Up 2 seconds 0.0.0.0:49155->5000/tcp nostalgic_morse

You can see we've specified a new flag, -l, for the docker ps command. This tells the docker pscommand to return the details of the last container started.

Note: By default, the docker ps command only shows information about running containers. If you want to see stopped containers too use the -a flag.

We can see the same details we saw when we first Dockerized a container with one important addition in the PORTS column.

PORTS
0.0.0.0:49155->5000/tcp

When we passed the -P flag to the docker run command Docker mapped any ports exposed in our image to our host.

Note: We'll learn more about how to expose ports in Docker images when we learn how to build images.

In this case Docker has exposed port 5000 (the default Python Flask port) on port 49155.

Network port bindings are very configurable in Docker. In our last example the -P flag is a shortcut for -p 5000 that maps port 5000 inside the container to a high port (from the range 49153 to 65535) on the local Docker host. We can also bind Docker containers to specific ports using the -p flag, for example:

$ sudo docker run -d -p 5000:5000 training/webapp python app.py

This would map port 5000 inside our container to port 5000 on our local host. You might be asking about now: why wouldn't we just want to always use 1:1 port mappings in Docker containers rather than mapping to high ports? Well 1:1 mappings have the constraint of only being able to map one of each port on your local host. Let's say you want to test two Python applications: both bound to port 5000 inside your container. Without Docker's port mapping you could only access one at a time.

So let's now browse to port 49155 in a web browser to see the application.

.

Our Python application is live!

Note: If you have used the boot2docker virtual machine on OS X, Windows or Linux, you'll need to get the IP of the virtual host instead of using localhost. You can do this by running the following in the boot2docker shell.

$ boot2docker ip
The VM's Host only interface IP address is: 192.168.59.103

In this case you'd browse to http://192.168.59.103:49155 for the above example.

A Network Port Shortcut

Using the docker ps command to return the mapped port is a bit clumsy so Docker has a useful shortcut we can use: docker port. To use docker port we specify the ID or name of our container and then the port for which we need the corresponding public-facing port.

$ sudo docker port nostalgic_morse 5000
0.0.0.0:49155

In this case we've looked up what port is mapped externally to port 5000 inside the container.

Viewing the Web Application's Logs

Let's also find out a bit more about what's happening with our application and use another of the commands we've learnt, docker logs.

$ sudo docker logs -f nostalgic_morse
* Running on http://0.0.0.0:5000/
10.0.2.2 - - [23/May/2014 20:16:31] "GET / HTTP/1.1" 200 -
10.0.2.2 - - [23/May/2014 20:16:31] "GET /favicon.ico HTTP/1.1" 404 -

This time though we've added a new flag, -f. This causes the docker logs command to act like the tail -f command and watch the container's standard out. We can see here the logs from Flask showing the application running on port 5000 and the access log entries for it.

Looking at our Web Application Container's processes

In addition to the container's logs we can also examine the processes running inside it using the docker top command.

$ sudo docker top nostalgic_morse
PID USER COMMAND
854 root python app.py

Here we can see our python app.py command is the only process running inside the container.

Inspecting our Web Application Container

Lastly, we can take a low-level dive into our Docker container using the docker inspect command. It returns a JSON hash of useful configuration and status information about Docker containers.

$ sudo docker inspect nostalgic_morse

Let's see a sample of that JSON output.

[{
"ID": "bc533791f3f500b280a9626688bc79e342e3ea0d528efe3a86a51ecb28ea20",
"Created": "2014-05-26T05:52:40.808952951Z",
"Path": "python",
"Args": [
"app.py"
],
"Config": {
"Hostname": "bc533791f3f5",
"Domainname": "",
"User": "",
. . .

We can also narrow down the information we want to return by requesting a specific element, for example to return the container's IP address we would:

$ sudo docker inspect -f '{{ .NetworkSettings.IPAddress }}' nostalgic_morse
172.17.0.5

Stopping our Web Application Container

Okay we've seen web application working. Now let's stop it using the docker stop command and the name of our container: nostalgic_morse.

$ sudo docker stop nostalgic_morse
nostalgic_morse

We can now use the docker ps command to check if the container has been stopped.

$ sudo docker ps -l

Restarting our Web Application Container

Oops! Just after you stopped the container you get a call to say another developer needs the container back. From here you have two choices: you can create a new container or restart the old one. Let's look at starting our previous container back up.

$ sudo docker start nostalgic_morse
nostalgic_morse

Now quickly run docker ps -l again to see the running container is back up or browse to the container's URL to see if the application responds.

Note: Also available is the docker restart command that runs a stop and then start on the container.

Removing our Web Application Container

Your colleague has let you know that they've now finished with the container and won't need it again. So let's remove it using the docker rm command.

$ sudo docker rm nostalgic_morse
Error: Impossible to remove a running container, please stop it first or use -f
2014/05/24 08:12:56 Error: failed to remove one or more containers

What's happened? We can't actually remove a running container. This protects you from accidentally removing a running container you might need. Let's try this again by stopping the container first.

$ sudo docker stop nostalgic_morse
nostalgic_morse
$ sudo docker rm nostalgic_morse
nostalgic_morse

And now our container is stopped and deleted.

Note: Always remember that deleting a container is final!

docker offical docs:Working with Containers的更多相关文章

  1. docker offical docs:Working with Docker Images

    Working with Docker Images ##orignal is always the best In the introduction we've discovered that Do ...

  2. 关于Docker中的Images与Containers

    Docker engine提供了启动Images和containers核心的技术的支持.当你运行docker run hello-world 命令时,实际上可分为三个部分: 告诉你操作系统你正在使用的 ...

  3. [E] Shiro 官方文档阅读笔记 The Reading Notes of Shiro's Offical Docs

    官方文档: https://shiro.apache.org/reference.html https://shiro.apache.org/java-authentication-guide.htm ...

  4. Docker Network containers

    Network containers Estimated reading time: 5 minutes If you are working your way through the user gu ...

  5. Docker入门(三):容器(Containers)

    这个<Docker入门系列>文档,是根据Docker官网(https://docs.docker.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指 ...

  6. 玩转docker

    开篇先论赌 (组词,赌博,....),时刻,每天都在赌! 何为赌?仁者见仁,智者必定又有一番见解,保持沉默,意见保留; ——改变思维模式,Ruiy让赌赢在“思维”!!!; 存在在IT界Ruiy定格,即 ...

  7. Docker之Compose服务编排

    Compose是Docker的服务编排工具,主要用来构建基于Docker的复杂应用,Compose 通过一个配置文件来管理多个Docker容器,非常适合组合使用多个容器进行开发的场景. 说明:Comp ...

  8. Play with docker 1.12

    Docker v1.12 brings in its integrated orchestration into docker engine. Starting with Docker 1.12, w ...

  9. 【云计算】Docker集中化web界面管理平台shipyard

    Docker集中化web界面管理平台shipyard docker shipyard seanlook                        2015年01月05日发布             ...

随机推荐

  1. OAuth2.0协议

    简介  OAuth(Open Authorization),协议为用户资源的授权提供了一个安全的.开放而又简易的标准.与以往的授权方式不同之处是OAuth的授权不会使第三方触及到用户的帐号信息(如用户 ...

  2. wordpress 自定义面板显示不了挂件区问题

    刚才在写一个wordpress主题,遇到一个问题.注册好的挂件区在控制面板(dashboard)上显示,在自定义面板上却不显示. 查询了下,发现几个老外朋友也遇到了这个问题: http://wordp ...

  3. using System.Diagnostics; 日志操作

    using System.Diagnostics 命名空间 包含了能够与系统进程 事件日志 和性能计数器进行交互的类 一般用于帮助诊断和调试应用程序 例如 Debug类用于帮组调试代码 Process ...

  4. 10.PHP内核探索:Apache运行与钩子函数

    Apache是目前世界上使用最为广泛的一种Web Server,它以跨平台.高效和稳定而闻名.按照去年官方统计的数据,Apache服务器的装机量占该市场60%以上的份额.尤其是在 X(Unix/Lin ...

  5. delphi下如何获得不带扩展名的文件名?

    Edit1.Text:=ChangeFileExt(ExtractFileName(Application.ExeName),'') ; //获取到应用程序名后,将后缀名清空就可以啦.

  6. php浮点数计算问题

    如果用php的+-*/计算浮点数的时候,可能会遇到一些计算结果错误的问题,比如echo intval( 0.58*100 );会打印57,而不是58,这个其实是计算机底层二进制无法精确表示浮点数的一个 ...

  7. XML xsd

    targetNamespace:表示本XSD中定义的元素和类型的名字空间都是http://exammpleOrder. xmlns:xsd:表示以前缀xsd开头的元素或则类型来自于http://www ...

  8. myeclipse添加源码支持

    在MyEclipse中开发,习惯于点击类名,按Ctrl键查看源码,但是,如果是Spring/Hibernate/Struts/JDK这些开源jar的源码该如何看呢? 一般,我们导入的只有jar文 件, ...

  9. Python之list添加新元素、删除元素、替换元素

    Python之list添加新元素 现在,班里有3名同学: >>> L = ['Adam', 'Lisa', 'Bart'] 今天,班里转来一名新同学 Paul,如何把新同学添加到现有 ...

  10. 企业服务总线(ESB)

    思考: 1.ESB的定义到底是什么?是一款产品还是一种架构模式? 2.ESB有何实际用处? 定义ESB 对于企业服务总线(Enterprise Service Bus),目前还没有公认的定义,根据供应 ...