github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/docs/userguide/basics.md (about)

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