• 主页
  • 关于
  • 入门
  • 社区
  • 文档
  • 日志
  • 索引 link to external site
Docker logo

文档

  • 介绍
  • 安装
    • Ubuntu
      • Ubuntu Precise 12.04 (LTS) (64-bit)
      • Ubuntu Raring 13.04 and Saucy 13.10 (64 bit)
      • Troubleshooting
      • Docker和防火墙
      • Mirrors
    • Red Hat Enterprise Linux
      • Installation
      • Issues?
    • Fedora
      • Installation
    • Arch Linux
      • Dependencies
      • Installation
      • Starting Docker
    • Gentoo
      • Installation
      • Starting Docker
    • openSUSE
      • Installation
    • FrugalWare
      • Dependencies
      • Installation
      • Starting Docker
    • Mac OS X
      • How To Install Docker On Mac OS X
      • How To Use Docker On Mac OS X
      • Learn More
    • 在Windows上安装Docker
      • 安装
      • 打开命令终端
      • 启动Ubuntu虚拟服务器
      • 登录Unbuntu服务器
      • 运行Docker
      • 故障排除
    • Amazon EC2
      • Amazon QuickStart
      • Standard Ubuntu Installation
      • Use Vagrant
    • Rackspace Cloud
    • Google Cloud Platform
      • Compute Engine QuickStart for Debian
    • Binaries
      • Check runtime dependencies
      • Check kernel dependencies
      • Get the docker binary:
      • Run the docker daemon
      • Giving non-root access
      • Upgrades
      • Run your first container!
  • 使用
    • 使用Docker第一步
      • 检测Docker安装
      • 下载预建的镜像
      • 运行一个交互的shell
      • 绑定Docker到另一台主机/端口或Unix套接字
      • 启动长期运行工作进程
      • 列出所有正在运行的容器
      • Bind a service on a TCP port
      • 提交(保存)容器状态
    • 通过存储库分享镜像
      • 公共库
      • 在中央索引库查找公共镜像
      • 贡献给中央档案
      • 提交一个容器到已命名的镜像
      • 推一个镜像到它的资料库
      • 可信构建
      • 私有库
      • 认证文件
    • 端口重定向
      • 端口绑定到主机接口
      • 连接容器
    • 设置网络
      • 如何使用特定的IP地址范围
      • 容器互通
      • 什么是vethXXXX设备?
      • 更多
    • 自动启动容器
      • 介绍
      • 示例初创脚本
      • 示例系统脚本
    • 通过卷共享目录
      • 入门
      • 已知问题
    • 链接容器
      • 容器命名
      • 链接:docker服务探索
    • 通过大使容器链接
      • 两个主机例子
      • 如何工作
      • The svendowideit/ambassador Dockerfile
    • 使用Puppet
      • 需求
      • 安装
      • 使用
  • 实例
    • Hello World
    • 检测Docker安装情况
      • Hello World
      • Hello World Daemon
    • Python Web应用
    • Node.js Web应用
      • 创建Node.js应用
      • 创建一个 Dockerfile
      • 创建镜像
      • 运行镜像
      • 测试
    • Redis服务
      • Create a docker container for Redis
      • Run the service
      • Create your web application container
    • SSH守护进程服务
      • 更新:
    • CouchDB 服务
      • 创建第一个数据库
      • 添加数据到第一个数据库
      • 创建第二个数据库
      • 在第二个数据库中浏览数据
    • PostgreSQL服务器
      • Installing PostgreSQL on Docker
      • PostgreSQL service auto-launch
    • 创建MongoDB镜像
      • 创建 Dockerfile
    • Riak 服务
      • 创建 Dockerfile
      • Create a supervisord configuration file
      • Build the Docker image for Riak
      • Next steps
    • 在Docker中使用Supervisor
      • 创建一个Dockerfile
      • 安装Supervisor
      • 添加Supervisor的配置文件
      • 暴露端口ports并运行Supervisor
      • 创建我们的容器
      • 运行我们Supervisor容器
    • 使用CFEngine管理进程
      • 如何工作
      • 使用
      • 适应您的应用程序
  • 参考手册
    • Commands
      • Command Line Help
      • Types of Options
      • Commands
      • daemon
      • attach
      • build
      • commit
      • cp
      • diff
      • events
      • export
      • history
      • images
      • import
      • info
      • insert
      • inspect
      • kill
      • load
      • login
      • logs
      • port
      • ps
      • pull
      • push
      • restart
      • rm
      • rmi
      • run
      • save
      • search
      • start
      • stop
      • tag
      • top
      • version
      • wait
    • Dockerfile Reference
      • 1. Usage
      • 2. Format
      • 3. Instructions
      • 4. Dockerfile Examples
    • Docker Run Reference
      • General Form
      • Operator Exclusive Options
      • Overriding Dockerfile Image Defaults
    • APIs
      • Registry & Index Spec
      • Docker Registry API
      • Docker Index API
      • Docker Remote API
      • Docker Remote API Client Libraries
  • 贡献
    • Contributing to Docker
    • Setting Up a Dev Environment
      • Step 1: Install Docker
      • Step 2: Check out the Source
      • Step 3: Build the Environment
      • Step 4: Build the Docker Binary
      • Step 5: Run the Tests
      • Step 6: Use Docker
      • Extra Step: Build and view the Documentation
  • 词汇表
    • File System
    • Layers
      • Layer
      • Union File System
    • Image
      • Parent Image
      • Base Image
      • Image IDs
    • Container
      • Container State
      • Container IDs
  • 文章
    • Docker 安全
      • Kernel Namespaces
      • Control Groups
      • Docker Daemon Attack Surface
      • Linux Kernel Capabilities
      • Other Kernel Security Features
      • Conclusions
    • 创建一个基础镜像
      • Getting Started
    • 运行指标
      • Control Groups
      • Enumerating Cgroups
      • Finding the Cgroup for a Given Container
      • Metrics from Cgroups: Memory, CPU, Block IO
      • Network Metrics
      • Tips for high-performance metric collection
      • Collecting metrics when a container exits
  • 常见问题
    • Most frequently asked questions.
      • 多少钱?
      • 使用的是哪个开源证书?
      • 能运行在Mac OS X 或者 Windows上吗?
      • 容器与虚拟机的比较?
      • What does Docker add to just plain LXC?
      • Docker容器和虚拟机的区别?
      • 当容器退出时我会失去了我的数据吗?
      • How far do Docker containers scale?
      • 如何连接Docker容器?
      • 如何在Docker容器中跑多个进程?
      • Docker运行在哪些平台?
      • 我能帮助添加一些问答吗?
      • 哪里可以找到更多答案?

