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