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