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