美文网首页
Docker的安装以及常用的一些命令

Docker的安装以及常用的一些命令

作者: DominicChen | 来源:发表于2019-04-15 23:57 被阅读0次

    这里以CentOS为例,如果在ubuntu上有不一样的地方,在后面咱在列出来。

    安装Docker

    • Install required packages. yum-utils provides the yum-config-manager utility, and device-mapper-persistent-data and lvm2 are required by the devicemapper storage driver.(这段话是照抄官网的)
    $ sudo yum install -y yum-utils device-mapper-persistent-data  lvm2
    
    • Set up the stable repository
    $ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    Loaded plugins: fastestmirror
    adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
    grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
    repo saved to /etc/yum.repos.d/docker-ce.repo
    $ sudo yum install -y docker-ce
    $ docker --version
    Docker version 18.09.5, build e8ff056
    $ sudo systemctl enable docker && sudo systemctl restart docker
    [sudo] password for ops: 
    Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
    

    上面第一个命令是添加repo. 第二个就是安装. 第三个是试一下是否安装成功. 第四个是将docker添加到service.下面来测试一下docker是不是真的work:

    $ docker run hello-world
    Unable to find image 'hello-world:latest' locally
    latest: Pulling from library/hello-world
    1b930d010525: Pull complete 
    Digest: sha256:92695bc579f31df7a63da6922075d0666e565ceccad16b59c3374d2cf4e8e50e
    Status: Downloaded newer image for hello-world:latest
    
    Hello from Docker!
    This message shows that your installation appears to be working correctly.
    
    To generate this message, Docker took the following steps:
     1. The Docker client contacted the Docker daemon.
     2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
        (amd64)
     3. The Docker daemon created a new container from that image which runs the
        executable that produces the output you are currently reading.
     4. The Docker daemon streamed that output to the Docker client, which sent it
        to your terminal.
    
    To try something more ambitious, you can run an Ubuntu container with:
     $ docker run -it ubuntu bash
    
    Share images, automate workflows, and more with a free Docker ID:
     https://hub.docker.com/
    
    For more examples and ideas, visit:
     https://docs.docker.com/get-started/
    

    Cool. 我们已经成功地把Docker安装在我们机器上了. 接下来听再做点其它的:
    首先呢, 大部分docker命令都是需要sudo权限的, 每次都要敲密码是不是很烦呢. 幸好, 在Docker安装好之后就有个group叫docker已经准备就绪. 我们只要把我们的用户名加到这个组就好了. Let's do it:

    $ sudo gpasswd -a $USER docker
    Adding user ops to group docker
    

    这个命令执行后需要重新登录一下shell.
    小结: 命令列表

    sudo sudo yum install -y yum-utils device-mapper-persistent-data  lvm2
    sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    sudo yum install -y docker-ce
    sudo gpasswd -a $USER docker
    sudo systemctl enable docker
    sudo systemctl restart docker
    

    测试

    接下来我们来敲几下命令玩一下:

    $ docker version
    Client:
     Version:           18.09.5
     API version:       1.39
     Go version:        go1.10.8
     Git commit:        e8ff056
     Built:             Thu Apr 11 04:43:34 2019
     OS/Arch:           linux/amd64
     Experimental:      false
    
    Server: Docker Engine - Community
     Engine:
      Version:          18.09.5
      API version:      1.39 (minimum version 1.12)
      Go version:       go1.10.8
      Git commit:       e8ff056
      Built:            Thu Apr 11 04:13:40 2019
      OS/Arch:          linux/amd64
      Experimental:     false
    
    

    运行下面这个命令, 你会得到docker命令的参数的完整列表

    $ docker help
    
    Usage:  docker [OPTIONS] COMMAND
    
    A self-sufficient runtime for containers
    
    Options:
          --config string      Location of client config files (default "/home/ops/.docker")
      -D, --debug              Enable debug mode
      -H, --host list          Daemon socket(s) to connect to
      -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
          --tls                Use TLS; implied by --tlsverify
          --tlscacert string   Trust certs signed only by this CA (default "/home/ops/.docker/ca.pem")
          --tlscert string     Path to TLS certificate file (default "/home/ops/.docker/cert.pem")
          --tlskey string      Path to TLS key file (default "/home/ops/.docker/key.pem")
          --tlsverify          Use TLS and verify the remote
      -v, --version            Print version information and quit
    
    Management Commands:
      builder     Manage builds
      config      Manage Docker configs
      container   Manage containers
      engine      Manage the docker engine
      image       Manage images
      network     Manage networks
      node        Manage Swarm nodes
      plugin      Manage plugins
      secret      Manage Docker secrets
      service     Manage services
      stack       Manage Docker stacks
      swarm       Manage Swarm
      system      Manage Docker
      trust       Manage trust on Docker images
      volume      Manage volumes
    
    Commands:
      attach      Attach local standard input, output, and error streams to a running container
      build       Build an image from a Dockerfile
      commit      Create a new image from a container's changes
      cp          Copy files/folders between a container and the local filesystem
      create      Create a new container
      diff        Inspect changes to files or directories on a container's filesystem
      events      Get real time events from the server
      exec        Run a command in a running container
      export      Export a container's filesystem as a tar archive
      history     Show the history of an image
      images      List images
      import      Import the contents from a tarball to create a filesystem image
      info        Display system-wide information
      inspect     Return low-level information on Docker objects
      kill        Kill one or more running containers
      load        Load an image from a tar archive or STDIN
      login       Log in to a Docker registry
      logout      Log out from a Docker registry
      logs        Fetch the logs of a container
      pause       Pause all processes within one or more containers
      port        List port mappings or a specific mapping for the container
      ps          List containers
      pull        Pull an image or a repository from a registry
      push        Push an image or a repository to a registry
      rename      Rename a container
      restart     Restart one or more containers
      rm          Remove one or more containers
      rmi         Remove one or more images
      run         Run a command in a new container
      save        Save one or more images to a tar archive (streamed to STDOUT by default)
      search      Search the Docker Hub for images
      start       Start one or more stopped containers
      stats       Display a live stream of container(s) resource usage statistics
      stop        Stop one or more running containers
      tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
      top         Display the running processes of a container
      unpause     Unpause all processes within one or more containers
      update      Update configuration of one or more containers
      version     Show the Docker version information
      wait        Block until one or more containers stop, then print their exit codes
    
    Run 'docker COMMAND --help' for more information on a command.
    

    如果想知道build这个命令怎么用, 就敲下面这个命令:

    $ docker build --help
    
    Usage:  docker build [OPTIONS] PATH | URL | -
    
    Build an image from a Dockerfile
    
    Options:
          --add-host list           Add a custom host-to-IP mapping (host:ip)
          --build-arg list          Set build-time variables
          --cache-from strings      Images to consider as cache sources
          --cgroup-parent string    Optional parent cgroup for the container
          --compress                Compress the build context using gzip
          --cpu-period int          Limit the CPU CFS (Completely Fair Scheduler) period
          --cpu-quota int           Limit the CPU CFS (Completely Fair Scheduler) quota
      -c, --cpu-shares int          CPU shares (relative weight)
          --cpuset-cpus string      CPUs in which to allow execution (0-3, 0,1)
          --cpuset-mems string      MEMs in which to allow execution (0-3, 0,1)
          --disable-content-trust   Skip image verification (default true)
      -f, --file string             Name of the Dockerfile (Default is 'PATH/Dockerfile')
          --force-rm                Always remove intermediate containers
          --iidfile string          Write the image ID to the file
          --isolation string        Container isolation technology
          --label list              Set metadata for an image
      -m, --memory bytes            Memory limit
          --memory-swap bytes       Swap limit equal to memory plus swap: '-1' to enable unlimited swap
          --network string          Set the networking mode for the RUN instructions during build (default "default")
          --no-cache                Do not use cache when building the image
          --pull                    Always attempt to pull a newer version of the image
      -q, --quiet                   Suppress the build output and print image ID on success
          --rm                      Remove intermediate containers after a successful build (default true)
          --security-opt strings    Security options
          --shm-size bytes          Size of /dev/shm
      -t, --tag list                Name and optionally a tag in the 'name:tag' format
          --target string           Set the target build stage to build.
          --ulimit ulimit           Ulimit options (default [])
    

    所有命令, 如果不知道怎么用, 都可以这样来.

    接下来来点实在的, 安装个nginx

    $ docker run --name docker-nginx -p 8080:80 nginx
    Unable to find image 'nginx:latest' locally
    latest: Pulling from library/nginx
    27833a3ba0a5: Pull complete 
    eb51733b5bc0: Pull complete 
    994d4a01fbe9: Pull complete 
    Digest: sha256:50174b19828157e94f8273e3991026dc7854ec7dd2bbb33e7d3bd91f0a4b333d
    Status: Downloaded newer image for nginx:latest
    

    现在试一下:

    $ wget http://localhost:8080
    --2019-04-13 16:32:54--  http://localhost:8080/
    Resolving localhost (localhost)... ::1, 127.0.0.1
    Connecting to localhost (localhost)|::1|:8080... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 612 [text/html]
    Saving to: ‘index.html’
    
    100%[=============================================================================>] 612         --.-K/s   in 0s      
    
    2019-04-13 16:32:54 (96.8 MB/s) - ‘index.html’ saved [612/612]
    
    $ cat index.html 
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
            width: 35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    
    

    常用命令

    列出当前的images

    $ docker image ls
    # OR
    $ docker images
    
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              bb776ce48575        2 days ago          109MB
    hello-world         latest              fce289e99eb9        3 months ago        1.84kB
    

    这就是我们刚才pull下来的两个images.
    未完待续

    相关文章

      网友评论

          本文标题:Docker的安装以及常用的一些命令

          本文链接:https://www.haomeiwen.com/subject/hsimwqtx.html