LoginSignup
8
10

More than 3 years have passed since last update.

【Docker】軽量なTerraformコンテナを立てる

Posted at

Terraformとは

インフラ構築や設定をコード(テンプレートファイル)を使って自動化するためのツールで、
Infrastructure as Codeによるコードベースで安全で効率的にインフラを管理できる。

複数クラウドに対応しており、OCIだけでなく他のクラウドでもTerraformを利用することができる。

dockerfileとdocker-composeはgithubで公開しています。

dockerfileの作成

ベースイメージはalpineを利用します。
ARGでterraformのバージョンを指定しています。

また、terraform init時にcurlを利用するので、
apk addでcurlをインストールしています。

FROM alpine:3.10

ARG terraform_version="0.12.5"

# Install terraform
RUN apk update --no-cache \
    && apk add --no-cache \
        wget \
        unzip \
        curl \
    && wget https://releases.hashicorp.com/terraform/${terraform_version}/terraform_${terraform_version}_linux_amd64.zip \
    && unzip ./terraform_${terraform_version}_linux_amd64.zip -d /usr/local/bin/ \
    && rm -rf ./terraform_${terraform_version}_linux_amd64.zip \
    && mkdir terraform

docker imageの生成

dockerfileがある階層で以下を実行します。

$ docker build -t alpine-terraform .

docker-compose.ymlの作成

[Your terraform project]には、terraformのコードがあるパスを指定してください。

version: '3'
services:
  web_container:
    image: alpine-terraform
    restart: always
    container_name: terraform_container
    volumes:
      - [Your terraform project]:/terraform
    tty: true

terraformコンテナの起動

docker-compose.ymlがある階層で以下を実行します。

$ docker-compose up -d

terraformコンテナへ接続

docker exec -it terraform_container /bin/sh

terraformディレクトリがあるので、その中でterraformを実行していきます。

8
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
10