docker入门 - 安装

docker tutorial 1

Changelog

data: 2020-05-26

Added

  • 增加 Fedora Docker 安装配置

1. Install docker

The following is taken from the docker documentation, archlinux wiki and some other pages.

1.1 Fedora

  1. Install the latest version of Moby Engine.

    1
    sudo dnf install docker
  2. Cgroups Exception: For Fedora 31 and higher, you need to enable the backward compatibility for Cgroups.

    1
    sudo grubby --update-kernel=ALL --args="systemd.unified_cgroup_hierarchy=0"

    After running the command, you must reboot for the changes to take effect.

  3. Start the docker.service.

    1
    sudo systemctl start docker
  4. Verify that Docker is installed correctly.

    1
    sudo docker info

1.2 Arch Linux

  1. Install the latest version of Docker Engine - Community

    1
    yay -S docker
  2. Start the docker.service

    1
    sudo systemctl start docker
  3. Verify that Docker Engine - Community is installed correctly.

    1
    sudo docker info

1.3 Ubuntu & Mint

  1. Update the apt package index:

    1
    sudo apt-get update
  2. Install packages to allow apt to use a repository over HTTPS:

    1
    2
    3
    4
    5
    6
    sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
  3. Add Docker’s official GPG key:

    1
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  4. Use the following command to set up the stable repository.

    • Ubuntu:
    1
    2
    3
    4
    sudo add-apt-repository \
    "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/ \
    $(lsb_release -cs) \
    stable"
    • Mint 19.x (based Ubuntu Bionic, so):

    [Note] There is an error in the official documentation.“ In Ubuntu, it seems to be provided by software-properties-common package, while Mint replaced it with mintsources.” from @rbbernardino.

    Deletting[arch=amd64]can fix it. “ I fixed by just removing the [arch=amd64] from the source.” from @mau21mau.

    1
    2
    3
    4
    sudo add-apt-repository \
    "deb https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/ \
    Bionic \
    stable"
  5. Update the apt package index.

    1
    sudo apt-get update
  6. Install the latest version of Docker Engine - Community

    1
    sudo apt-get install docker-ce docker-ce-cli containerd.io
  7. Verify that Docker Engine - Community is installed correctly.

    1
    sudo docker info

    2. Post-installation steps for Linux

2.1 Manage Docker as a non-root user

To create the docker group and add your user:

  1. Create the docker group.

    1
    sudo groupadd docker
  2. Add your user to the docker group.

    1
    sudo usermod -aG docker $USER
  3. Log out and log back in so that your group membership is re-evaluated.

    If testing on a virtual machine, it may be necessary to restart the virtual machine for changes to take effect.

    On Linux, you can also run the following command to activate the changes to groups:

    1
    newgrp docker
  4. Verify that you can run docker commands without sudo.

    1
    docker run hello-world

    This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.

2.2 Configure Docker to start on boot

1
sudo systemctl enable docker

2.3 Configure HTTP/HTTPS proxy

  1. Create a systemd drop-in directory for the docker service:
1
sudo mkdir -p /etc/systemd/system/docker.service.d
  1. Create a file called /etc/systemd/system/docker.service.d/https-proxy.conf that adds the HTTPS_PROXY environment variable:
1
2
[Service]
Environment="HTTPS_PROXY=http://192.168.160.1:7890/" "NO_PROXY=localhost,127.0.0.1,mydocker-registry.com:5000"
  1. Flush changes:
1
sudo systemctl daemon-reload
  1. Restart Docker:
1
sudo systemctl restart docker
  1. Verify that the configuration has been loaded:
1
systemctl show --property=Environment docker

3. Daemon configuration file

  1. Change or Create a file called /etc/docker/daemon.json that sets the registry-mirrors

    1
    sudo vim /etc/docker/daemon.json
    1
    2
    3
    4
    5
    6
    7
    {
    "registry-mirrors": [
    "https://docker.mirrors.ustc.edu.cn",
    "https://hub-mirror.c.163.com",
    "https://registry.docker-cn.com"
    ]
    }
  2. Restart Docker.

    1
    sudo systemctl restart docker.service
  3. Verify that Docker Engine - Community is installed correctly by running the hello-world image.

    1
    sudo docker run hello-world

    This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.

参考1:Docker Documentation
参考2:阮一峰. Docker 入门教程
参考3:https://pastebin.com/7bdKPafy
参考4:Docker Daemon生产环境关键的几个参数