github.com/slene/docker@v1.8.0-rc1/docs/articles/basics.md (about) 1 <!--[metadata]> 2 +++ 3 title = "Get started with containers" 4 description = "Common usage and commands" 5 keywords = ["Examples, Usage, basic commands, docker, documentation, examples"] 6 [menu.main] 7 parent = "smn_containers" 8 +++ 9 <![end-metadata]--> 10 11 # Get started with containers 12 13 This guide assumes you have a working installation of Docker. To verify Docker is 14 installed, use the following command: 15 16 # Check that you have a working install 17 $ docker info 18 19 If you get `docker: command not found` or something like 20 `/var/lib/docker/repositories: permission denied` you may have an 21 incomplete Docker installation or insufficient privileges to access 22 Docker on your machine. Please 23 24 Additionally, depending on your Docker system configuration, you may be required 25 to preface each `docker` command with `sudo`. To avoid having to use `sudo` with 26 the `docker` command, your system administrator can create a Unix group called 27 `docker` and add users to it. 28 29 For more information about installing Docker or `sudo` configuration, refer to 30 the [installation](/installation) instructions for your operating system. 31 32 33 ## Download a pre-built image 34 35 # Download an ubuntu image 36 $ docker pull ubuntu 37 38 This will find the `ubuntu` image by name on 39 [*Docker Hub*](/userguide/dockerrepos/#searching-for-images) 40 and download it from [Docker Hub](https://hub.docker.com) to a local 41 image cache. 42 43 > **Note**: 44 > When the image is successfully downloaded, you see a 12 character 45 > hash `539c0211cd76: Download complete` which is the 46 > short form of the image ID. These short image IDs are the first 12 47 > characters of the full image ID - which can be found using 48 > `docker inspect` or `docker images --no-trunc=true`. 49 50 > **Note:** if you are using a remote Docker daemon, such as Boot2Docker, 51 > then _do not_ type the `sudo` before the `docker` commands shown in the 52 > documentation's examples. 53 54 ## Running an interactive shell 55 56 To run an interactive shell in the Ubuntu image: 57 58 $ docker run -i -t ubuntu /bin/bash 59 60 The `-i` flag starts an interactive container. The `-t` flag creates a pseudo-TTY that attaches `stdin` and `stdout`. 61 62 To detach the `tty` without exiting the shell, use the escape sequence `Ctrl-p` + `Ctrl-q`. The container will continue to exist in a stopped state once exited. To list all containers, stopped and running use the `docker ps -a` command. 63 64 ## Bind Docker to another host/port or a Unix socket 65 66 > **Warning**: 67 > Changing the default `docker` daemon binding to a 68 > TCP port or Unix *docker* user group will increase your security risks 69 > by allowing non-root users to gain *root* access on the host. Make sure 70 > you control access to `docker`. If you are binding 71 > to a TCP port, anyone with access to that port has full Docker access; 72 > so it is not advisable on an open network. 73 74 With `-H` it is possible to make the Docker daemon to listen on a 75 specific IP and port. By default, it will listen on 76 `unix:///var/run/docker.sock` to allow only local connections by the 77 *root* user. You *could* set it to `0.0.0.0:2375` or a specific host IP 78 to give access to everybody, but that is **not recommended** because 79 then it is trivial for someone to gain root access to the host where the 80 daemon is running. 81 82 Similarly, the Docker client can use `-H` to connect to a custom port. 83 84 `-H` accepts host and port assignment in the following format: 85 86 tcp://[host][:port][path] or unix://path 87 88 For example: 89 90 - `tcp://host:2375` -> TCP connection on 91 host:2375 92 - `tcp://host:2375/path` -> TCP connection on 93 host:2375 and prepend path to all requests 94 - `unix://path/to/socket` -> Unix socket located 95 at `path/to/socket` 96 97 `-H`, when empty, will default to the same value as 98 when no `-H` was passed in. 99 100 `-H` also accepts short form for TCP bindings: 101 102 host[:port] or :port 103 104 Run Docker in daemon mode: 105 106 $ sudo <path to>/docker daemon -H 0.0.0.0:5555 & 107 108 Download an `ubuntu` image: 109 110 $ docker -H :5555 pull ubuntu 111 112 You can use multiple `-H`, for example, if you want to listen on both 113 TCP and a Unix socket 114 115 # Run docker in daemon mode 116 $ sudo <path to>/docker daemon -H tcp://127.0.0.1:2375 -H unix:///var/run/docker.sock & 117 # Download an ubuntu image, use default Unix socket 118 $ docker pull ubuntu 119 # OR use the TCP port 120 $ docker -H tcp://127.0.0.1:2375 pull ubuntu 121 122 ## Starting a long-running worker process 123 124 # Start a very useful long-running process 125 $ JOB=$(docker run -d ubuntu /bin/sh -c "while true; do echo Hello world; sleep 1; done") 126 127 # Collect the output of the job so far 128 $ docker logs $JOB 129 130 # Kill the job 131 $ docker kill $JOB 132 133 ## Listing containers 134 135 $ docker ps # Lists only running containers 136 $ docker ps -a # Lists all containers 137 138 ## Controlling containers 139 140 # Start a new container 141 $ JOB=$(docker run -d ubuntu /bin/sh -c "while true; do echo Hello world; sleep 1; done") 142 143 # Stop the container 144 $ docker stop $JOB 145 146 # Start the container 147 $ docker start $JOB 148 149 # Restart the container 150 $ docker restart $JOB 151 152 # SIGKILL a container 153 $ docker kill $JOB 154 155 # Remove a container 156 $ docker stop $JOB # Container must be stopped to remove it 157 $ docker rm $JOB 158 159 ## Bind a service on a TCP port 160 161 # Bind port 4444 of this container, and tell netcat to listen on it 162 $ JOB=$(docker run -d -p 4444 ubuntu:12.10 /bin/nc -l 4444) 163 164 # Which public port is NATed to my container? 165 $ PORT=$(docker port $JOB 4444 | awk -F: '{ print $2 }') 166 167 # Connect to the public port 168 $ echo hello world | nc 127.0.0.1 $PORT 169 170 # Verify that the network connection worked 171 $ echo "Daemon received: $(docker logs $JOB)" 172 173 ## Committing (saving) a container state 174 175 Save your containers state to an image, so the state can be 176 re-used. 177 178 When you commit your container, Docker only stores the diff (difference) between the source image and the current state of the container's image. To list images you already have, use the `docker images` command. 179 180 # Commit your container to a new named image 181 $ docker commit <container> <some_name> 182 183 # List your images 184 $ docker images 185 186 You now have an image state from which you can create new instances. 187 188 Read more about [*Share Images via 189 Repositories*](/userguide/dockerrepos) or 190 continue to the complete [*Command 191 Line*](/reference/commandline/cli)