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