github.com/jogo/docker@v1.7.0-rc1/docs/sources/userguide/usingdocker.md (about) 1 page_title: Working with containers 2 page_description: Learn how to manage and operate Docker containers. 3 page_keywords: docker, the docker guide, documentation, docker.io, monitoring containers, docker top, docker inspect, docker port, ports, docker logs, log, Logs 4 5 # Working with containers 6 7 In the [last section of the Docker User Guide](/userguide/dockerizing) 8 we launched our first containers. We launched two containers using the 9 `docker run` command. 10 11 * Containers we ran interactively in the foreground. 12 * One container we ran daemonized in the background. 13 14 In the process we learned about several Docker commands: 15 16 * `docker ps` - Lists containers. 17 * `docker logs` - Shows us the standard output of a container. 18 * `docker stop` - Stops running containers. 19 20 > **Tip:** 21 > Another way to learn about `docker` commands is our 22 > [interactive tutorial](https://www.docker.com/tryit/). 23 24 The `docker` client is pretty simple. Each action you can take 25 with Docker is a command and each command can take a series of 26 flags and arguments. 27 28 # Usage: [sudo] docker [command] [flags] [arguments] .. 29 # Example: 30 $ docker run -i -t ubuntu /bin/bash 31 32 Let's see this in action by using the `docker version` command to return 33 version information on the currently installed Docker client and daemon. 34 35 $ docker version 36 37 This command will not only provide you the version of Docker client and 38 daemon you are using, but also the version of Go (the programming 39 language powering Docker). 40 41 Client version: 0.8.0 42 Go version (client): go1.2 43 44 Git commit (client): cc3a8c8 45 Server version: 0.8.0 46 47 Git commit (server): cc3a8c8 48 Go version (server): go1.2 49 50 Last stable version: 0.8.0 51 52 ## Get Docker command help 53 54 You can display the help for specific Docker commands. The help details the 55 options and their usage. To see a list of all the possible commands, use the 56 following: 57 58 $ docker --help 59 60 To see usage for a specific command, specify the command with the `--help` flag: 61 62 $ docker attach --help 63 64 Usage: docker attach [OPTIONS] CONTAINER 65 66 Attach to a running container 67 68 --help=false Print usage 69 --no-stdin=false Do not attach stdin 70 --sig-proxy=true Proxy all received signals to the process 71 72 > **Note:** 73 > For further details and examples of each command, see the 74 > [command reference](/reference/commandline/cli/) in this guide. 75 76 ## Running a web application in Docker 77 78 So now we've learnt a bit more about the `docker` client let's move onto 79 the important stuff: running more containers. So far none of the 80 containers we've run did anything particularly useful though. So let's 81 build on that experience by running an example web application in 82 Docker. 83 84 For our web application we're going to run a Python Flask application. 85 Let's start with a `docker run` command. 86 87 $ docker run -d -P training/webapp python app.py 88 89 Let's review what our command did. We've specified two flags: `-d` and 90 `-P`. We've already seen the `-d` flag which tells Docker to run the 91 container in the background. The `-P` flag is new and tells Docker to 92 map any required network ports inside our container to our host. This 93 lets us view our web application. 94 95 We've specified an image: `training/webapp`. This image is a 96 pre-built image we've created that contains a simple Python Flask web 97 application. 98 99 Lastly, we've specified a command for our container to run: `python app.py`. This launches our web application. 100 101 > **Note:** 102 > You can see more detail on the `docker run` command in the [command 103 > reference](/reference/commandline/cli/#run) and the [Docker Run 104 > Reference](/reference/run/). 105 106 ## Viewing our web application container 107 108 Now let's see our running container using the `docker ps` command. 109 110 $ docker ps -l 111 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 112 bc533791f3f5 training/webapp:latest python app.py 5 seconds ago Up 2 seconds 0.0.0.0:49155->5000/tcp nostalgic_morse 113 114 You can see we've specified a new flag, `-l`, for the `docker ps` 115 command. This tells the `docker ps` command to return the details of the 116 *last* container started. 117 118 > **Note:** 119 > By default, the `docker ps` command only shows information about running 120 > containers. If you want to see stopped containers too use the `-a` flag. 121 122 We can see the same details we saw [when we first Dockerized a 123 container](/userguide/dockerizing) with one important addition in the `PORTS` 124 column. 125 126 PORTS 127 0.0.0.0:49155->5000/tcp 128 129 When we passed the `-P` flag to the `docker run` command Docker mapped any 130 ports exposed in our image to our host. 131 132 > **Note:** 133 > We'll learn more about how to expose ports in Docker images when 134 > [we learn how to build images](/userguide/dockerimages). 135 136 In this case Docker has exposed port 5000 (the default Python Flask 137 port) on port 49155. 138 139 Network port bindings are very configurable in Docker. In our last example the 140 `-P` flag is a shortcut for `-p 5000` that maps port 5000 inside the container 141 to a high port (from *ephemeral port range* which typically ranges from 32768 142 to 61000) on the local Docker host. We can also bind Docker containers to 143 specific ports using the `-p` flag, for example: 144 145 $ docker run -d -p 80:5000 training/webapp python app.py 146 147 This would map port 5000 inside our container to port 80 on our local 148 host. You might be asking about now: why wouldn't we just want to always 149 use 1:1 port mappings in Docker containers rather than mapping to high 150 ports? Well 1:1 mappings have the constraint of only being able to map 151 one of each port on your local host. Let's say you want to test two 152 Python applications: both bound to port 5000 inside their own containers. 153 Without Docker's port mapping you could only access one at a time on the 154 Docker host. 155 156 So let's now browse to port 49155 in a web browser to 157 see the application. 158 159 ![Viewing the web application](/userguide/webapp1.png). 160 161 Our Python application is live! 162 163 > **Note:** 164 > If you have used the `boot2docker` virtual machine on OS X, Windows or Linux, 165 > you'll need to get the IP of the virtual host instead of using localhost. 166 > You can do this by running the following outside of the `boot2docker` shell 167 > (i.e., from your comment line or terminal application). 168 > 169 > $ boot2docker ip 170 > The VM's Host only interface IP address is: 192.168.59.103 171 > 172 > In this case you'd browse to http://192.168.59.103:49155 for the above example. 173 174 ## A network port shortcut 175 176 Using the `docker ps` command to return the mapped port is a bit clumsy so 177 Docker has a useful shortcut we can use: `docker port`. To use `docker port` we 178 specify the ID or name of our container and then the port for which we need the 179 corresponding public-facing port. 180 181 $ docker port nostalgic_morse 5000 182 0.0.0.0:49155 183 184 In this case we've looked up what port is mapped externally to port 5000 inside 185 the container. 186 187 ## Viewing the web application's logs 188 189 Let's also find out a bit more about what's happening with our application and 190 use another of the commands we've learnt, `docker logs`. 191 192 $ docker logs -f nostalgic_morse 193 * Running on http://0.0.0.0:5000/ 194 10.0.2.2 - - [23/May/2014 20:16:31] "GET / HTTP/1.1" 200 - 195 10.0.2.2 - - [23/May/2014 20:16:31] "GET /favicon.ico HTTP/1.1" 404 - 196 197 This time though we've added a new flag, `-f`. This causes the `docker 198 logs` command to act like the `tail -f` command and watch the 199 container's standard out. We can see here the logs from Flask showing 200 the application running on port 5000 and the access log entries for it. 201 202 ## Looking at our web application container's processes 203 204 In addition to the container's logs we can also examine the processes 205 running inside it using the `docker top` command. 206 207 $ docker top nostalgic_morse 208 PID USER COMMAND 209 854 root python app.py 210 211 Here we can see our `python app.py` command is the only process running inside 212 the container. 213 214 ## Inspecting our web application container 215 216 Lastly, we can take a low-level dive into our Docker container using the 217 `docker inspect` command. It returns a JSON hash of useful configuration 218 and status information about Docker containers. 219 220 $ docker inspect nostalgic_morse 221 222 Let's see a sample of that JSON output. 223 224 [{ 225 "ID": "bc533791f3f500b280a9626688bc79e342e3ea0d528efe3a86a51ecb28ea20", 226 "Created": "2014-05-26T05:52:40.808952951Z", 227 "Path": "python", 228 "Args": [ 229 "app.py" 230 ], 231 "Config": { 232 "Hostname": "bc533791f3f5", 233 "Domainname": "", 234 "User": "", 235 . . . 236 237 We can also narrow down the information we want to return by requesting a 238 specific element, for example to return the container's IP address we would: 239 240 $ docker inspect -f '{{ .NetworkSettings.IPAddress }}' nostalgic_morse 241 172.17.0.5 242 243 ## Stopping our web application container 244 245 Okay we've seen web application working. Now let's stop it using the 246 `docker stop` command and the name of our container: `nostalgic_morse`. 247 248 $ docker stop nostalgic_morse 249 nostalgic_morse 250 251 We can now use the `docker ps` command to check if the container has 252 been stopped. 253 254 $ docker ps -l 255 256 ## Restarting our web application container 257 258 Oops! Just after you stopped the container you get a call to say another 259 developer needs the container back. From here you have two choices: you 260 can create a new container or restart the old one. Let's look at 261 starting our previous container back up. 262 263 $ docker start nostalgic_morse 264 nostalgic_morse 265 266 Now quickly run `docker ps -l` again to see the running container is 267 back up or browse to the container's URL to see if the application 268 responds. 269 270 > **Note:** 271 > Also available is the `docker restart` command that runs a stop and 272 > then start on the container. 273 274 ## Removing our web application container 275 276 Your colleague has let you know that they've now finished with the container 277 and won't need it again. So let's remove it using the `docker rm` command. 278 279 $ docker rm nostalgic_morse 280 Error: Impossible to remove a running container, please stop it first or use -f 281 2014/05/24 08:12:56 Error: failed to remove one or more containers 282 283 What happened? We can't actually remove a running container. This protects 284 you from accidentally removing a running container you might need. Let's try 285 this again by stopping the container first. 286 287 $ docker stop nostalgic_morse 288 nostalgic_morse 289 $ docker rm nostalgic_morse 290 nostalgic_morse 291 292 And now our container is stopped and deleted. 293 294 > **Note:** 295 > Always remember that deleting a container is final! 296 297 # Next steps 298 299 Until now we've only used images that we've downloaded from 300 [Docker Hub](https://hub.docker.com) now let's get introduced to 301 building and sharing our own images. 302 303 Go to [Working with Docker Images](/userguide/dockerimages). 304