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