/containerize

Primary LanguageShellApache License 2.0Apache-2.0

Containerize (create-container-image)

Create, tag, and optionally publish a container for a GIT repository.

The container tag has the format:

  • <name>:<version>
  • image created from Dockerfile without extension has also tag "latest"

The container name is generated based on the following precedence:

  1. The GIT <organization>/<repository> of the 'origin' remote.
  2. The top level GIT directory
  3. The current directory

The container version is generated based on the following precedence:

  1. Drone tag
  2. Drone branch
  3. GIT branch
  4. The string unknown

Requires

  1. A .git directory in CWD or parent path
  2. One or more Dockerfiles in the project at the current working directory.
    1. File names match: Dockerfile and Dockerfile.*
    2. Dockerfiles SHOULD contain:
    ARG TAG
    ARG GIT_DESCRIBE
    ARG GIT_SHA
    ARG BUILD_DATE
    ARG SRC_REPO
    LABEL TAG=$TAG \
      GIT_DESCRIBE=$GIT_DESCRIBE \
      GIT_SHA=$GIT_SHA \
      BUILD_DATE=$BUILD_DATE \
      SRC_REPO=$SRC_REPO
    
  3. Docker installed on the localhost.
  4. [Optional] Tags will prefer a git repository but can work without it.

Registries

  1. Containerize will attempt to publish to the registries listed in a registry file, see Registry File Precedence
  2. Containerize will attempt to create repositories for some versions of Dockerhub (e.g. HPE's version), and AWS ECR
    1. The Namespace for HPE's Dockerhub is required to exist prior to running this utility

Registry File Precedence

  1. The -f <file> command line option
  2. A user registry file: <project_dir>/.dev/registry.json
    1. Be sure to add .dev/ to the project .gitignore file.
  3. A project registry file: <project_dir>/registry.json
  4. A program registry file in the docker image for containerize

Registry File Format

Follow this example showing an unsecure private registry, a secure private registry, a secure aws ec2 registry: registry.json`

Usage

Options

  • containerize.sh : Will build a container image
  • containerize.sh --publish: Will build and publish a container image
  • containerize.sh -q|--quiet : Will only print the tag <name>:<version>

CLI/bash

  1. Get the containerize image:
    1. Build it from source: ./containerize.sh
    2. Pull from registry: docker pull containerize:latest
  2. cd <project_dir>
  3. docker run --init --rm --workdir="$PWD" -v $HOME/.docker:/root/.docker -v /var/run/docker.sock:/var/run/docker.sock -v "$PWD":"$PWD" -e HTTP_PROXY -e HTTPS_PROXY -e NO_PROXY -e http_proxy -e https_proxy -e no_proxy containerize

Drone

clone:
  tags: true
build:
  create-container:
    image: picasso/containerize:latest
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    commands:
      - containerize.sh --publish

Issues and Troubleshooting

  1. Docker login not working:
    1. Identification: WARNING: Error loading config file: .docker/config.json: open .docker/config.json: permission denied
    2. Cause: If docker login has not previously been run then the $HOME/.docker/config.json file will not exist. Executing this tool will create it but may create it with root ownership.
    3. Solution: sudo rm -f $HOME/.docker/config.json and then run docker login again.