Docker Container for deploying to AWS with CLI

2019-10-10

This is about the docker environment to use aws cli. Though there is much information about this way.

By using docker, It can keep clean on localhost and easily manage many AWS accounts. It’s useful to deploy to AWS from this docker container.

Docker Image with the AWS CLI

create a Dockerfile as the following

FROM docker
ARG pip_installer="https://bootstrap.pypa.io/get-pip.py"
ARG awscli_version="1.16.76"

RUN apk --update add 
    bash 
    python 
    curl 
    groff 
    jq 
    less

RUN curl ${pip_installer} | python && 
    pip install awscli==${awscli_version}
RUN bash -c 'echo complete -C '/usr/bin/aws_completer' aws  >> $HOME/.bashrc'

ENV PS1="[u@h:w]$"

And create “credentials" and “config".
credentials

[ecr]
aws_access_key_id = HOGE
aws_secret_access_key = FUGA

config

[profile ecr]
region = ap-northeast-1

Put credentials and config in “.aws" directory, then these are mounted with docker-compose.yml.

version: '3'
services:
  aws-cli:
      build: ./
      working_dir: /src
      volumes:
        - /var/run/docker.sock:/var/run/docker.sock
        - ./.aws:/root/.aws
      tty: true

Run to create a Docker Image with the AWS CLI.

docker-compose build
docker-compose up -d

Use docker command in Docker container

It can use docker command on the above docker image. Because the container was created from “Official docker" image, and mounted “/var/run/docker.sock" on host machine to container.

It can build other Docker container or image on this Docker container, then it can push the image built to AWS ECR.
For example, use the following command in the AWS-CI container.

$(aws ecr get-login --region ap-northeast-1 --profile erc --no-include-email)
docker commit [CONTAINER NAME] ECR-URI
docker push ECR-URI

This Docker container can do from building Docker Image to deploying to AWS.