github.com/fcwu/docker@v1.4.2-0.20150115145920-2a69ca89f0df/docs/sources/userguide/dockerizing.md (about)

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