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