Skip to content

Hypervisor Docker #44

Open
Open

Description

@junxnone

Docker

Install Docker

Ubuntu 下安装docker

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
sudo apt-get update
sudo apt-get install docker-ce
  • 将当前用户添加到 docker 组
sudo usermod -aG docker $USER

安装指定版本的docker

curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get -y update
sudo apt-cache madison docker-ce
sudo apt-get install -y docker-ce=$(apt-cache madison docker-ce | grep 17.12.1 | head -1 | awk '{print $3}')

UseCase

Usecase cmd
Verify Install docker run hello-world
Save/Load docker image docker save -o demo.tar demo:latest
sudo docker load -i demo.tar
Export/Import docker Filesystem image docker export demo> demo.tar
docker import demo.tar demo:latest
Run in bash docker exec -it docker_name bash
删除 none 镜像 docker rmi -f `docker images | grep 'none' | awk '{print $3}'`
以 root 用户进入 container docker exec -it --user root container_id
删除 所有 Stopped Container docker container prune

如果打包包含了多个 image 则可以通过 your_image.tar/repositories 查看 镜像名称 和 commit id

Proxy setup

  • Setup container proxy
  • Setup daemon proxy
维度 Daemon Proxy Container Proxy
谁用它 dockerd 自己(root 进程) 容器内进程(app/pip/curl/apt)
作用 让 docker pull/push/login 能出网 让容器里的程序能访问公网
配置位置 /etc/systemd/system/docker.service.d/http-proxy.conf 的 Environment= 容器的环境变量 HTTP_PROXY=...
生效时机 systemctl daemon-reload && restart docker 容器启动时(不重启容器无法变更)
影响范围 整个 Docker 引擎,所有 pull 都受影响 仅这一个容器内进程
shell 里的 http_proxy 有用吗 ❌ 完全没用——daemon 是 systemd 起的 ❌ 默认不会传进容器,必须显式注入
失败现象 docker pull 超时 / DNS 失败 容器跑起来了,但 pip install / curl / 模型下载超时

Setup container Proxy

vi ~/.docker/config.json
{
 "proxies":
 {
   "default":
   {
     "httpProxy": "http://127.0.0.1:8123",
     "httpsProxy": "http://127.0.0.1:8123",
     "noProxy": "*.test.example.com,.example2.com"
   }
 }
}

Setup daemon proxy

sudo mkdir -p /etc/systemd/system/docker.service.d
sudo tee /etc/systemd/system/docker.service.d/http-proxy.conf > /dev/null <<EOF
[Service]
Environment="HTTP_PROXY=http://127.0.0.1:8123"
Environment="HTTPS_PROXY=http://127.0.0.1:8123"
Environment="NO_PROXY=localhost,127.0.0.1,::1,.local"
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
  • Proxy & Mirror 配置完后都要重新加载配置文件和重启 docker 生效

Mirrors Setup

sudo vi /etc/docker/daemon.json
{
 "registry-mirrors": ["https://registry.docker-cn.com"]
}
  • Yocto
    • Yocto 环境下 docker build 不能获取 apt update: 需要配置 DNS
  • mirrors
    • USTC Mirror : https://docker.mirrors.ustc.edu.cn
  • 如果碰到如下问题可以更换 mirror 试试
toomanyrequests: You have reached your pull rate limit. You may increase the limit by authenticating and upgrading: https://www.docker.com/increase-rate-limit

docker容器的自启动

--restart 参数

  • no 不自动重启容器. (默认值)
  • on-failure 容器发生error而退出(容器退出状态不为0)重启容器,可以指定重启的最大次数,如:on-failure:10
  • unless-stopped 在容器已经stop掉或Docker stoped/restarted的时候才重启容器
  • always 在容器已经stop掉或Docker stoped/restarted的时候才重启容器,手动stop的不算

e.g.
docker run --restart always -d -v ~/workm/opengrok_src:/opengrok/src -p 8080:8080 opengrok/docker:latest
如果容器已经被创建,我们想要修改容器的重启策略
e.g.
docker update --restart always xxxx

Reference

Activity

  1. transferred this issue fromjunxnone/tioon Sep 8, 2019
  2. transferred this issue fromjunxnone/exampleson Sep 8, 2019
  3. changed the title [-]Docker[/-] [+]Docker - Summary[/+] on Oct 28, 2020
  4. changed the title [-]Docker - Summary[/-] [+]Docker[/+] on Sep 20, 2021
  5. changed the title [-]Docker[/-] [+]Hypervisor Docker[/+] on Dec 22, 2022
  6. transferred this issue fromjunxnone/techwikion Dec 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions