github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/docs/userguide/containers/dockerizing.md (about)

     1  <!--[metadata]>
     2  +++
     3  aliases = ["/engine/userguide/dockerizing/"]
     4  title = "Hello world in a container"
     5  description = "A simple 'Hello world' exercise that introduced you to Docker."
     6  keywords = ["docker guide, docker, docker platform, how to, dockerize, dockerizing apps, dockerizing applications, container,  containers"]
     7  [menu.main]
     8  parent="engine_learn"
     9  weight=-6
    10  +++
    11  <![end-metadata]-->
    12  
    13  # Hello world in a container
    14  
    15  *So what's this Docker thing all about?*
    16  
    17  Docker allows you to run applications, worlds you create, inside containers.
    18  Running an application inside a container takes a single command: `docker run`.
    19  
    20  >**Note**: Depending on your Docker system configuration, you may be required to
    21  >preface each `docker` command on this page with `sudo`. To avoid this behavior,
    22  >your system administrator can create a Unix group called `docker` and add users
    23  >to it.
    24  
    25  ## Run a Hello world
    26  
    27  Let's run a hello world container.
    28  
    29      $ docker run ubuntu /bin/echo 'Hello world'
    30      Hello world
    31  
    32  You just launched your first container!
    33  
    34  In this example:
    35  
    36  * `docker run` runs a container.
    37  
    38  * `ubuntu` is the image you run, for example the Ubuntu operating system image.
    39    When you specify an image, Docker looks first for the image on your
    40    Docker host. If the image does not exist locally, then the image is pulled from the public
    41    image registry [Docker Hub](https://hub.docker.com).
    42  
    43  * `/bin/echo` is the command to run inside the new container.
    44  
    45  The container launches. Docker creates a new Ubuntu
    46  environment and executes the `/bin/echo` command inside it and then prints out:
    47  
    48      Hello world
    49  
    50  So what happened to the container after that? Well, Docker containers
    51  only run as long as the command you specify is active. Therefore, in the above example,
    52  the container stops once the command is executed.
    53  
    54  ## Run an interactive container
    55  
    56  Let's specify a new command to run in the container.
    57  
    58      $ docker run -t -i ubuntu /bin/bash
    59      root@af8bae53bdd3:/#
    60  
    61  In this example:
    62  
    63  * `docker run` runs a container.
    64  * `ubuntu` is the image you would like to run.
    65  * `-t` flag assigns a pseudo-tty or terminal inside the new container.
    66  * `-i` flag allows you to make an interactive connection by
    67  grabbing the standard in (`STDIN`) of the container.
    68  * `/bin/bash` launches a Bash shell inside our container.
    69  
    70  The container launches. We can see there is a
    71  command prompt inside it:
    72  
    73      root@af8bae53bdd3:/#
    74  
    75  Let's try running some commands inside the container:
    76  
    77      root@af8bae53bdd3:/# pwd
    78      /
    79      root@af8bae53bdd3:/# ls
    80      bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
    81  
    82  In this example:
    83  
    84  * `pwd` displays the current directory, the `/` root directory.  
    85  * `ls` displays the directory listing of the root directory of a typical Linux file system.
    86  
    87  Now, you can play around inside this container. When completed, run the `exit` command or enter Ctrl-D
    88  to exit the interactive shell.
    89  
    90      root@af8bae53bdd3:/# exit
    91  
    92  >**Note:** As with our previous container, once the Bash shell process has
    93  finished, the container stops.
    94  
    95  ## Start a daemonized Hello world
    96  
    97  Let's create a container that runs as a daemon.
    98  
    99      $ docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"
   100      1e5535038e285177d5214659a068137486f96ee5c2e85a4ac52dc83f2ebe4147
   101  
   102  In this example:
   103  
   104  * `docker run` runs the container.
   105  * `-d` flag runs the container in the background (to daemonize it).
   106  * `ubuntu` is the image you would like to run.
   107  
   108  Finally, we specify a command to run:
   109  
   110      /bin/sh -c "while true; do echo hello world; sleep 1; done"
   111  
   112  
   113  In the output, we do not see `hello world` but a long string:
   114  
   115      1e5535038e285177d5214659a068137486f96ee5c2e85a4ac52dc83f2ebe4147
   116  
   117  This long string is called a *container ID*. It uniquely
   118  identifies a container so we can work with it.
   119  
   120  > **Note:**
   121  > The container ID is a bit long and unwieldy. Later, we will cover the short
   122  > ID and ways to name our containers to make
   123  > working with them easier.
   124  
   125  We can use this container ID to see what's happening with our `hello world` daemon.
   126  
   127  First, let's make sure our container is running. Run the `docker ps` command.
   128  The `docker ps` command queries the Docker daemon for information about all the containers it knows
   129  about.
   130  
   131      $ docker ps
   132      CONTAINER ID  IMAGE         COMMAND               CREATED        STATUS       PORTS NAMES
   133      1e5535038e28  ubuntu  /bin/sh -c 'while tr  2 minutes ago  Up 1 minute        insane_babbage
   134  
   135  In this example, we can see our daemonized container. The `docker ps` returns some useful
   136  information:
   137  
   138  * `1e5535038e28` is the shorter variant of the container ID.
   139  * `ubuntu` is the used image.
   140  * the command, status, and assigned name `insane_babbage`.
   141  
   142  
   143  > **Note:**
   144  > Docker automatically generates names for any containers started.
   145  > We'll see how to specify your own names a bit later.
   146  
   147  Now, we know the container is running. But is it doing what we asked it to do? To
   148  see this we're going to look inside the container using the `docker logs`
   149  command.
   150  
   151  Let's use the container name `insane_babbage`.
   152  
   153      $ docker logs insane_babbage
   154      hello world
   155      hello world
   156      hello world
   157      . . .
   158  
   159  In this example:
   160  
   161  * `docker logs` looks inside the container and returns `hello world`.
   162  
   163  Awesome! The daemon is working and you have just created your first
   164  Dockerized application!
   165  
   166  Next, run the `docker stop` command to stop our detached container.
   167  
   168      $ docker stop insane_babbage
   169      insane_babbage
   170  
   171  The `docker stop` command tells Docker to politely stop the running
   172  container and returns the name of the container it stopped.
   173  
   174  Let's check it worked with the `docker ps` command.
   175  
   176      $ docker ps
   177      CONTAINER ID  IMAGE         COMMAND               CREATED        STATUS       PORTS NAMES
   178  
   179  Excellent. Our container is stopped.
   180  
   181  # Next steps
   182  
   183  So far, you launched your first containers using the `docker run` command. You
   184  ran an *interactive container* that ran in the foreground. You also ran a
   185  *detached container* that ran in the background. In the process you learned
   186  about several Docker commands:
   187  
   188  * `docker ps` - Lists containers.
   189  * `docker logs` - Shows us the standard output of a container.
   190  * `docker stop` - Stops running containers.
   191  
   192  Now, you have the basis learn more about Docker and how to do some more advanced
   193  tasks. Go to ["*Run a simple application*"](usingdocker.md) to actually build a
   194  web application with the Docker client.