首页 > 解决方案 > Alpine:等待docker启动后再继续

问题描述

我正在为我的 CI/CD 机器使用 Alpine linux。下面的docker文件:

FROM node:10.15-alpine
RUN npm i -g sequelize sequelize-cli mysql2
RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories
RUN apk update
RUN apk add --update git bash openssh terraform aws-cli docker openrc
WORKDIR /var/app

问题是当 CI 尝试执行 docker 代码时,例如。docker login, 它失败:

[Container] 2019/01/10 11:18:10 Running command $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Warning: failed to get default registry endpoint from daemon (Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?). Using system default: https://index.docker.io/v1/
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
 [Container] 2019/01/10 11:18:10 Command did not exit successfully $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email) exit status 1

当我尝试添加该行时

RUN service docker start

我让码头工人已经开始了

那我该如何等待 docker 启动呢?

标签: dockeralpine

解决方案


请考虑@DavidMaze 的答案,据说 DinD 不是正确的方法,但我认为这似乎更容易......此外,我在 AWS 上找到了有关如何完成这项工作的文档。

https://docs.aws.amazon.com/codebuild/latest/userguide/sample-docker-custom-image.html

我学到的关键点是我需要在 CodeBuild 上启用特权模式,然后在我的 buildspec 中,我需要这样做:

phases:
  install:
    commands:
      - nohup /usr/local/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://127.0.0.1:2375 --storage-driver=overlay2&
      - timeout -t 15 sh -c "until docker info; do echo .; sleep 1; done"


推荐阅读