Docker教程通俗理解1 -- Docker理解与配置

1. Docker是什么

    Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源。Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),容器性能开销极低。

Docker教程通俗理解1 -- Docker理解与配置_第1张图片

    特点:像虚拟机但是更加依赖host machine,比之开销更低,移植性更好

Docker教程通俗理解1 -- Docker理解与配置_第2张图片

    用Docker的logo来通俗解释:鲸鱼(或者是货轮)是操作系统,应用程序看作是货物,原本要将各种各样形状、尺寸不同的货物放到大鲸鱼上,你得为每件货物考虑怎么安放(就是应用程序配套的环境),还得考虑货物和货物是否能叠起来(应用程序依赖的环境是否会冲突)。现在使用了集装箱(容器)把每件货物都放到集装箱里,这样大鲸鱼可以用同样地方式安放、堆叠集装了,省事省力。

2. Docker的Ubuntu配置

    一个很好的教程 https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-16-04

Docker教程通俗理解1 -- Docker理解与配置_第3张图片

需求:64bit Ubuntu + 内核>3.10 (用 uname -r 检查) + non-root user + sudo 特权

安装步骤:

        1. add the GPG key for the official Docker repository to the system:  关于gpgkey教程:  http://www.ruanyifeng.com/blog/2013/07/gpg.html
 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
        2.  Add the Docker repository to APT sources list :
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
        3. update the package database with the Docker packages from the newly added repo:
sudo apt-get update
        4. Make sure you are about to install from the Docker repo instead of the default Ubuntu 16.04 repo: 
apt-cache policy docker-ce
        得到类似下图的输出
docker-ce:
  Installed: (none)
  Candidate: 17.03.1~ce-0~ubuntu-xenial
  Version table:
     17.03.1~ce-0~ubuntu-xenial 500
        500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
     17.03.0~ce-0~ubuntu-xenial 500
        500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
        5. install  安装docker社区版本(CE)
sudo apt-get install -y docker-ce
        Docker should now be installed, the daemon started, and the process enabled to start on boot. Check that it's running:
        6. 检测是否成功运行 查看docker的服务是否启动
sudo systemctl status docker
        得到类似下图的输出
Output
● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2016-05-01 06:53:52 CDT; 1 weeks 3 days ago
     Docs: https://docs.docker.com
 Main PID: 749 (docker)
Installing Docker now gives you not just the Docker service (daemon) but also the docker command line utility, or the Docker client. We'll explore how to use the docker command later in this tutorial.   
        如果未启动服务则使用以下代码:
$ sudo systemctl enable docker
$ sudo systemctl start docker
        查看是否安装成功
$ docker run hello-world
3. 避免sudo前缀

     默认情况下, docker 命令会使用 Unix socket 与 Docker 引擎通讯。而只有 root 用户和docker 组的用户才可以访问 Docker 引擎的 Unix socket。出于安全考虑,一般 Linux 系统上不会直接使用 root 用户。因此,更好地做法是将需要使用 docker 的用户加入 docker用户组。

    0. 创建用户组(此处为docker)

    $ sudo groupadd docker

    1. add your username to the docker group:

sudo usermod -aG docker ${USER}
    To apply the new group membership, you can log out of the server and back in, or you can type the following:
su - ${USER}
    You will be prompted to enter your user's password to continue. Afterwards, you can confirm that your user is now added to the docker group by typing:
id -nG
Output
sammy sudo docker
    If you need to add a user to the docker group that you're not logged in as, declare that username explicitly using:
sudo usermod -aG docker username
    The rest of this article assumes you are running the docker command as a user in the docker user group. If you choose not to, please prepend the commands with sudo.

4. Docker的命令行
    1. docker查看所有指令
docker
As of Docker 1.11.1, the complete list of available subcommands includes:


Output


    attach    Attach 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 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 a container or image
    kill      Kill a running container
    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
    network   Manage Docker networks
    pause     Pause all processes within a container
    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 a container
    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
    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 a running container
    tag       Tag an image into a repository
    top       Display the running processes of a container
    unpause   Unpause all processes within a container
    update    Update configuration of one or more containers
    version   Show the Docker version information
    volume    Manage Docker volumes
    wait      Block until a container stops, then print its exit code


0. 其他问题:

    a. 使用docker出现timeout错误

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
78445dd45222: Pulling fs layer
docker: error pulling image configuration: Get https://dseasb33srnrn.cloudfront.net/registry-v2/docker/registry/v2/blobs/sha256/48/48b5124b2768d2b917edcb640435044a97967015485e812545546cbed5cf0233/data?Expires=1490975964&Signature=OUmibCvoXIlDlmt-GF187GDtJySfLIHJdgfs~GTyzARgOPaVYLfU8kLKKFnS3NczD39-PtCdTxEMKmE0IigxpsmDQimidTyuqOIlac-wDGhrLPveHoKIUYZ9QP9hzzAvRyhci9wrsWJJkrsOz3ITUaNRDM3z9xHyyQi07WQGLiQ_&Key-Pair-Id=APKAJECH5M7VWIS5YZ6Q: dial tcp: lookup dseasb33srnrn.cloudfront.net on 192.168.65.1:53: read udp 192.168.65.2:55172->192.168.65.1:53: i/o timeout.
See 'docker run --help'.

    Solu: may be you are behind a firewall/proxy server. i was also behind my office firewall so i tried below steps which resolved this issue.

    For setting up proxy for docker, please do the following:
    (1). Create a systemd drop-in directory for the docker service:

        $ mkdir -p /etc/systemd/system/docker.service.d
        $ vim /etc/systemd/system/docker.service.d/http-proxy.conf
        Then add below content with proxy settings with it,注意将addr和port换成代理的host和port
        [Service]
        Environment="HTTP_PROXY=http://:/"   
    (2). For HTTPS proxy server:
        $ vim /etc/systemd/system/docker.service.d/https-proxy.conf
        Then add below content with proxy settings with it
        [Service]
        Environment="HTTPS_PROXY=https://:/"
    (3). Flush changes execute: $ sudo systemctl daemon-reload
    (4). Restart Docker: $ sudo systemctl restart docker

    (5). Verify that the configuration has been loaded: $ systemctl show --property=Environment docker





你可能感兴趣的:(Docker)