【问题标题】:How to write a Dockerfile to run another docker container如何编写 Dockerfile 来运行另一个 docker 容器
【发布时间】:2021-04-26 03:25:26
【问题描述】:

最近我们从 gitlab shell executor 转移到了docker+machine executor。

所以现在挑战来了,在我的.gitlab-ci.yml 我有几个工作如下:

stages:
  - RUN_TESTS

build-docker:
  stage: RUN_TESTS
  script:
    - echo "Running the tests..."
    - apt-get update 
    - docker build -t run-tests .
    - aws configure set aws_access_key_id ${effi_access_key}
    - aws configure set aws_secret_access_key ${effi_secrect_key}
    - aws configure set default.region ap-southeast-2
  only:
    - run-test
  tags:
    - shared-spot-runner

所以我需要一个能够运行apt-get updatedocker build -t run-tests . 以及其他aws 命令的docker 图像。

问题是,我如何编写一个包含所有这些依赖项的Dockerfile

【问题讨论】:

  • 你为什么不安装任何东西就运行 apt -get update?

标签: docker docker-in-docker


【解决方案1】:

这是一个如何在 docker 中使用 docker 和 nodejs 的示例 您可以对依赖项执行相同操作,也可以基于 docker image 中的 docker 创建新图像并在 gitlab-ci 中使用此图像。

# This file is a template, and might need editing before it works on your project.
build-master:
  # Official docker image.
  image: my-repo/docker:dind-awscli
  stage: build
  services:
    - docker:dind
  script:
    - apk add nodejs
    - node app.js
    - docker ps

这使用了 alpine,所以没有 apt 你可以使用 apk(alpine 包管理器)来代替。 您还可以将 docker 套接字挂载到您现有的映像,以及所有依赖项,以便将 docker 添加到您的映像中。

编辑
从 docker:dind 创建 docker 镜像

FROM docker:dind

RUN apk add --no-cache aws-cli

构建您的镜像并将其推送到您的 dockerhub 或 gitlab 注册表:

docker build -t my-repo/docker:dind-awscli .
docker push my-repo/docker:dind-awscli

在您的.gitlab-ci.yml 中更改图像(就像在上面的示例中我更新了它)

您可以在本地测试图像以确保其正常工作,并在需要时尝试添加更多应用:

docker run -it --rm my-repo/docker:dind-awscli sh
# Inside the container try
aws version

祝你好运

【讨论】:

  • 我也可以安装aws cli吗?使用apk ?
  • 您好,它成功了,但现在我得到一个不同的错误,我已经打开了另一个问题,你能看看吗? stackoverflow.com/questions/65842641/…
  • 我遇到了和你其他问题一样的错误,所以我在那里回答了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多