从 Gitlab-CI 构建并推送 docker 图像到 Amazon AWS ECR

Building and pushing docker image from Gitlab-CI to Amazon AWS ECR

我在自己的机器上托管了一个私有的 Gitlab。我将我的代码存储在 Gitlab 中,并希望从 Docker 文件构建一个 Docker 图像,然后将其推送到我的 Amazon ECR 注册表。不幸的是,这不起作用,因为它会引发错误:

Flag --email has been deprecated, will be removed in 1.13.
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
/dev/mapper/control: open failed: Operation not permitted
Failure to communicate with kernel device-mapper driver.
Check that device-mapper is available in the kernel.
Command failed
mount: permission denied
Could not mount /sys/kernel/security.
AppArmor detection and --privileged mode might break.
mkdir: cannot create directory '/sys/fs/cgroup/name=systemd': Read-only file system
mount: mount point /sys/fs/cgroup/name=systemd is not a directory
ln: failed to create symbolic link '/sys/fs/cgroup/systemd/name=systemd': Read-only file system

Timed out trying to connect to internal docker host.

gitlab-ci 代码如下所示:

stages:
  - build

build_airflow:
  stage: build
  only: [master, develop]
  image: gitlab/dind:latest
  services:
    - docker:dind
  script:
    - APP=airflow
    - sh ./scripts/login-ecs.sh my_fancy_project

login-ecs.sh 脚本只是执行 eval $(/usr/local/bin/aws ecr get-login --region eu-central-1),它应该将映像登录到 ECR docker 注册表(来源:https://blog.madisonhub.org/gitlab-ci-build-how-to-login-to-ecr/)。

我找不到问题,所以我希望你能帮助我。

提前谢谢你。

PS:如果我在本地执行来自 aws ecr get-login 的命令,它工作正常。所以它应该与 gitlab 做一些事情。我是否必须更改一些允许私有注册表登录的配置?

背景:
您基本上是在 运行 一个 docker 容器中的 docker 服务器,默认情况下在 gitlab-ci 中不可用。您可以通过 运行ning:

检查 docker 客户端和服务器的状态

docker version

在你的 gitlab-ci 脚本中。这通常是一个好主意,只是为了确保客户端和服务器正确 运行ning。

您会在 运行 之后注意到 docker 服务器未 运行ning,这就是您收到此错误的原因: Cannot connect to the Docker daemon. Is the docker daemon running on this host?

解法:
正如 Gitlab 文档中详细描述的那样,有几种方法可以解决这个问题:
https://docs.gitlab.com/ce/ci/docker/using_docker_build.html

我们解决它的方法是使用 docker-in-docker executor which involves updating your Gitlab Runner configuration and run your build using the special docker-in-docker (dind) Docker Image.

您需要更新 运行ner 以便它 运行 处于 特权 模式。这是一个示例 config.toml:

[[runners]]
  url = "https://gitlab.com/ci"
  token = TOKEN
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "docker:latest"
    privileged = true
    disable_cache = false
    volumes = ["/cache"]
  [runners.cache]
    Insecure = false

然后使用 docker:latest 图像和 docker:dind 服务。这是一个示例 gitlab-ci.yaml 片段:

image: docker:latest

# When using dind, it's wise to use the overlayfs driver for
# improved performance.
variables:
  DOCKER_DRIVER: overlay

services:
- docker:dind

before_script:
- docker info

build:
  stage: build
  script:
  - docker version
  - docker build -t my-docker-image .
  - docker run my-docker-image /script/to/run/tests

另一种方法是设置单独的 docker 服务器,设置环境变量 DOCKER_TLS_VERIFYDOCKER_HOST 以便您的 docker 客户端可以安全地连接到服务器。

要启用 TLS,您需要按照以下说明操作:
https://docs.docker.com/engine/security/https/

我按照以下步骤操作:

https://webcaptioner.com/blog/2017/12/deploy-from-gitlab-to-aws-fargate/

它使用 docker-in-docker 和以下内容来获得登录 AWS ECR 的能力

export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY 
$(aws ecr get-login --no-include-email --region $AWS_REGION | tr -d '\r')

但似乎得到了一个An error occurred (InvalidSignatureException) when calling the GetAuthorizationToken operation 错误。我认为这可能是 python2/python3 的事情?