github.com/chenchun/docker@v1.3.2-0.20150629222414-20467faf132b/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] or unix://path 87 88 For example: 89 90 - `tcp://host:2375` -> TCP connection on 91 host:2375 92 - `unix://path/to/socket` -> Unix socket located 93 at `path/to/socket` 94 95 `-H`, when empty, will default to the same value as 96 when no `-H` was passed in. 97 98 `-H` also accepts short form for TCP bindings: 99 100 host[:port] or :port 101 102 Run Docker in daemon mode: 103 104 $ sudo <path to>/docker -H 0.0.0.0:5555 -d & 105 106 Download an `ubuntu` image: 107 108 $ docker -H :5555 pull ubuntu 109 110 You can use multiple `-H`, for example, if you want to listen on both 111 TCP and a Unix socket 112 113 # Run docker in daemon mode 114 $ sudo <path to>/docker -H tcp://127.0.0.1:2375 -H unix:///var/run/docker.sock -d & 115 # Download an ubuntu image, use default Unix socket 116 $ docker pull ubuntu 117 # OR use the TCP port 118 $ docker -H tcp://127.0.0.1:2375 pull ubuntu 119 120 ## Starting a long-running worker process 121 122 # Start a very useful long-running process 123 $ JOB=$(docker run -d ubuntu /bin/sh -c "while true; do echo Hello world; sleep 1; done") 124 125 # Collect the output of the job so far 126 $ docker logs $JOB 127 128 # Kill the job 129 $ docker kill $JOB 130 131 ## Listing containers 132 133 $ docker ps # Lists only running containers 134 $ docker ps -a # Lists all containers 135 136 ## Controlling containers 137 138 # Start a new container 139 $ JOB=$(docker run -d ubuntu /bin/sh -c "while true; do echo Hello world; sleep 1; done") 140 141 # Stop the container 142 $ docker stop $JOB 143 144 # Start the container 145 $ docker start $JOB 146 147 # Restart the container 148 $ docker restart $JOB 149 150 # SIGKILL a container 151 $ docker kill $JOB 152 153 # Remove a container 154 $ docker stop $JOB # Container must be stopped to remove it 155 $ docker rm $JOB 156 157 ## Bind a service on a TCP port 158 159 # Bind port 4444 of this container, and tell netcat to listen on it 160 $ JOB=$(docker run -d -p 4444 ubuntu:12.10 /bin/nc -l 4444) 161 162 # Which public port is NATed to my container? 163 $ PORT=$(docker port $JOB 4444 | awk -F: '{ print $2 }') 164 165 # Connect to the public port 166 $ echo hello world | nc 127.0.0.1 $PORT 167 168 # Verify that the network connection worked 169 $ echo "Daemon received: $(docker logs $JOB)" 170 171 ## Committing (saving) a container state 172 173 Save your containers state to an image, so the state can be 174 re-used. 175 176 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. 177 178 # Commit your container to a new named image 179 $ docker commit <container> <some_name> 180 181 # List your images 182 $ docker images 183 184 You now have an image state from which you can create new instances. 185 186 Read more about [*Share Images via 187 Repositories*](/userguide/dockerrepos) or 188 continue to the complete [*Command 189 Line*](/reference/commandline/cli)