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