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