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