链接容器¶

From version 0.6.5 you are now able to name a container and link it to another container by referring to its name. This will create a parent -> child relationship where the parent container can see selected information about its child.

容器命名¶

v0.6.5 新版功能.

You can now name your container by using the -name flag. If no name is provided, Docker will automatically generate a name. You can see this name using the docker ps command.

# format is "sudo docker run -name <container_name> <image_name> <command>"
$ sudo docker run -name test ubuntu /bin/bash

# the flag "-a" Show all containers. Only running containers are shown by default.
$ sudo docker ps -a
CONTAINER ID        IMAGE                            COMMAND             CREATED             STATUS              PORTS               NAMES
2522602a0d99        ubuntu:12.04                     /bin/bash           14 seconds ago      Exit 0                                  test

链接:docker服务探索¶

v0.6.5 新版功能.

Links allow containers to discover and securely communicate with each other by using the flag -link name:alias. Inter-container communication can be disabled with the daemon flag -icc=false. With this flag set to false, Container A cannot access Container B unless explicitly allowed via a link. This is a huge win for securing your containers. When two containers are linked together Docker creates a parent child relationship between the containers. The parent container will be able to access information via environment variables of the child such as name, exposed ports, IP and other selected environment variables.

When linking two containers Docker will use the exposed ports of the container to create a secure tunnel for the parent to access. If a database container only exposes port 8080 then the linked container will only be allowed to access port 8080 and nothing else if inter-container communication is set to false.

For example, there is an image called crosbymichael/redis that exposes the port 6379 and starts the Redis server. Let’s name the container as redis based on that image and run it as daemon.

$ sudo docker run -d -name redis crosbymichael/redis

We can issue all the commands that you would expect using the name redis; start, stop, attach, using the name for our container. The name also allows us to link other containers into this one.

Next, we can start a new web application that has a dependency on Redis and apply a link to connect both containers. If you noticed when running our Redis server we did not use the -p flag to publish the Redis port to the host system. Redis exposed port 6379 and this is all we need to establish a link.

$ sudo docker run -t -i -link redis:db -name webapp ubuntu bash

When you specified -link redis:db you are telling Docker to link the container named redis into this new container with the alias db. Environment variables are prefixed with the alias so that the parent container can access network and environment information from the containers that are linked into it.

If we inspect the environment variables of the second container, we would see all the information about the child container.

$ root@4c01db0b339c:/# env

HOSTNAME=4c01db0b339c
DB_NAME=/webapp/db
TERM=xterm
DB_PORT=tcp://172.17.0.8:6379
DB_PORT_6379_TCP=tcp://172.17.0.8:6379
DB_PORT_6379_TCP_PROTO=tcp
DB_PORT_6379_TCP_ADDR=172.17.0.8
DB_PORT_6379_TCP_PORT=6379
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
SHLVL=1
HOME=/
container=lxc
_=/usr/bin/env
root@4c01db0b339c:/#

Accessing the network information along with the environment of the child container allows us to easily connect to the Redis service on the specific IP and port in the environment.

Running docker ps shows the 2 containers, and the webapp/db alias name for the redis container.

$ docker ps
CONTAINER ID        IMAGE                        COMMAND                CREATED              STATUS              PORTS               NAMES
4c01db0b339c        ubuntu:12.04                 bash                   17 seconds ago       Up 16 seconds                           webapp
d7886598dbe2        crosbymichael/redis:latest   /redis-server --dir    33 minutes ago       Up 33 minutes       6379/tcp            redis,webapp/db
在GitHub上编辑

Docker是一个开源项目, Docker Inc.资助, 使用apache 2.0 证书

文档托管在 Read the Docs

Twitter GitHub Reddit Google+ Facebook Slideshare Youtube Flickr LinkedIn

注释: 您当前正在浏览的开发文档。目前的版本可能工作方式不同.

可用版本:
  • latest
  • stable