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