github.com/circular-dark/docker@v1.7.0/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 check 14 your Docker install, run 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 has successfully downloaded, you will 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 # Run an interactive shell in the ubuntu image, 57 # allocate a tty, attach stdin and stdout 58 # To detach the tty without exiting the shell, 59 # use the escape sequence Ctrl-p + Ctrl-q 60 # note: This will continue to exist in a stopped state once exited (see "docker ps -a") 61 $ docker run -i -t ubuntu /bin/bash 62 63 ## Bind Docker to another host/port or a Unix socket 64 65 > **Warning**: 66 > Changing the default `docker` daemon binding to a 67 > TCP port or Unix *docker* user group will increase your security risks 68 > by allowing non-root users to gain *root* access on the host. Make sure 69 > you control access to `docker`. If you are binding 70 > to a TCP port, anyone with access to that port has full Docker access; 71 > so it is not advisable on an open network. 72 73 With `-H` it is possible to make the Docker daemon to listen on a 74 specific IP and port. By default, it will listen on 75 `unix:///var/run/docker.sock` to allow only local connections by the 76 *root* user. You *could* set it to `0.0.0.0:2375` or a specific host IP 77 to give access to everybody, but that is **not recommended** because 78 then it is trivial for someone to gain root access to the host where the 79 daemon is running. 80 81 Similarly, the Docker client can use `-H` to connect to a custom port. 82 83 `-H` accepts host and port assignment in the following format: 84 85 tcp://[host][:port]` or `unix://path 86 87 For example: 88 89 - `tcp://host:2375` -> TCP connection on 90 host:2375 91 - `unix://path/to/socket` -> Unix socket located 92 at `path/to/socket` 93 94 `-H`, when empty, will default to the same value as 95 when no `-H` was passed in. 96 97 `-H` also accepts short form for TCP bindings: 98 99 host[:port]` or `:port 100 101 Run Docker in daemon mode: 102 103 $ sudo <path to>/docker -H 0.0.0.0:5555 -d & 104 105 Download an `ubuntu` image: 106 107 $ docker -H :5555 pull ubuntu 108 109 You can use multiple `-H`, for example, if you want to listen on both 110 TCP and a Unix socket 111 112 # Run docker in daemon mode 113 $ sudo <path to>/docker -H tcp://127.0.0.1:2375 -H unix:///var/run/docker.sock -d & 114 # Download an ubuntu image, use default Unix socket 115 $ docker pull ubuntu 116 # OR use the TCP port 117 $ docker -H tcp://127.0.0.1:2375 pull ubuntu 118 119 ## Starting a long-running worker process 120 121 # Start a very useful long-running process 122 $ JOB=$(docker run -d ubuntu /bin/sh -c "while true; do echo Hello world; sleep 1; done") 123 124 # Collect the output of the job so far 125 $ docker logs $JOB 126 127 # Kill the job 128 $ docker kill $JOB 129 130 ## Listing containers 131 132 $ docker ps # Lists only running containers 133 $ docker ps -a # Lists all containers 134 135 ## Controlling containers 136 137 # Start a new container 138 $ JOB=$(docker run -d ubuntu /bin/sh -c "while true; do echo Hello world; sleep 1; done") 139 140 # Stop the container 141 $ docker stop $JOB 142 143 # Start the container 144 $ docker start $JOB 145 146 # Restart the container 147 $ docker restart $JOB 148 149 # SIGKILL a container 150 $ docker kill $JOB 151 152 # Remove a container 153 $ docker stop $JOB # Container must be stopped to remove it 154 $ docker rm $JOB 155 156 ## Bind a service on a TCP port 157 158 # Bind port 4444 of this container, and tell netcat to listen on it 159 $ JOB=$(docker run -d -p 4444 ubuntu:12.10 /bin/nc -l 4444) 160 161 # Which public port is NATed to my container? 162 $ PORT=$(docker port $JOB 4444 | awk -F: '{ print $2 }') 163 164 # Connect to the public port 165 $ echo hello world | nc 127.0.0.1 $PORT 166 167 # Verify that the network connection worked 168 $ echo "Daemon received: $(docker logs $JOB)" 169 170 ## Committing (saving) a container state 171 172 Save your containers state to an image, so the state can be 173 re-used. 174 175 When you commit your container only the differences between the image 176 the container was created from and the current state of the container 177 will be stored (as a diff). See which images you already have using the 178 `docker images` command. 179 180 # Commit your container to a new named image 181 $ docker commit <container_id> <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)