github.com/a4a881d4/docker@v1.9.0-rc2/docs/userguide/usingdocker.md (about)

     1  <!--[metadata]>
     2  +++
     3  title = "Working with containers"
     4  description = "Learn how to manage and operate Docker containers."
     5  keywords = ["docker, the docker guide, documentation, docker.io, monitoring containers, docker top, docker inspect, docker port, ports, docker logs, log,  Logs"]
     6  [menu.main]
     7  parent="smn_containers"
     8  +++
     9  <![end-metadata]-->
    10  
    11  # Working with containers
    12  
    13  In the [last section of the Docker User Guide](dockerizing.md)
    14  we launched our first containers. We launched containers using the
    15  `docker run` command:
    16  
    17  * Interactive container runs in the foreground.
    18  * Daemonized container runs in the background.
    19  
    20  In the process we learned about several Docker commands:
    21  
    22  * `docker ps` - Lists containers.
    23  * `docker logs` - Shows us the standard output of a container.
    24  * `docker stop` - Stops running containers.
    25  
    26  > **Tip:**
    27  > Another way to learn about `docker` commands is our
    28  > [interactive tutorial](https://www.docker.com/tryit/).
    29  
    30  The `docker` client is pretty simple. Each action you can take
    31  with Docker is a command and each command can take a series of
    32  flags and arguments.
    33  
    34      # Usage:  [sudo] docker [command] [flags] [arguments] ..
    35      # Example:
    36      $ docker run -i -t ubuntu /bin/bash
    37  
    38  Let's see this in action by using the `docker version` command to return
    39  version information on the currently installed Docker client and daemon.
    40  
    41      $ docker version
    42  
    43  This command will not only provide you the version of Docker client and
    44  daemon you are using, but also the version of Go (the programming
    45  language powering Docker).
    46    
    47      Client:
    48        Version:      1.8.1
    49        API version:  1.20
    50        Go version:   go1.4.2
    51        Git commit:   d12ea79
    52        Built:        Thu Aug 13 02:35:49 UTC 2015
    53        OS/Arch:      linux/amd64
    54  
    55      Server:
    56        Version:      1.8.1
    57        API version:  1.20
    58        Go version:   go1.4.2
    59        Git commit:   d12ea79
    60        Built:        Thu Aug 13 02:35:49 UTC 2015
    61        OS/Arch:      linux/amd64
    62  
    63  ## Get Docker command help
    64  
    65  You can display the help for specific Docker commands. The help details the
    66  options and their usage. To see a list of all the possible commands, use the
    67  following:
    68  
    69      $ docker --help
    70  
    71  To see usage for a specific command, specify the command with the `--help` flag:
    72  
    73      $ docker attach --help
    74  
    75      Usage: docker attach [OPTIONS] CONTAINER
    76  
    77      Attach to a running container
    78  
    79        --help=false        Print usage
    80        --no-stdin=false    Do not attach stdin
    81        --sig-proxy=true    Proxy all received signals to the process
    82  
    83  > **Note:** 
    84  > For further details and examples of each command, see the
    85  > [command reference](../reference/commandline/cli.md) in this guide.
    86  
    87  ## Running a web application in Docker
    88  
    89  So now we've learnt a bit more about the `docker` client let's move onto
    90  the important stuff: running more containers. So far none of the
    91  containers we've run did anything particularly useful, so let's
    92  change that by running an example web application in Docker.
    93  
    94  For our web application we're going to run a Python Flask application.
    95  Let's start with a `docker run` command.
    96  
    97      $ docker run -d -P training/webapp python app.py
    98  
    99  Let's review what our command did. We've specified two flags: `-d` and
   100  `-P`. We've already seen the `-d` flag which tells Docker to run the
   101  container in the background. The `-P` flag is new and tells Docker to
   102  map any required network ports inside our container to our host. This
   103  lets us view our web application.
   104  
   105  We've specified an image: `training/webapp`. This image is a
   106  pre-built image we've created that contains a simple Python Flask web
   107  application.
   108  
   109  Lastly, we've specified a command for our container to run: `python app.py`. This launches our web application.
   110  
   111  > **Note:** 
   112  > You can see more detail on the `docker run` command in the [command
   113  > reference](../reference/commandline/run.md) and the [Docker Run
   114  > Reference](../reference/run.md).
   115  
   116  ## Viewing our web application container
   117  
   118  Now let's see our running container using the `docker ps` command.
   119  
   120      $ docker ps -l
   121      CONTAINER ID  IMAGE                   COMMAND       CREATED        STATUS        PORTS                    NAMES
   122      bc533791f3f5  training/webapp:latest  python app.py 5 seconds ago  Up 2 seconds  0.0.0.0:49155->5000/tcp  nostalgic_morse
   123  
   124  You can see we've specified a new flag, `-l`, for the `docker ps`
   125  command. This tells the `docker ps` command to return the details of the
   126  *last* container started.
   127  
   128  > **Note:** 
   129  > By default, the `docker ps` command only shows information about running
   130  > containers. If you want to see stopped containers too use the `-a` flag.
   131  
   132  We can see the same details we saw [when we first Dockerized a
   133  container](dockerizing.md) with one important addition in the `PORTS`
   134  column.
   135  
   136      PORTS
   137      0.0.0.0:49155->5000/tcp
   138  
   139  When we passed the `-P` flag to the `docker run` command Docker mapped any
   140  ports exposed in our image to our host.
   141  
   142  > **Note:** 
   143  > We'll learn more about how to expose ports in Docker images when
   144  > [we learn how to build images](dockerimages.md).
   145  
   146  In this case Docker has exposed port 5000 (the default Python Flask
   147  port) on port 49155.
   148  
   149  Network port bindings are very configurable in Docker. In our last example the
   150  `-P` flag is a shortcut for `-p 5000` that maps port 5000 inside the container
   151  to a high port (from *ephemeral port range* which typically ranges from 32768
   152  to 61000) on the local Docker host. We can also bind Docker containers to
   153  specific ports using the `-p` flag, for example:
   154  
   155      $ docker run -d -p 80:5000 training/webapp python app.py
   156  
   157  This would map port 5000 inside our container to port 80 on our local
   158  host. You might be asking about now: why wouldn't we just want to always
   159  use 1:1 port mappings in Docker containers rather than mapping to high
   160  ports? Well 1:1 mappings have the constraint of only being able to map
   161  one of each port on your local host. Let's say you want to test two
   162  Python applications: both bound to port 5000 inside their own containers.
   163  Without Docker's port mapping you could only access one at a time on the
   164  Docker host.
   165  
   166  So let's now browse to port 49155 in a web browser to
   167  see the application.
   168  
   169  ![Viewing the web application](webapp1.png).
   170  
   171  Our Python application is live!
   172  
   173  > **Note:**
   174  > If you have been using a virtual machine on OS X, Windows or Linux,
   175  > you'll need to get the IP of the virtual host instead of using localhost.
   176  > You can do this by running the `docker-machine ip your_vm_name` from your  command line or terminal application, for example:
   177  > 
   178  >     $ docker-machine ip my-docker-vm
   179  >     192.168.99.100
   180  > 
   181  > In this case you'd browse to `http://192.168.99.100:49155` for the above example.
   182  
   183  ## A network port shortcut
   184  
   185  Using the `docker ps` command to return the mapped port is a bit clumsy so
   186  Docker has a useful shortcut we can use: `docker port`. To use `docker port` we
   187  specify the ID or name of our container and then the port for which we need the
   188  corresponding public-facing port.
   189  
   190      $ docker port nostalgic_morse 5000
   191      0.0.0.0:49155
   192  
   193  In this case we've looked up what port is mapped externally to port 5000 inside
   194  the container.
   195  
   196  ## Viewing the web application's logs
   197  
   198  Let's also find out a bit more about what's happening with our application and
   199  use another of the commands we've learnt, `docker logs`.
   200  
   201      $ docker logs -f nostalgic_morse
   202      * Running on http://0.0.0.0:5000/
   203      10.0.2.2 - - [23/May/2014 20:16:31] "GET / HTTP/1.1" 200 -
   204      10.0.2.2 - - [23/May/2014 20:16:31] "GET /favicon.ico HTTP/1.1" 404 -
   205  
   206  This time though we've added a new flag, `-f`. This causes the `docker
   207  logs` command to act like the `tail -f` command and watch the
   208  container's standard out. We can see here the logs from Flask showing
   209  the application running on port 5000 and the access log entries for it.
   210  
   211  ## Looking at our web application container's processes
   212  
   213  In addition to the container's logs we can also examine the processes
   214  running inside it using the `docker top` command.
   215  
   216      $ docker top nostalgic_morse
   217      PID                 USER                COMMAND
   218      854                 root                python app.py
   219  
   220  Here we can see our `python app.py` command is the only process running inside
   221  the container.
   222  
   223  ## Inspecting our web application container
   224  
   225  Lastly, we can take a low-level dive into our Docker container using the
   226  `docker inspect` command. It returns a JSON document containing useful
   227  configuration and status information for the specified container.
   228  
   229      $ docker inspect nostalgic_morse
   230  
   231  Let's see a sample of that JSON output.
   232  
   233      [{
   234          "ID": "bc533791f3f500b280a9626688bc79e342e3ea0d528efe3a86a51ecb28ea20",
   235          "Created": "2014-05-26T05:52:40.808952951Z",
   236          "Path": "python",
   237          "Args": [
   238             "app.py"
   239          ],
   240          "Config": {
   241             "Hostname": "bc533791f3f5",
   242             "Domainname": "",
   243             "User": "",
   244      . . .
   245  
   246  We can also narrow down the information we want to return by requesting a
   247  specific element, for example to return the container's IP address we would:
   248  
   249      $ docker inspect -f '{{ .NetworkSettings.IPAddress }}' nostalgic_morse
   250      172.17.0.5
   251  
   252  ## Stopping our web application container
   253  
   254  Okay we've seen web application working. Now let's stop it using the
   255  `docker stop` command and the name of our container: `nostalgic_morse`.
   256  
   257      $ docker stop nostalgic_morse
   258      nostalgic_morse
   259  
   260  We can now use the `docker ps` command to check if the container has
   261  been stopped.
   262  
   263      $ docker ps -l
   264  
   265  ## Restarting our web application container
   266  
   267  Oops! Just after you stopped the container you get a call to say another
   268  developer needs the container back. From here you have two choices: you
   269  can create a new container or restart the old one. Let's look at
   270  starting our previous container back up.
   271  
   272      $ docker start nostalgic_morse
   273      nostalgic_morse
   274  
   275  Now quickly run `docker ps -l` again to see the running container is
   276  back up or browse to the container's URL to see if the application
   277  responds.
   278  
   279  > **Note:** 
   280  > Also available is the `docker restart` command that runs a stop and
   281  > then start on the container.
   282  
   283  ## Removing our web application container
   284  
   285  Your colleague has let you know that they've now finished with the container
   286  and won't need it again. So let's remove it using the `docker rm` command.
   287  
   288      $ docker rm nostalgic_morse
   289      Error: Impossible to remove a running container, please stop it first or use -f
   290      2014/05/24 08:12:56 Error: failed to remove one or more containers
   291  
   292  What happened? We can't actually remove a running container. This protects
   293  you from accidentally removing a running container you might need. Let's try
   294  this again by stopping the container first.
   295  
   296      $ docker stop nostalgic_morse
   297      nostalgic_morse
   298      $ docker rm nostalgic_morse
   299      nostalgic_morse
   300  
   301  And now our container is stopped and deleted.
   302  
   303  > **Note:**
   304  > Always remember that removing a container is final!
   305  
   306  # Next steps
   307  
   308  Until now we've only used images that we've downloaded from
   309  [Docker Hub](https://hub.docker.com). Next, let's get introduced to
   310  building and sharing our own images.
   311  
   312  Go to [Working with Docker Images](dockerimages.md).
   313