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