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

     1  <!--[metadata]>
     2  +++
     3  title = "Dockerizing applications: A 'Hello world'"
     4  description = "A simple 'Hello world' exercise that introduced you to Docker."
     5  keywords = ["docker guide, docker, docker platform, virtualization framework, how to, dockerize, dockerizing apps, dockerizing applications, container,  containers"]
     6  [menu.main]
     7  parent = "smn_applied"
     8  +++
     9  <![end-metadata]-->
    10  
    11  # Dockerizing applications: A "Hello world"
    12  
    13  *So what's this Docker thing all about?*
    14  
    15  Docker allows you to run applications inside containers. Running an
    16  application inside a container takes a single command: `docker run`.
    17  
    18  >**Note**: Depending on your Docker system configuration, you may be required to
    19  >preface each `docker` command on this page with `sudo`. To avoid this behavior,
    20  >your system administrator can create a Unix group called `docker` and add users
    21  >to it. 
    22  
    23  ## Hello world
    24  
    25  Let's try it now.
    26  
    27      $ docker run ubuntu:14.04 /bin/echo 'Hello world'
    28      Hello world
    29  
    30  And you just launched your first container!
    31  
    32  So what just happened? Let's step through what the `docker run` command
    33  did.
    34  
    35  First we specified the `docker` binary and the command we wanted to
    36  execute, `run`. The `docker run` combination *runs* containers.
    37  
    38  Next we specified an image: `ubuntu:14.04`. This is the source of the container
    39  we ran. Docker calls this an image. In this case we used an Ubuntu 14.04
    40  operating system image.
    41  
    42  When you specify an image, Docker looks first for the image on your
    43  Docker host. If it can't find it then it downloads the image from the public
    44  image registry: [Docker Hub](https://hub.docker.com).
    45  
    46  Next we told Docker what command to run inside our new container:
    47  
    48      /bin/echo 'Hello world'
    49  
    50  When our container was launched Docker created a new Ubuntu 14.04
    51  environment and then executed the `/bin/echo` command inside it. We saw
    52  the result on the command line:
    53  
    54      Hello world
    55  
    56  So what happened to our container after that? Well Docker containers
    57  only run as long as the command you specify is active. Here, as soon as
    58  `Hello world` was echoed, the container stopped.
    59  
    60  ## An interactive container
    61  
    62  Let's try the `docker run` command again, this time specifying a new
    63  command to run in our container.
    64  
    65      $ docker run -t -i ubuntu:14.04 /bin/bash
    66      root@af8bae53bdd3:/#
    67  
    68  Here we've again specified the `docker run` command and launched an
    69  `ubuntu:14.04` image. But we've also passed in two flags: `-t` and `-i`.
    70  The `-t` flag assigns a pseudo-tty or terminal inside our new container
    71  and the `-i` flag allows us to make an interactive connection by
    72  grabbing the standard in (`STDIN`) of the container.
    73  
    74  We've also specified a new command for our container to run:
    75  `/bin/bash`. This will launch a Bash shell inside our container.
    76  
    77  So now when our container is launched we can see that we've got a
    78  command prompt inside it:
    79  
    80      root@af8bae53bdd3:/#
    81  
    82  Let's try running some commands inside our container:
    83  
    84      root@af8bae53bdd3:/# pwd
    85      /
    86      root@af8bae53bdd3:/# ls
    87      bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
    88  
    89  You can see we've run the `pwd` to show our current directory and can
    90  see we're in the `/` root directory. We've also done a directory listing
    91  of the root directory which shows us what looks like a typical Linux
    92  file system.
    93  
    94  You can play around inside this container and when you're done you can
    95  use the `exit` command or enter Ctrl-D to finish.
    96  
    97      root@af8bae53bdd3:/# exit
    98  
    99  As with our previous container, once the Bash shell process has
   100  finished, the container is stopped.
   101  
   102  ## A daemonized Hello world
   103  
   104  Now a container that runs a command and then exits has some uses but
   105  it's not overly helpful. Let's create a container that runs as a daemon,
   106  like most of the applications we're probably going to run with Docker.
   107  
   108  Again we can do this with the `docker run` command:
   109  
   110      $ docker run -d ubuntu:14.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"
   111      1e5535038e285177d5214659a068137486f96ee5c2e85a4ac52dc83f2ebe4147
   112  
   113  Wait, what? Where's our "hello world" output? Let's look at what we've run here.
   114  It should look pretty familiar. We ran `docker run` but this time we
   115  specified a flag: `-d`. The `-d` flag tells Docker to run the container
   116  and put it in the background, to daemonize it.
   117  
   118  We also specified the same image: `ubuntu:14.04`.
   119  
   120  Finally, we specified a command to run:
   121  
   122      /bin/sh -c "while true; do echo hello world; sleep 1; done"
   123  
   124  This is the (hello) world's silliest daemon: a shell script that echoes
   125  `hello world` forever.
   126  
   127  So why aren't we seeing any `hello world`'s? Instead Docker has returned
   128  a really long string:
   129  
   130      1e5535038e285177d5214659a068137486f96ee5c2e85a4ac52dc83f2ebe4147
   131  
   132  This really long string is called a *container ID*. It uniquely
   133  identifies a container so we can work with it.
   134  
   135  > **Note:** 
   136  > The container ID is a bit long and unwieldy. A bit later,
   137  > we'll see a shorter ID and ways to name our containers to make
   138  > working with them easier.
   139  
   140  We can use this container ID to see what's happening with our `hello world` daemon.
   141  
   142  Firstly let's make sure our container is running. We can
   143  do that with the `docker ps` command. The `docker ps` command queries
   144  the Docker daemon for information about all the containers it knows
   145  about.
   146  
   147      $ docker ps
   148      CONTAINER ID  IMAGE         COMMAND               CREATED        STATUS       PORTS NAMES
   149      1e5535038e28  ubuntu:14.04  /bin/sh -c 'while tr  2 minutes ago  Up 1 minute        insane_babbage
   150  
   151  Here we can see our daemonized container. The `docker ps` has returned some useful
   152  information about it, starting with a shorter variant of its container ID:
   153  `1e5535038e28`.
   154  
   155  We can also see the image we used to build it, `ubuntu:14.04`, the command it
   156  is running, its status and an automatically assigned name,
   157  `insane_babbage`. 
   158  
   159  > **Note:** 
   160  > Docker automatically generates names for any containers started.
   161  > We'll see how to specify your own names a bit later.
   162  
   163  Okay, so we now know it's running. But is it doing what we asked it to do? To see this
   164  we're going to look inside the container using the `docker logs`
   165  command. Let's use the container name Docker assigned.
   166  
   167      $ docker logs insane_babbage
   168      hello world
   169      hello world
   170      hello world
   171      . . .
   172  
   173  The `docker logs` command looks inside the container and returns its standard
   174  output: in this case the output of our command `hello world`.
   175  
   176  Awesome! Our daemon is working and we've just created our first
   177  Dockerized application!
   178  
   179  Now we've established we can create our own containers let's tidy up
   180  after ourselves and stop our daemonized container. To do this we use the
   181  `docker stop` command.
   182  
   183      $ docker stop insane_babbage
   184      insane_babbage
   185  
   186  The `docker stop` command tells Docker to politely stop the running
   187  container. If it succeeds it will return the name of the container it
   188  has just stopped.
   189  
   190  Let's check it worked with the `docker ps` command.
   191  
   192      $ docker ps
   193      CONTAINER ID  IMAGE         COMMAND               CREATED        STATUS       PORTS NAMES
   194  
   195  Excellent. Our container has been stopped.
   196  
   197  # Next steps
   198  
   199  Now we've seen how simple it is to get started with Docker. Let's learn how to
   200  do some more advanced tasks.
   201  
   202  Go to [Working With Containers](usingdocker.md).
   203