mirror of
https://github.com/docker/docs.git
synced 2026-04-05 10:48:55 +07:00
1009 lines
42 KiB
HTML
1009 lines
42 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
|
|
|
<meta name="docker_version" content="1.4.1">
|
|
<meta name="docker_git_branch" content="master">
|
|
<meta name="docker_git_commit" content="3c097c2">
|
|
<meta name="docker_build_date" content="Wed Jan 28 04:30:29 UTC 2015">
|
|
|
|
<meta name="description" content="Learn how to connect Docker containers together.">
|
|
<meta name="keywords" content="Examples, Usage, user guide, links, linking, docker, documentation, examples, names, name, container naming, port, map, network port, network">
|
|
|
|
<link rel="canonical" href="/userguide/dockerlinks/">
|
|
<link href="/css/bootstrap-custom.css" rel="stylesheet">
|
|
<link href="/css/main.css" rel="stylesheet">
|
|
<link href="/css/prettify-1.0.css" rel="stylesheet">
|
|
<link rel="stylesheet" type="text/css" href="/css/dockerfile_tutorial.css">
|
|
<link href="/tipuesearch/tipuesearch.css" rel="stylesheet">
|
|
<link href="/css/docs.css" rel="stylesheet">
|
|
<link rel="shortcut icon" href="/img/favicon.png">
|
|
<title>Linking containers together - Docker Documentation</title>
|
|
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
|
|
<!--[if lt IE 9]>
|
|
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
|
|
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
|
|
<![endif]-->
|
|
|
|
<script type="text/javascript">
|
|
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
|
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
|
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
|
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
|
ga('create', 'UA-6096819-11', 'docker.com');
|
|
ga('require', 'linkid', 'linkid.js');
|
|
ga('send', 'pageview', {
|
|
'page': location.pathname + location.search + location.hash,
|
|
});
|
|
</script>
|
|
|
|
</head>
|
|
<body>
|
|
|
|
<div id="topmostnav" class="topmostnav_loggedout navbar navbar-static-top public">
|
|
<div class="container">
|
|
<a href="http://www.docker.com/" title="Homepage">
|
|
<div class="brand logo"><img src="/img/nav/docker-logo-loggedout.png"> </div>
|
|
</a>
|
|
<ul class="nav">
|
|
<li class=""><a href="http://www.docker.com/whatisdocker/" title="What is Docker">What is Docker?</a></li>
|
|
<li class=""><a href="http://www.docker.com/resources/usecases/" title="Use Cases">Use Cases</a></li>
|
|
<li class=""><a href="http://www.docker.com/tryit/" title="Try It!">Try It!</a></li>
|
|
<li class="active"><a href="https://docs.docker.com" title="Install & Docs">Install & Docs</a></li>
|
|
<li><a href="https://registry.hub.docker.com" title="Browse">Browse</a></li>
|
|
</ul>
|
|
<div id="usernav" class="pull-right">
|
|
<a href="https://hub.docker.com/account/login" class="btn nav-button2" title="Lg In">Log In</a>
|
|
<a href="https://hub.docker.com/account/signup" class="btn nav-button1" title="Sign Up">Sign Up</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div id="topmostnav" class="topmostnav_loggedin navbar navbar-static-top">
|
|
<div class="container">
|
|
<a href="http://www.docker.com/" title="Docker Docs Home"><div class="brand logo"><img src="/img/nav/docker-logo-loggedin.png"> </div></a>
|
|
<form id="search_box_header" class="navbar-index-search pull-right" action="https://registry.hub.docker.com/search">
|
|
<span role="status" aria-live="polite" class="ui-helper-hidden-accessible"></span><input type="text" class="search-query ui-autocomplete-input" placeholder="Search..." name="q" value="" autocomplete="off">
|
|
</form>
|
|
<ul class="nav">
|
|
<li><a href="https://registry.hub.docker.com" title="Browse Repos">Browse Repos</a></li>
|
|
<li class="active"><a href="http://docs.docker.com" title="Documentation">Documentation</a></li>
|
|
<li><a href="http://www.docker.com/community/participate/" title="Community">Community</a></li>
|
|
<li><a href="http://www.docker.com/resources/help/" title="Help">Help</a></li>
|
|
</ul>
|
|
<div id="usernav" class="pull-right">
|
|
<ul class="nav user">
|
|
<li class="dropdown">
|
|
<a id="logged-in-header-username" class="dropdown-toggle" data-toggle="dropdown" href="#">
|
|
<img class="profile" src="" alt="profile picture">
|
|
</a>
|
|
<ul class="dropdown-menu pull-right">
|
|
<li><a href="https://hub.docker.com/">View Profile</a></li>
|
|
<li><a href="https://hub.docker.com/account/settings/">Settings</a></li>
|
|
<li><a href="https://hub.docker.com/repos/">My Repositories</a></li>
|
|
<li><a href="https://hub.docker.com/plans/billing-info">Billing</a></li>
|
|
<li><a href="https://hub.docker.com/account/logout/?next=/">Log out</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="wrap">
|
|
<nav id="nav_menu" class="clearfix navbar navbar-default navbar-static-top affix" role="navigation">
|
|
<div id="docsnav">
|
|
<ul id="main-nav" class="pull-left">
|
|
|
|
|
|
<li class="dd_menu pull-left">
|
|
|
|
<a href="/">About</a>
|
|
|
|
<ul class="dd_submenu" style="max-height: 75px;">
|
|
|
|
<li >
|
|
<a href="/">Docker</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/release-notes/">Release Notes</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/introduction/understanding-docker/">Understanding Docker</a>
|
|
</li>
|
|
|
|
</ul>
|
|
</li>
|
|
|
|
|
|
|
|
<li class="dd_menu pull-left">
|
|
|
|
<a href="/installation/mac/">Installation</a>
|
|
|
|
<ul class="dd_submenu" style="max-height: 75px;">
|
|
|
|
<li >
|
|
<a href="/installation/mac/">Mac OS X</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/installation/ubuntulinux/">Ubuntu</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/installation/rhel/">Red Hat Enterprise Linux</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/installation/oracle/">Oracle Linux</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/installation/centos/">CentOS</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/installation/debian/">Debian</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/installation/gentoolinux/">Gentoo</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/installation/google/">Google Cloud Platform</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/installation/rackspace/">Rackspace Cloud</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/installation/amazon/">Amazon EC2</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/installation/softlayer/">IBM Softlayer</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/installation/archlinux/">Arch Linux</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/installation/frugalware/">FrugalWare</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/installation/fedora/">Fedora</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/installation/SUSE/">SUSE</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/installation/cruxlinux/">CRUX Linux</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/installation/windows/">Microsoft Windows</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/installation/binaries/">Binaries</a>
|
|
</li>
|
|
|
|
</ul>
|
|
</li>
|
|
|
|
|
|
|
|
<li class="dd_menu pull-left active">
|
|
|
|
<a href="/userguide/">User Guide</a>
|
|
|
|
<ul class="dd_submenu" style="max-height: 75px;">
|
|
|
|
<li >
|
|
<a href="/userguide/">The Docker User Guide</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/userguide/dockerhub/">Getting Started with Docker Hub</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/userguide/dockerizing/">Dockerizing Applications</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/userguide/usingdocker/">Working with Containers</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/userguide/dockerimages/">Working with Docker Images</a>
|
|
</li>
|
|
|
|
<li class="active">
|
|
<a href="/userguide/dockerlinks/">Linking containers together</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/userguide/dockervolumes/">Managing data in containers</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/userguide/dockerrepos/">Working with Docker Hub</a>
|
|
</li>
|
|
|
|
</ul>
|
|
</li>
|
|
|
|
|
|
|
|
<li class="dd_menu pull-left">
|
|
|
|
<a href="/docker-hub/">Docker Hub</a>
|
|
|
|
<ul class="dd_submenu" style="max-height: 75px;">
|
|
|
|
<li >
|
|
<a href="/docker-hub/">Docker Hub</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/docker-hub/accounts/">Accounts</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/docker-hub/repos/">Repositories</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/docker-hub/builds/">Automated Builds</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/docker-hub/official_repos/">Official Repo Guidelines</a>
|
|
</li>
|
|
|
|
</ul>
|
|
</li>
|
|
|
|
|
|
|
|
<li class="dd_menu pull-left">
|
|
|
|
<a href="/examples/nodejs_web_app/">Examples</a>
|
|
|
|
<ul class="dd_submenu" style="max-height: 75px;">
|
|
|
|
<li >
|
|
<a href="/examples/nodejs_web_app/">Dockerizing a Node.js web application</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/examples/mongodb/">Dockerizing MongoDB</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/examples/running_redis_service/">Dockerizing a Redis service</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/examples/postgresql_service/">Dockerizing a PostgreSQL service</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/examples/running_riak_service/">Dockerizing a Riak service</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/examples/running_ssh_service/">Dockerizing an SSH service</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/examples/couchdb_data_volumes/">Dockerizing a CouchDB service</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/examples/apt-cacher-ng/">Dockerizing an Apt-Cacher-ng service</a>
|
|
</li>
|
|
|
|
</ul>
|
|
</li>
|
|
|
|
|
|
|
|
<li class="dd_menu pull-left">
|
|
|
|
<a href="/articles/basics/">Articles</a>
|
|
|
|
<ul class="dd_submenu" style="max-height: 75px;">
|
|
|
|
<li >
|
|
<a href="/articles/basics/">Docker basics</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/articles/networking/">Advanced networking</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/articles/security/">Security</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/articles/https/">Running Docker with HTTPS</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/articles/registry_mirror/">Run a local registry mirror</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/articles/host_integration/">Automatically starting containers</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/articles/baseimages/">Creating a base image</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/articles/dockerfile_best-practices/">Best practices for writing Dockerfiles</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/articles/certificates/">Using certificates for repository client verification</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/articles/using_supervisord/">Using Supervisor</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/articles/cfengine_process_management/">Process management with CFEngine</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/articles/puppet/">Using Puppet</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/articles/chef/">Using Chef</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/articles/dsc/">Using PowerShell DSC</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/articles/ambassador_pattern_linking/">Cross-Host linking using ambassador containers</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/articles/runmetrics/">Runtime metrics</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/articles/b2d_volume_resize/">Increasing a Boot2Docker volume</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/articles/systemd/">Controlling and configuring Docker using Systemd</a>
|
|
</li>
|
|
|
|
</ul>
|
|
</li>
|
|
|
|
|
|
|
|
<li class="dd_menu pull-left">
|
|
|
|
<a href="/reference/commandline/cli/">Reference</a>
|
|
|
|
<ul class="dd_submenu" style="max-height: 75px;">
|
|
|
|
<li >
|
|
<a href="/reference/commandline/cli/">Command line</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/reference/builder/">Dockerfile</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/faq/">FAQ</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/reference/run/">Run Reference</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/reference/api/docker-io_api/">Docker Hub API</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/reference/api/registry_api/">Docker Registry API</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/reference/api/registry_api_client_libraries/">Docker Registry API Client Libraries</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/reference/api/hub_registry_spec/">Docker Hub and Registry Spec</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/reference/api/docker_remote_api/">Docker Remote API</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/reference/api/docker_remote_api_v1.16/">Docker Remote API v1.16</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/reference/api/docker_remote_api_v1.15/">Docker Remote API v1.15</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/reference/api/docker_remote_api_v1.14/">Docker Remote API v1.14</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/reference/api/docker_remote_api_v1.13/">Docker Remote API v1.13</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/reference/api/docker_remote_api_v1.12/">Docker Remote API v1.12</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/reference/api/docker_remote_api_v1.11/">Docker Remote API v1.11</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/reference/api/remote_api_client_libraries/">Docker Remote API Client Libraries</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/reference/api/docker_io_accounts_api/">Docker Hub Accounts API</a>
|
|
</li>
|
|
|
|
</ul>
|
|
</li>
|
|
|
|
|
|
|
|
<li class="dd_menu pull-left">
|
|
|
|
<a href="/contributing/contributing/">Contribute</a>
|
|
|
|
<ul class="dd_submenu" style="max-height: 75px;">
|
|
|
|
<li >
|
|
<a href="/contributing/contributing/">Contributing</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/contributing/devenvironment/">Development environment</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="/contributing/docs_style-guide/">Documentation style guide</a>
|
|
</li>
|
|
|
|
</ul>
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
<form id="nav_search" class="pull-right" action="/jsearch/">
|
|
<span role="status" aria-live="polite" class="ui-helper-hidden-accessible"></span>
|
|
<input name="q" id="tipue_search_input" type="text" class="search_input search-query ui-autocomplete-input" placeholder="Search the Docs" autocomplete="off">
|
|
</form>
|
|
</div>
|
|
</nav>
|
|
<div id="content" class="container">
|
|
<div class="row">
|
|
|
|
<div class="span3" id="leftnav">
|
|
<div id="toc_table">
|
|
<ul class="nav nav-tabs nav-stacked">
|
|
|
|
|
|
<li class=""><a href="#network-port-mapping-refresher">Network port mapping refresher</a></li>
|
|
|
|
|
|
<li class=""><a href="#docker-container-linking">Docker Container Linking</a></li>
|
|
|
|
|
|
<li class=""><a href="#container-naming">Container naming</a></li>
|
|
|
|
|
|
<li class=""><a href="#container-linking">Container Linking</a></li>
|
|
|
|
<h3><a href="#environment-variables">Environment Variables</a></h3>
|
|
|
|
<h3><a href="#updating-the-etchosts-file">Updating the /etc/hosts file</a></h3>
|
|
|
|
|
|
|
|
|
|
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
<div class="span9 content-body">
|
|
|
|
|
|
|
|
<div id="versionnav" class="span3 pull-right">
|
|
<ul class="nav version pull-right">
|
|
<li class="dropdown">
|
|
<a id="document-version-number" class="dropdown-toggle" data-toggle="dropdown" href="#">
|
|
Version v1.4
|
|
</a>
|
|
<ul id="documentation-version-list" class="dropdown-menu pull-right">
|
|
|
|
<li role="presentation" class="divider"></li>
|
|
<li> <a class="home-link3 tertiary-nav" href="https://github.com/docker/docker/blob/master/docs/sources/userguide/dockerlinks.md" >Edit on GitHub</a></li>
|
|
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
|
|
<h1 id="linking-containers-together">Linking Containers Together</h1>
|
|
<p>In <a href="/userguide/usingdocker">the Using Docker section</a>, you saw how you can
|
|
connect to a service running inside a Docker container via a network
|
|
port. But a port connection is only one way you can interact with services and
|
|
applications running inside Docker containers. In this section, we'll briefly revisit
|
|
connecting via a network port and then we'll introduce you to another method of access:
|
|
container linking.</p>
|
|
<h2 id="network-port-mapping-refresher">Network port mapping refresher</h2>
|
|
<p>In <a href="/userguide/usingdocker">the Using Docker section</a>, you created a
|
|
container that ran a Python Flask application:</p>
|
|
<pre class="prettyprint well"><code>$ sudo docker run -d -P training/webapp python app.py
|
|
</code></pre>
|
|
<blockquote>
|
|
<p><strong>Note:</strong>
|
|
Containers have an internal network and an IP address
|
|
(as we saw when we used the <code>docker inspect</code> command to show the container's
|
|
IP address in the <a href="/userguide/usingdocker/">Using Docker</a> section).
|
|
Docker can have a variety of network configurations. You can see more
|
|
information on Docker networking <a href="/articles/networking/">here</a>.</p>
|
|
</blockquote>
|
|
<p>When that container was created, the <code>-P</code> flag was used to automatically map any
|
|
network ports inside it to a random high port from the range 49153
|
|
to 65535 on our Docker host. Next, when <code>docker ps</code> was run, you saw that
|
|
port 5000 in the container was bound to port 49155 on the host.</p>
|
|
<pre class="prettyprint well"><code>$ sudo docker ps nostalgic_morse
|
|
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
|
|
</code></pre>
|
|
<p>You also saw how you can bind a container's ports to a specific port using
|
|
the <code>-p</code> flag:</p>
|
|
<pre class="prettyprint well"><code>$ sudo docker run -d -p 5000:5000 training/webapp python app.py
|
|
</code></pre>
|
|
<p>And you saw why this isn't such a great idea because it constrains you to
|
|
only one container on that specific port.</p>
|
|
<p>There are also a few other ways you can configure the <code>-p</code> flag. By
|
|
default the <code>-p</code> flag will bind the specified port to all interfaces on
|
|
the host machine. But you can also specify a binding to a specific
|
|
interface, for example only to the <code>localhost</code>.</p>
|
|
<pre class="prettyprint well"><code>$ sudo docker run -d -p 127.0.0.1:5000:5000 training/webapp python app.py
|
|
</code></pre>
|
|
<p>This would bind port 5000 inside the container to port 5000 on the
|
|
<code>localhost</code> or <code>127.0.0.1</code> interface on the host machine.</p>
|
|
<p>Or, to bind port 5000 of the container to a dynamic port but only on the
|
|
<code>localhost</code>, you could use:</p>
|
|
<pre class="prettyprint well"><code>$ sudo docker run -d -p 127.0.0.1::5000 training/webapp python app.py
|
|
</code></pre>
|
|
<p>You can also bind UDP ports by adding a trailing <code>/udp</code>. For example:</p>
|
|
<pre class="prettyprint well"><code>$ sudo docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py
|
|
</code></pre>
|
|
<p>You also learned about the useful <code>docker port</code> shortcut which showed us the
|
|
current port bindings. This is also useful for showing you specific port
|
|
configurations. For example, if you've bound the container port to the
|
|
<code>localhost</code> on the host machine, then the <code>docker port</code> output will reflect that.</p>
|
|
<pre class="prettyprint well"><code>$ sudo docker port nostalgic_morse 5000
|
|
127.0.0.1:49155
|
|
</code></pre>
|
|
<blockquote>
|
|
<p><strong>Note:</strong>
|
|
The <code>-p</code> flag can be used multiple times to configure multiple ports.</p>
|
|
</blockquote>
|
|
<h2 id="docker-container-linking">Docker Container Linking</h2>
|
|
<p>Network port mappings are not the only way Docker containers can connect
|
|
to one another. Docker also has a linking system that allows you to link
|
|
multiple containers together and send connection information from one to another.
|
|
When containers are linked, information about a source container can be sent to a
|
|
recipient container. This allows the recipient to see selected data describing
|
|
aspects of the source container.</p>
|
|
<h2 id="container-naming">Container naming</h2>
|
|
<p>To establish links, Docker relies on the names of your containers.
|
|
You've already seen that each container you create has an automatically
|
|
created name; indeed you've become familiar with our old friend
|
|
<code>nostalgic_morse</code> during this guide. You can also name containers
|
|
yourself. This naming provides two useful functions:</p>
|
|
<ol>
|
|
<li>
|
|
<p>It can be useful to name containers that do specific functions in a way
|
|
that makes it easier for you to remember them, for example naming a
|
|
container containing a web application <code>web</code>.</p>
|
|
</li>
|
|
<li>
|
|
<p>It provides Docker with a reference point that allows it to refer to other
|
|
containers, for example, you can specify to link the container <code>web</code> to container <code>db</code>.</p>
|
|
</li>
|
|
</ol>
|
|
<p>You can name your container by using the <code>--name</code> flag, for example:</p>
|
|
<pre class="prettyprint well"><code>$ sudo docker run -d -P --name web training/webapp python app.py
|
|
</code></pre>
|
|
<p>This launches a new container and uses the <code>--name</code> flag to
|
|
name the container <code>web</code>. You can see the container's name using the
|
|
<code>docker ps</code> command.</p>
|
|
<pre class="prettyprint well"><code>$ sudo docker ps -l
|
|
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
|
aed84ee21bde training/webapp:latest python app.py 12 hours ago Up 2 seconds 0.0.0.0:49154->5000/tcp web
|
|
</code></pre>
|
|
<p>You can also use <code>docker inspect</code> to return the container's name.</p>
|
|
<pre class="prettyprint well"><code>$ sudo docker inspect -f "{{ .Name }}" aed84ee21bde
|
|
/web
|
|
</code></pre>
|
|
<blockquote>
|
|
<p><strong>Note:</strong>
|
|
Container names have to be unique. That means you can only call
|
|
one container <code>web</code>. If you want to re-use a container name you must delete
|
|
the old container (with <code>docker rm</code>) before you can create a new
|
|
container with the same name. As an alternative you can use the <code>--rm</code>
|
|
flag with the <code>docker run</code> command. This will delete the container
|
|
immediately after it is stopped.</p>
|
|
</blockquote>
|
|
<h2 id="container-linking">Container Linking</h2>
|
|
<p>Links allow containers to discover each other and securely transfer information about one
|
|
container to another container. When you set up a link, you create a conduit between a
|
|
source container and a recipient container. The recipient can then access select data
|
|
about the source. To create a link, you use the <code>--link</code> flag. First, create a new
|
|
container, this time one containing a database.</p>
|
|
<pre class="prettyprint well"><code>$ sudo docker run -d --name db training/postgres
|
|
</code></pre>
|
|
<p>This creates a new container called <code>db</code> from the <code>training/postgres</code>
|
|
image, which contains a PostgreSQL database.</p>
|
|
<p>Now, you need to delete the <code>web</code> container you created previously so you can replace it
|
|
with a linked one:</p>
|
|
<pre class="prettyprint well"><code>$ sudo docker rm -f web
|
|
</code></pre>
|
|
<p>Now, create a new <code>web</code> container and link it with your <code>db</code> container.</p>
|
|
<pre class="prettyprint well"><code>$ sudo docker run -d -P --name web --link db:db training/webapp python app.py
|
|
</code></pre>
|
|
<p>This will link the new <code>web</code> container with the <code>db</code> container you created
|
|
earlier. The <code>--link</code> flag takes the form:</p>
|
|
<pre class="prettyprint well"><code>--link name:alias
|
|
</code></pre>
|
|
<p>Where <code>name</code> is the name of the container we're linking to and <code>alias</code> is an
|
|
alias for the link name. You'll see how that alias gets used shortly.</p>
|
|
<p>Next, inspect your linked containers with <code>docker inspect</code>:</p>
|
|
<pre class="prettyprint well"><code>$ sudo docker inspect -f "{{ .HostConfig.Links }}" web
|
|
[/db:/web/db]
|
|
</code></pre>
|
|
<p>You can see that the <code>web</code> container is now linked to the <code>db</code> container
|
|
<code>web/db</code>. Which allows it to access information about the <code>db</code> container.</p>
|
|
<p>So what does linking the containers actually do? You've learned that a link allows a
|
|
source container to provide information about itself to a recipient container. In
|
|
our example, the recipient, <code>web</code>, can access information about the source <code>db</code>. To do
|
|
this, Docker creates a secure tunnel between the containers that doesn't need to
|
|
expose any ports externally on the container; you'll note when we started the
|
|
<code>db</code> container we did not use either the <code>-P</code> or <code>-p</code> flags. That's a big benefit of
|
|
linking: we don't need to expose the source container, here the PostgreSQL database, to
|
|
the network.</p>
|
|
<p>Docker exposes connectivity information for the source container to the
|
|
recipient container in two ways:</p>
|
|
<ul>
|
|
<li>Environment variables,</li>
|
|
<li>Updating the <code>/etc/hosts</code> file.</li>
|
|
</ul>
|
|
<h3 id="environment-variables">Environment Variables</h3>
|
|
<p>When two containers are linked, Docker will set some environment variables
|
|
in the target container to enable programmatic discovery of information
|
|
related to the source container.</p>
|
|
<p>First, Docker will set an <code><alias>_NAME</code> environment variable specifying the
|
|
alias of each target container that was given in a <code>--link</code> parameter. So,
|
|
for example, if a new container called <code>web</code> is being linked to a database
|
|
container called <code>db</code> via <code>--link db:webdb</code> then in the <code>web</code> container
|
|
would be <code>WEBDB_NAME=/web/webdb</code>.</p>
|
|
<p>Docker will then also define a set of environment variables for each
|
|
port that is exposed by the source container. The pattern followed is:</p>
|
|
<ul>
|
|
<li><code><name>_PORT_<port>_<protocol></code> will contain a URL reference to the
|
|
port. Where <code><name></code> is the alias name specified in the <code>--link</code> parameter
|
|
(e.g. <code>webdb</code>), <code><port></code> is the port number being exposed, and <code><protocol></code>
|
|
is either <code>TCP</code> or <code>UDP</code>. The format of the URL will be:
|
|
<code><protocol>://<container_ip_address>:<port></code>
|
|
(e.g. <code>tcp://172.17.0.82:8080</code>). This URL will then be
|
|
split into the following 3 environment variables for convenience:</li>
|
|
<li><code><name>_PORT_<port>_<protocol>_ADDR</code> will contain just the IP address
|
|
from the URL (e.g. <code>WEBDB_PORT_8080_TCP_ADDR=172.17.0.82</code>).</li>
|
|
<li><code><name>_PORT_<port>_<protocol>_PORT</code> will contain just the port number
|
|
from the URL (e.g. <code>WEBDB_PORT_8080_TCP_PORT=8080</code>).</li>
|
|
<li><code><name>_PORT_<port>_<protocol>_PROTO</code> will contain just the protocol
|
|
from the URL (e.g. <code>WEBDB_PORT_8080_TCP_PROTO=tcp</code>).</li>
|
|
</ul>
|
|
<p>If there are multiple ports exposed then the above set of environment
|
|
variables will be defined for each one.</p>
|
|
<p>Finally, there will be an environment variable called <code><alias>_PORT</code> that will
|
|
contain the URL of the first exposed port of the source container.
|
|
For example, <code>WEBDB_PORT=tcp://172.17.0.82:8080</code>. In this case, 'first'
|
|
is defined as the lowest numbered port that is exposed. If that port is
|
|
used for both tcp and udp, then the tcp one will be specified.</p>
|
|
<p>Returning back to our database example, you can run the <code>env</code>
|
|
command to list the specified container's environment variables.</p>
|
|
<pre class="prettyprint well"><code> $ sudo docker run --rm --name web2 --link db:db training/webapp env
|
|
. . .
|
|
DB_NAME=/web2/db
|
|
DB_PORT=tcp://172.17.0.5:5432
|
|
DB_PORT_5432_TCP=tcp://172.17.0.5:5432
|
|
DB_PORT_5432_TCP_PROTO=tcp
|
|
DB_PORT_5432_TCP_PORT=5432
|
|
DB_PORT_5432_TCP_ADDR=172.17.0.5
|
|
. . .
|
|
</code></pre>
|
|
|
|
<blockquote>
|
|
<p><strong>Note</strong>:
|
|
These Environment variables are only set for the first process in the
|
|
container. Similarly, some daemons (such as <code>sshd</code>)
|
|
will scrub them when spawning shells for connection.</p>
|
|
<p><strong>Note</strong>:
|
|
Unlike host entries in the <a href="#updating-the-etchosts-file"><code>/ets/hosts</code> file</a>,
|
|
IP addresses stored in the environment variables are not automatically updated
|
|
if the source container is restarted. We recommend using the host entries in
|
|
<code>/etc/hosts</code> to resolve the IP address of linked containers.</p>
|
|
</blockquote>
|
|
<p>You can see that Docker has created a series of environment variables with
|
|
useful information about the source <code>db</code> container. Each variable is prefixed with
|
|
<code>DB_</code>, which is populated from the <code>alias</code> you specified above. If the <code>alias</code>
|
|
were <code>db1</code>, the variables would be prefixed with <code>DB1_</code>. You can use these
|
|
environment variables to configure your applications to connect to the database
|
|
on the <code>db</code> container. The connection will be secure and private; only the
|
|
linked <code>web</code> container will be able to talk to the <code>db</code> container.</p>
|
|
<h3 id="updating-the-etchosts-file">Updating the <code>/etc/hosts</code> file</h3>
|
|
<p>In addition to the environment variables, Docker adds a host entry for the
|
|
source container to the <code>/etc/hosts</code> file. Here's an entry for the <code>web</code>
|
|
container:</p>
|
|
<pre class="prettyprint well"><code>$ sudo docker run -t -i --rm --link db:db training/webapp /bin/bash
|
|
root@aed84ee21bde:/opt/webapp# cat /etc/hosts
|
|
172.17.0.7 aed84ee21bde
|
|
. . .
|
|
172.17.0.5 db
|
|
</code></pre>
|
|
<p>You can see two relevant host entries. The first is an entry for the <code>web</code>
|
|
container that uses the Container ID as a host name. The second entry uses the
|
|
link alias to reference the IP address of the <code>db</code> container. You can ping
|
|
that host now via this host name.</p>
|
|
<pre class="prettyprint well"><code>root@aed84ee21bde:/opt/webapp# apt-get install -yqq inetutils-ping
|
|
root@aed84ee21bde:/opt/webapp# ping db
|
|
PING db (172.17.0.5): 48 data bytes
|
|
56 bytes from 172.17.0.5: icmp_seq=0 ttl=64 time=0.267 ms
|
|
56 bytes from 172.17.0.5: icmp_seq=1 ttl=64 time=0.250 ms
|
|
56 bytes from 172.17.0.5: icmp_seq=2 ttl=64 time=0.256 ms
|
|
</code></pre>
|
|
<blockquote>
|
|
<p><strong>Note:</strong>
|
|
In the example, you'll note you had to install <code>ping</code> because it was not included
|
|
in the container initially.</p>
|
|
</blockquote>
|
|
<p>Here, you used the <code>ping</code> command to ping the <code>db</code> container using its host entry,
|
|
which resolves to <code>172.17.0.5</code>. You can use this host entry to configure an application
|
|
to make use of your <code>db</code> container.</p>
|
|
<blockquote>
|
|
<p><strong>Note:</strong>
|
|
You can link multiple recipient containers to a single source. For
|
|
example, you could have multiple (differently named) web containers attached to your
|
|
<code>db</code> container.</p>
|
|
</blockquote>
|
|
<p>If you restart the source container, the linked containers <code>/etc/hosts</code> files
|
|
will be automatically updated with the source container's new IP address,
|
|
allowing linked communication to continue.</p>
|
|
<pre class="prettyprint well"><code>$ sudo docker restart db
|
|
root@aed84ee21bde:/opt/webapp# cat /etc/hosts
|
|
172.17.0.7 aed84ee21bde
|
|
. . .
|
|
172.17.0.9 db
|
|
</code></pre>
|
|
<h1 id="next-step">Next step</h1>
|
|
<p>Now that you know how to link Docker containers together, the next step is
|
|
learning how to manage data, volumes and mounts inside your containers.</p>
|
|
<p>Go to <a href="/userguide/dockervolumes">Managing Data in Containers</a>.</p>
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="push-footer"></div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div id="footer-container" class="container">
|
|
<div id="footer" class="grey-body">
|
|
<div class="row">
|
|
<div class="span2">
|
|
<span class="footer-title">Community</span>
|
|
<ul class="unstyled">
|
|
<li><a class="primary-button" href="https://www.docker.com/community/events/">Events</a></li>
|
|
<li><a class="primary-button" href="http://posts.docker.com">Friends' Posts</a></li>
|
|
<li><a class="primary-button" href="https://www.docker.com/community/meetups/">Meetups</a></li>
|
|
<li><a class="primary-button" href="https://www.docker.com/community/governance/">Governance</a></li>
|
|
<li><a class="primary-button" href="http://forums.docker.com">Forums</a></li>
|
|
<li><a class="primary-button" href="http://botbot.me/freenode/docker">IRC</a></li>
|
|
<li><a class="primary-button" href="https://github.com/docker/docker">GitHub</a></li>
|
|
<li><a class="primary-button" href="http://stackoverflow.com/search?q=docker">Stackoverflow</a></li>
|
|
<li><a class="primary-button" href="http://www.cafepress.com/docker">Swag</a></li>
|
|
</ul>
|
|
</div>
|
|
<div class="span2">
|
|
<span class="footer-title">Enterprise</span>
|
|
<ul class="unstyled">
|
|
<li><a class="primary-button" href="https://www.docker.com/enterprise/support/">Support</a></li>
|
|
<li><a class="primary-button" href="https://www.docker.com/enterprise/education/">Education</a></li>
|
|
<li><a class="primary-button" href="https://www.docker.com/enterprise/services/">Services</a></li>
|
|
</ul>
|
|
<span class="footer-title">Partner Solutions</span>
|
|
<ul class="unstyled">
|
|
<li><a class="primary-button" href="https://www.docker.com/partners/find/">Find a Partner</a></li>
|
|
<li><a class="primary-button" href="https://www.docker.com/partners/program/">Partner Program</a></li>
|
|
<li><a class="primary-button" href="https://www.docker.com/partners/learn/">Learn More</a></li>
|
|
</ul>
|
|
</div>
|
|
<div class="span2">
|
|
<span class="footer-title">Resources</span>
|
|
<ul class="unstyled">
|
|
<li><a class="primary-button" href="https://docs.docker.com">Documentation</a></li>
|
|
<li><a class="primary-button" href="https://www.docker.com/resources/help/">Help</a></li>
|
|
<li><a class="primary-button" href="https://www.docker.com/resources/usecases/">Use Cases</a></li>
|
|
<li><a class="primary-button" href="http://www.docker.com/tryit/">Online Tutorial</a></li>
|
|
<li><a class="primary-button" href="https://www.docker.com/resources/howtobuy/">How To Buy</a></li>
|
|
<li><a class="primary-button" href="http://status.docker.com">Status</a></li>
|
|
<li><a class="primary-button" href="https://www.docker.com/resources/security/">Security</a></li>
|
|
</ul>
|
|
</div>
|
|
<div class="span2">
|
|
<span class="footer-title">Company</span>
|
|
<ul class="unstyled">
|
|
<li><a class="primary-button" href="https://www.docker.com/company/aboutus/">About Us</a></li>
|
|
<li><a class="primary-button" href="https://www.docker.com/company/team/">Team</a></li>
|
|
<li><a class="primary-button" href="https://www.docker.com/company/news/">News</a></li>
|
|
<li><a class="primary-button" href="https://www.docker.com/company/press/">Press</a></li>
|
|
<li><a class="primary-button" href="https://www.docker.com/company/careers/">Careers</a></li>
|
|
<li><a class="primary-button" href="https://www.docker.com/company/contact/">Contact</a></li>
|
|
</ul>
|
|
</div>
|
|
<div class="span3">
|
|
<span class="footer-title">Connect</span>
|
|
<div class="search">
|
|
<span>Subscribe to our newsletter</span>
|
|
<form action="https://www.docker.com/subscribe_newsletter/" method="post">
|
|
<input type='hidden' name='csrfmiddlewaretoken' value='aWL78QXQkY8DSKNYh6cl08p5eTLl7sOa' />
|
|
<tr><th><label for="id_email">Email:</label></th><td><input class="form-control" id="id_email" name="email" placeholder="Enter your email" type="text" /></td></tr>
|
|
|
|
<button type="submit"><i class="icon-arrow-right"></i> </button>
|
|
</form>
|
|
</div>
|
|
<ul class="unstyled social">
|
|
<li><a title="Docker on Twitter" class="primary-button blog" href="http://blog.docker.com">Blog</a></li>
|
|
<li><a title="Docker on Twitter" class="primary-button twitter" href="http://twitter.com/docker">Twitter</a></li>
|
|
<li><a title="Docker on Google+" class="primary-button googleplus" href="https://plus.google.com/u/0/communities/108146856671494713993">Google+</a></li>
|
|
<li><a title="Docker on Facebook" class="primary-button facebook" href="https://www.facebook.com/docker.run">Facebook</a></li>
|
|
<li><a title="Docker on Youtube" class="primary-button youtube" href="http://www.youtube.com/user/dockerrun">YouTube</a></li>
|
|
</ul>
|
|
<ul class="unstyled social">
|
|
<li><a title="Docker on SlideShare" class="primary-button slideshare" href="http://www.slideshare.net/Docker">Slideshare</a></li>
|
|
<li>
|
|
<a title="Docker on LinkedIn" class="primary-button" href="https://www.linkedin.com/company/docker">
|
|
<span class="linkedin"></span>
|
|
LinkedIn
|
|
</a>
|
|
</li>
|
|
<li>
|
|
<a title="Docker on GitHub" class="primary-button" href="https://github.com/docker/">
|
|
<span class="github"></span>
|
|
GitHub
|
|
</a>
|
|
</li>
|
|
<li>
|
|
<a title="Docker on Reddit" class="primary-button" href="http://www.reddit.com/r/docker">
|
|
<span class="reddit"></span>
|
|
Reddit
|
|
</a>
|
|
</li>
|
|
<li>
|
|
<a title="Docker on AngelList" class="primary-button" href="https://angel.co/docker-inc-1">
|
|
<span class="angellist"></span>
|
|
AngelList
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
<div class="row clearfix">
|
|
<div class="span6 pagination-right copyright">
|
|
<span>© 2014-2015 Docker, Inc.</span>
|
|
</div>
|
|
<div class="span6 pagination-left copyright">
|
|
<a href="http://www.docker.com/legal/terms_of_service">Terms</a> ·
|
|
<a href="http://www.docker.com/legal/privacy_policy">Privacy</a> ·
|
|
<a href="http://www.docker.com/legal/trademark_guidelines">Trademarks</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="/js/jquery-1.10.2.min.js"></script>
|
|
<script src="/js/jquery.cookie.js" ></script>
|
|
<script src="/js/jquery-scrolltofixed-min.js"></script>
|
|
<script src="/js/bootstrap-3.0.3.min.js"></script>
|
|
<script src="/js/prettify-1.0.min.js"></script>
|
|
<script src="/js/dockerfile_tutorial.js"></script>
|
|
<script src="/js/dockerfile_tutorial_level.js"></script>
|
|
<script src="/js/base.js"></script>
|
|
<script src="/tipuesearch/tipuesearch_set.js"></script>
|
|
<script src="/tipuesearch/tipuesearch.min.js"></script>
|
|
<script type="text/javascript">
|
|
piAId = '45082';
|
|
piCId = '1482';
|
|
|
|
(function() {
|
|
function async_load(){
|
|
var s = document.createElement('script'); s.type = 'text/javascript';
|
|
s.src = ('https:' == document.location.protocol ? 'https://pi' : 'http://cdn') + '.pardot.com/pd.js';
|
|
var c = document.getElementsByTagName('script')[0]; c.parentNode.insertBefore(s, c);
|
|
}
|
|
if(window.attachEvent) { window.attachEvent('onload', async_load); }
|
|
else { window.addEventListener('load', async_load, false); }
|
|
})();
|
|
</script>
|
|
<script type="text/javascript">
|
|
$(document).ready(function() {
|
|
$('#content').css("min-height", $(window).height() - 553 );
|
|
// if the URL contains a version string, update the version picker to reflect that
|
|
version = document.location.pathname.match(/^\/(v\d\.\d)\/.*/)
|
|
if (version && version[1]) {
|
|
$('#document-version-number')[0].text = 'Version '+version[1];
|
|
} else {
|
|
$('#document-version-number')[0].text = $('#document-version-number')[0].text + " (Latest)"
|
|
}
|
|
// load the complete versions list
|
|
$.get("/versions.html_fragment", function( data ) {
|
|
$('#documentation-version-list').prepend(data);
|
|
//remove any "/v1.1/" bits from front, so we can add the path to the version selection dropdown.
|
|
path = document.location.pathname.replace(/^\/v\d\.\d/, "");
|
|
$('#documentation-version-list a.version').each(function(i, e) {
|
|
e.href = e.href+path;
|
|
$(e).removeClass()
|
|
});
|
|
});
|
|
|
|
})
|
|
var userName = getCookie('docker_sso_username');
|
|
if (userName) {
|
|
$('.topmostnav_loggedout').hide();
|
|
$('.topmostnav_loggedin').show();
|
|
$('#logged-in-header-username').text(userName);
|
|
} else {
|
|
$('.topmostnav_loggedout').show();
|
|
$('.topmostnav_loggedin').hide();
|
|
}
|
|
</script>
|
|
</body>
|
|
</html> |