mirror of
https://github.com/clearlinux/docker.git
synced 2026-09-05 21:21:42 +00:00
First pass at consolidating
Removing old networking.md Updating dockernetworks.md with images Adding information on network plugins Adding blurb about links to docker networking Updating the working documentation Adding Overlay Getting Started Downplaying links by removing refs/examples, adding refs/examples for network. Updating getting started to reflect networks not links Pulling out old network material Updating per discussion with Madhu to add Default docs section Updating with bridge default Fix bad merge Updating with new cluster-advertise behavior Update working and NetworkSettings examples Correcting example for default bridge discovery behavior Entering comments Fixing broken Markdown Syntax Updating with comments Updating all the links Signed-off-by: Mary Anthony <mary@docker.com> (cherry picked from commit 9ef855f9e5fa8077468bda5ce43155318c58e60e)
This commit is contained in:
@@ -1,41 +1,35 @@
|
||||
<!--[metadata]>
|
||||
+++
|
||||
title = "Working with containers"
|
||||
title = "Run a simple application"
|
||||
description = "Learn how to manage and operate Docker containers."
|
||||
keywords = ["docker, the docker guide, documentation, docker.io, monitoring containers, docker top, docker inspect, docker port, ports, docker logs, log, Logs"]
|
||||
[menu.main]
|
||||
parent="smn_containers"
|
||||
weight=-5
|
||||
+++
|
||||
<![end-metadata]-->
|
||||
|
||||
# Working with containers
|
||||
# Run a simple application
|
||||
|
||||
In the [last section of the Docker User Guide](dockerizing.md)
|
||||
we launched our first containers. We launched containers using the
|
||||
`docker run` command:
|
||||
|
||||
* Interactive container runs in the foreground.
|
||||
* Daemonized container runs in the background.
|
||||
|
||||
In the process we learned about several Docker commands:
|
||||
In the ["*Hello world in a container*"](dockerizing.md) you launched your
|
||||
first containers using the `docker run` command. You ran an *interactive container* that ran in the foreground. You also ran a *detached container* that ran in the background. In the process you 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](https://www.docker.com/tryit/).
|
||||
## Learn about the Docker client
|
||||
|
||||
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.
|
||||
If you didn't realize it yet, you've been using the Docker client each time you
|
||||
typed `docker` in your Bash terminal. The client is a simple command line client
|
||||
also known as a command-line interface (CLI). Each action you can take with
|
||||
the client is a command and each command can take a series of flags and arguments.
|
||||
|
||||
# Usage: [sudo] docker [command] [flags] [arguments] ..
|
||||
# Usage: [sudo] docker [subcommand] [flags] [arguments] ..
|
||||
# Example:
|
||||
$ docker run -i -t ubuntu /bin/bash
|
||||
|
||||
Let's see this in action by using the `docker version` command to return
|
||||
You can see this in action by using the `docker version` command to return
|
||||
version information on the currently installed Docker client and daemon.
|
||||
|
||||
$ docker version
|
||||
@@ -43,7 +37,7 @@ version information on the currently installed Docker client and daemon.
|
||||
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: 1.8.1
|
||||
API version: 1.20
|
||||
@@ -80,52 +74,52 @@ To see usage for a specific command, specify the command with the `--help` flag:
|
||||
--no-stdin=false Do not attach stdin
|
||||
--sig-proxy=true Proxy all received signals to the process
|
||||
|
||||
> **Note:**
|
||||
> **Note:**
|
||||
> For further details and examples of each command, see the
|
||||
> [command reference](../reference/commandline/cli.md) in this guide.
|
||||
|
||||
## Running a web application in Docker
|
||||
|
||||
So now we've learnt a bit more about the `docker` client let's move onto
|
||||
So now you've learned a bit more about the `docker` client you can move onto
|
||||
the important stuff: running more containers. So far none of the
|
||||
containers we've run did anything particularly useful, so let's
|
||||
containers you've run did anything particularly useful, so you can
|
||||
change that 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 run` command.
|
||||
Start with a `docker run` command.
|
||||
|
||||
$ 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 `-d` flag which tells Docker to run the
|
||||
Review what the command did. You've specified two flags: `-d` and
|
||||
`-P`. You've already seen the `-d` flag 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
|
||||
You've specified an image: `training/webapp`. This image is a
|
||||
pre-built image you'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.
|
||||
Lastly, you've specified a command for our container to run: `python app.py`. This launches our web application.
|
||||
|
||||
> **Note:**
|
||||
> **Note:**
|
||||
> You can see more detail on the `docker run` command in the [command
|
||||
> reference](../reference/commandline/run.md) and the [Docker Run
|
||||
> Reference](../reference/run.md).
|
||||
|
||||
## Viewing our web application container
|
||||
|
||||
Now let's see our running container using the `docker ps` command.
|
||||
Now you can see your running container using the `docker ps` command.
|
||||
|
||||
$ 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`
|
||||
You can see you've specified a new flag, `-l`, for the `docker ps`
|
||||
command. This tells the `docker ps` command to return the details of the
|
||||
*last* container started.
|
||||
|
||||
> **Note:**
|
||||
> **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.
|
||||
|
||||
@@ -139,7 +133,7 @@ column.
|
||||
When we passed the `-P` flag to the `docker run` command Docker mapped any
|
||||
ports exposed in our image to our host.
|
||||
|
||||
> **Note:**
|
||||
> **Note:**
|
||||
> We'll learn more about how to expose ports in Docker images when
|
||||
> [we learn how to build images](dockerimages.md).
|
||||
|
||||
@@ -158,12 +152,13 @@ This would map port 5000 inside our container to port 80 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 their own containers.
|
||||
Without Docker's port mapping you could only access one at a time on the
|
||||
Docker host.
|
||||
one of each port on your local host.
|
||||
|
||||
So let's now browse to port 49155 in a web browser to
|
||||
Suppose you want to test two Python applications: both bound to port 5000 inside
|
||||
their own containers. Without Docker's port mapping you could only access one at
|
||||
a time on the Docker host.
|
||||
|
||||
So you can now browse to port 49155 in a web browser to
|
||||
see the application.
|
||||
|
||||
.
|
||||
@@ -174,10 +169,10 @@ Our Python application is live!
|
||||
> If you have been using a 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 `docker-machine ip your_vm_name` from your command line or terminal application, for example:
|
||||
>
|
||||
>
|
||||
> $ docker-machine ip my-docker-vm
|
||||
> 192.168.99.100
|
||||
>
|
||||
>
|
||||
> In this case you'd browse to `http://192.168.99.100:49155` for the above example.
|
||||
|
||||
## A network port shortcut
|
||||
@@ -190,20 +185,20 @@ corresponding public-facing port.
|
||||
$ 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
|
||||
In this case you'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`.
|
||||
You can also find out a bit more about what's happening with our application and
|
||||
use another of the commands you've learned, `docker logs`.
|
||||
|
||||
$ 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
|
||||
This time though you'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.
|
||||
@@ -228,7 +223,7 @@ configuration and status information for the specified container.
|
||||
|
||||
$ docker inspect nostalgic_morse
|
||||
|
||||
Let's see a sample of that JSON output.
|
||||
You can see a sample of that JSON output.
|
||||
|
||||
[{
|
||||
"ID": "bc533791f3f500b280a9626688bc79e342e3ea0d528efe3a86a51ecb28ea20",
|
||||
@@ -246,12 +241,12 @@ Let's see a sample of that JSON output.
|
||||
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:
|
||||
|
||||
$ docker inspect -f '{{ .NetworkSettings.IPAddress }}' nostalgic_morse
|
||||
$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 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
|
||||
Okay you've seen web application working. Now you can stop it using the
|
||||
`docker stop` command and the name of our container: `nostalgic_morse`.
|
||||
|
||||
$ docker stop nostalgic_morse
|
||||
@@ -266,8 +261,8 @@ been stopped.
|
||||
|
||||
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.
|
||||
can create a new container or restart the old one. Look at
|
||||
starting your previous container back up.
|
||||
|
||||
$ docker start nostalgic_morse
|
||||
nostalgic_morse
|
||||
@@ -276,21 +271,21 @@ 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:**
|
||||
> **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.
|
||||
and won't need it again. Now, you can remove it using the `docker rm` command.
|
||||
|
||||
$ 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 happened? We can't actually remove a running container. This protects
|
||||
you from accidentally removing a running container you might need. Let's try
|
||||
you from accidentally removing a running container you might need. You can try
|
||||
this again by stopping the container first.
|
||||
|
||||
$ docker stop nostalgic_morse
|
||||
@@ -305,9 +300,7 @@ And now our container is stopped and deleted.
|
||||
|
||||
# Next steps
|
||||
|
||||
Until now we've only used images that we've downloaded from
|
||||
[Docker Hub](https://hub.docker.com). Next, let's get introduced to
|
||||
building and sharing our own images.
|
||||
Until now you've only used images that you've downloaded from Docker Hub. Next,
|
||||
you can get introduced to building and sharing our own images.
|
||||
|
||||
Go to [Working with Docker Images](dockerimages.md).
|
||||
|
||||
|
||||
Reference in New Issue
Block a user