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