7.3.2. Docker 外部 registry 白名单和黑名单
通过为 docker 守护进程配置 registries 和 block_registries 标志,可以将 Docker 配置为阻止来自外部 registry 的操作。
流程
使用
registries标记将允许的 registry 添加到/etc/containers/registries.conf文件中:registries: - registry.redhat.io - my.registry.example.com注意docker.ioregistry 可使用相同的方法添加。阻止其余的 registry:
block_registries: - all阻止旧版本中的 registry 的其余部分:
BLOCK_REGISTRY='--block-registry=all'重启
docker守护进程:$ sudo systemctl restart docker.service重启
docker守护进程会导致docker容器重启。在本例中,
docker.ioregistry 已被列入黑名单,因此有关该 registry 的任何操作都会失败:$ sudo docker pull hello-world Using default tag: latest Trying to pull repository registry.redhat.io/hello-world ... Trying to pull repository my.registry.example.com/hello-world ... Trying to pull repository registry.redhat.io/hello-world ... unknown: Not Found $ sudo docker pull docker.io/hello-world Using default tag: latest Trying to pull repository docker.io/library/hello-world ... All endpoints blocked.通过再次修改文件并重新启动服务,将
docker.io添加会registry变量。registries: - registry.redhat.io - my.registry.example.com - docker.io block_registries: - all或者
ADD_REGISTRY="--add-registry=registry.redhat.io --add-registry=my.registry.example.com --add-registry=docker.io" BLOCK_REGISTRY='--block-registry=all'重启 Docker 服务:
$ sudo systemctl restart docker验证镜像现在可拉取(pull):
$ sudo docker pull docker.io/hello-world Using default tag: latest Trying to pull repository docker.io/library/hello-world ... latest: Pulling from docker.io/library/hello-world 9a0669468bf7: Pull complete Digest: sha256:0e06ef5e1945a718b02a8c319e15bae44f47039005530bc617a5d071190ed3fc如果需要使用外部 registry,例如修改所有需要使用该 registry 的节点主机中的
docker守护进程配置文件,请在这些节点上创建一个黑名单以避免恶意容器被执行。使用 Ansible 安装程序,这可以使用 Ansible 主机文件中的
openshift_docker_additional_registries和openshift_docker_blocked_registries变量进行配置:openshift_docker_additional_registries=registry.redhat.io,my.registry.example.com openshift_docker_blocked_registries=all