Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
The things we've covered so far would allow you to run single Docker containers that host a website, or a web service with only one exposed port. But that's only the simplest use of Docker's networking capabilities. There are many real-world use cases for connecting two, ten, or even a hundred containers together.
New Terms
- Port - A port is a property of a machine that you can access via networking protocols, and is used to transmit data around the machine itself and to other machines. For example, websites run by default on port 80 and port 443, and SSH runs on port 22.
- Bridge Network - a single aggregate network from multiple communication networks or network segments, but in Docker land, this means we’re bridging together the Docker container with the Host OS.
- Nginx - a popular web server/reverse proxy used for deploying websites and services on Linux machines.
Further Reading
The things we've covered so far would
allow you to run single Docker containers
0:00
that host a website or
a web service with only one exposed port.
0:04
But that's only the simplest use of
Docker's networking capabilities.
0:08
There are many real world use cases for
connecting two containers together,
0:13
such as running a JavaScript frontend
that fetches data from a Node.js backend.
0:17
Or spending up ten servers running
big-data processing software, like Spark.
0:22
Using Docker's networking,
each container can share the workload.
0:27
Using Docker here will let you
work with large amounts of data,
0:31
without the hassles typical
of large-scale deployments.
0:34
Networking is one of Docker's
most fundamental strengths.
0:38
From exposing ports to sharing
a network between containers,
0:42
Docker can do almost anything.
0:46
We briefly demonstrated the simplest
use of Docker's networking
0:49
capabilities before.
0:52
That is, exposing a single port and
publishing it on a port on the host.
0:53
Let's review that process using a Docker
image from the NGINX web server.
0:58
We'll retrieve the NGINX image from the
Docker registry with, docker pull nginx.
1:03
It'll go out to the Docker registry and
download the image we need.
1:11
Then we can use the docker inspect
command on the nginx image to confirm
1:16
which port it exposes,
with docker inspect nginx.
1:21
That will give us the images
configuration in JSON format.
1:24
Under the ExposedPorts key,
we see that it exposes port 80,
1:28
which make sense considering web
browsers connect to port 80 by default.
1:32
Now, let's create a container from
the image using the docker run command.
1:37
We'll have to publish
the ExposedPort to a port on
1:43
our host if we want it to be
accessible using the -p Option.
1:46
But while we're just playing around using
a development machine as our Docker host,
1:50
we probably won't be able to use port 80.
1:54
There may already be
a server running there.
1:56
And even if there weren't, we would need
administrative privileges to open port 80.
1:58
So we'll just take the exposed port 80 and
publish it as port 8080 instead.
2:02
So we'll type 8080 as the port publish to,
:, and then the expose port 80.
2:08
Since we're just demonstrating,
2:14
there's one more option I
want to add here, --detach.
2:16
Normally, it would attach our
terminal to the NGINX process, and
2:20
not let us do anything else
until we hold to the NGINX.
2:24
But adding this option will leave
the container running in the background so
2:26
we can do other things.
2:30
And the last thing we need
to add to our run command
2:31
is the name of the image we want to use,
nginx.
2:34
Docker will spun a container from
the nginx image, show us an ID for
2:38
the new container,
then return us to our shell props.
2:42
We can get a list of all the running
containers including our new nginx
2:45
container with the docker ps command,
ps stands for process status.
2:49
In the listing,
you'll see part of the container ID again.
2:55
The name of the image it was started from.
2:58
The command that's running within
the container, that's the nginx server.
3:01
And here, under the port setting,
3:06
my apologies that it is wrapped
around to the edge of the terminal.
3:07
You'll see the port 80 is
published as port 80->80.
3:10
Let's try reconnecting to the container
through the publish port.
3:15
In your browser's address bar,
3:18
type local host to connect to your own
computer, which is also the Docker host.
3:20
Then type :8080, and hit Enter to
connect to the publish port, 8080.
3:24
Your request will be forwarded to the
exposed port on the container, port 80.
3:31
And you'll get a response
from the nginx server.
3:35
When you first install Docker,
it creates three networks automatically.
3:38
To view them, run docker network ls.
3:42
These three networks are already
built in the Docker.
3:47
When you run a container, you can pass
the double badge network equals flag
3:49
to the wrong command to specify which
networks your container can talk to.
3:54
By default, Docker containers
connect to the bridge network.
3:58
If we run the command Docker
network inspect bridge,
4:02
We'll see various info about the network,
4:09
including a list of
the containers connected to it.
4:11
Right now,
we'll only see our nginx container.
4:15
We can stop the nginx container
using the docker stop command.
4:18
But we're going to need to
specify which container to stop.
4:23
So first,
4:26
I'm going to run the docker ps command
to get a list of running containers.
4:26
Then I'll copy the container
ID shown under Docker PS, and
4:31
paste that into the docker stop command.
4:36
We don't need to type the entire ID,
just enough characters so
4:41
that it can't be confused with
the IDs of other containers.
4:44
If we run docker ps again,
we´ll see that the container has stopped.
4:47
And if we run,
docker network inspect bridge again,
4:52
We´ll see that all containers have
been removed from the bridge network.
5:00
Now, let´s run two containers
at once in the same network, and
5:04
try connecting one to the other.
5:07
We'll be using the Ubuntu image for
5:09
this which contains
the Ubuntu Linux operating system.
5:11
In case we don't already have it,
we'll first run a command to
5:14
hold the Ubuntu image from the Docker
registry, with docker pull ubuntu.
5:17
Then we'll create two containers based
off the image with a docker run command.
5:24
We want to be able to attach to
a shell within these containers.
5:30
So we'll pass the -i flag to docker run,
5:33
which will set this up as an interactive
session on the container.
5:36
We also need to allocate
a teletype interface, so
5:40
that we can communicate with
the shell using our terminal.
5:43
So we'll add a t flag right after -i.
5:45
We want to detach from the container
at first so we can run other commands.
5:48
So we´ll add the --detach option again.
5:52
This time, let´s also specify a name for
5:56
our container to make
it easier to identify.
5:58
We´ll do that with the --name option.
6:01
We type an = after the --name option, and
then the name of the container we want.
6:05
We´ll use container1.
6:10
And again, we need to specify which
image we want to base the container on.
6:13
So we'll add ubuntu.
6:16
If we hit enter, Docker will print
the ID of the new container and
6:19
then return us to our shell prompt.
6:22
Let's bring up the same command
again to start up another container.
6:24
We'll use all the same parameters except
that we'll change the name to container2.
6:27
If we run docker ps,
we'll see both containers.
6:33
This listing will include the mains
we assigned in the last column.
6:37
With our two containers running,
if we type docker network
6:41
inspect bridge, We'll see both
containers listed on the network.
6:46
We'll also see IP addresses listed for
both.
6:54
Let's copy the address for container2.
6:57
Now, these addresses aren't accessible
to the Internet at large or
7:01
even to our Docker host.
7:04
If we try to ping one of
these IP addresses from our
7:06
host with the ping command and
pasting in the address there.
7:09
The connection will fail, but
7:18
these addresses are accessible
from container to container.
7:19
Let's open a shell session
on our first container, and
7:23
try pinging the second container.
7:26
We'll run the command, docker attach
7:28
container1, which will attach
to container1's entry point,
7:33
which for this container,
is a badge shell.
7:38
Since we've assigned the container a name,
7:41
we can use that name in
place of the container ID.
7:43
This time, the prompt that's displayed is
a shell on our container, not our host OS.
7:46
Now, let's try to run the same command,
ping and
7:51
the IP address that was shown
in the network listing.
7:54
But we hit a problem.
7:59
The guest operating system on docker
images is usually seriously stripped down,
8:00
so that it consumes fewer resources.
8:05
Even basic commands like
ping are often missing.
8:08
So first, we'll need to install
the ping command using apt-get,
8:11
the Ubuntu package manager.
8:16
We'll run apt-get update first, which will
update the list of available packages.
8:18
Then we'll run apt-get install
8:28
iputils-ping to install the ping command.
8:32
It will take a minute to download and
install the package,
8:37
after which we'll be returned
to the shell prompt.
8:39
Now, we should be ready
to try the ping command.
8:45
We'll bring up the exact same
command again and run it.
8:48
And this time, container2 will respond.
8:52
Docker established a connection between
the two containers over its bridge
8:55
network.
8:59
Before we wrap this video,
we should do a little clean up.
9:00
Type exit, to exit the shell on
container1, and get back to the host OS.
9:03
Since the shell was the primary
process for the container,
9:08
container1 should shut
down as soon as we exit.
9:12
Now, we just need to shut down container2,
9:15
docker stop container2.
9:20
Again, we can use the container
name in place of a container ID.
9:23
If we run, docker network,
inspect bridge again,
9:27
we can see that both our containers
are disconnected from the bridge network.
9:33
In this stage,
we covered the building blocks of Docker.
9:40
From retrieving images from the Docker
registry, to creating containers from
9:42
those images, to connecting containers
with Docker's networking features.
9:47
Up next, we'll take a closer look at using
Dockerfiles to create your own images.
9:52
See you in the next stage.
9:57
You need to sign up for Treehouse in order to download course files.
Sign up