如何编写一个 Dockerfile 来运行另一个 docker 容器

问题描述

最近我们从 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

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

问题是,我如何编写一个具有所有这些依赖关系的 Dockerfile

解决方法

这里是一个例子,如何在 docker 中使用 docker with nodejs 您可以对依赖项执行相同操作,也可以基于 docker 镜像中的 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

祝你好运

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...