Search code examples
dockerdocker-in-docker

How to write a Dockerfile to run another docker container


Recently we moved from gitlab shell executor to docker+machine executor.

So now here comes the challenge, in my .gitlab-ci.yml I have couple of jobs like below:

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

So I need to have a docker image that has the ability to run apt-get update and docker build -t run-tests . and other aws commands.

Problem is, how can I write a Dockerfile that has all these dependecnies?


Solution

  • Here is an example how to use docker in docker with nodejs You can do the same with your dependencies or you can create new image based on the docker in docker image and use this image in your 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
    

    This uses alpine so there is no apt you can use apk (alpine package manager) instead. You can also mount the docker socket to your existing image, with all the dependencies so this will add docker to your image.

    Edit:
    Create docker image from docker:dind

    FROM docker:dind
    
    RUN apk add --no-cache aws-cli
    

    Build and push your image to your dockerhub or gitlab registry:

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

    In your .gitlab-ci.yml chnage the image (like in the example above i updated it)

    You can test the image localy to make sure it's working and try adding more apps if you need:

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

    Good luck