CircleCI2.0 のDocker環境からAWS ECRにpushする

CircleCI1.0からCircleCI2.0への移行作業中、ECRへのpushでハマった。

```bash
Warning: failed to get default registry endpoint from daemon (Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?). Using system default: https://index.docker.io/v1/
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
Exited with code 1
```

ようやく解決したので備忘録として記事に残します。

# CircleCI1.0でのECRデプロイコード

CircleCIの設定画面から
`AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`をセットしておく

```yml:circle.yml
machine:
  services:
    - docker
  node:
    version: 6.0.0
  environment:
    AWS_ID: {ここにAWS ACCOUNT ID}

dependencies:
  override:
    - npm install

test:
  override:
    - npm run build

deployment:
  development:
    branch: master
    commands:
      - $(aws ecr get-login --region ap-northeast-1)
      - docker build -t hogehoge:fuga-$CIRCLE_SHA1 .
      - docker tag hogehoge:fuga-$CIRCLE_SHA1 ${AWS_ID}.dkr.ecr.ap-northeast-1.amazonaws.com/hogehoge:fuga-$CIRCLE_SHA1
      - docker push ${AWS_ID}.dkr.ecr.ap-northeast-1.amazonaws.com/hogehoge:fuga-$CIRCLE_SHA1
```

# CircleCI2.0 での書き方（ボツ）

頑張って2.0に置き換えてみた

```yml:.circleci/config.yml
version: 2
defaults: &defaults
  working_directory: ~/workspace
  docker:
    - image: circleci/python:2.7-node-browsers
      environment:
        AWS_ID: {ここにAWS ACCOUNT ID}

jobs:
  ecr:
    <<: *defaults
    steps:
      - checkout
      - run: npm install
      - run: npm run build
      - run: sudo pip install awscli
      - run: $(aws ecr get-login --region ap-northeast-1 --no-include-email)
      - run: docker build -t hogehoge:fuga-$CIRCLE_SHA1 .
      - run: docker tag hogehoge:fuga-$CIRCLE_SHA1 ${AWS_ID}.dkr.ecr.ap-northeast-1.amazonaws.com/hogehoge:fuga-$CIRCLE_SHA1
      - run: docker push ${AWS_ID}.dkr.ecr.ap-northeast-1.amazonaws.com/hogehoge:fuga-$CIRCLE_SHA1

# Workflows
workflows:
  version: 2
  ecr_push:
    jobs:
      - ecr:
          filters:
            branches:
              only: /^master/
```

Docker daemonがいないと怒られてしまった。

```bash
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
```

![error.png](https://qiita-image-store.s3.amazonaws.com/0/216978/09b53978-b711-a019-f928-ef2b2087da85.png)

# CircleCI2.0 での書き方(正)

丸一日悩んだ結果、公式ドキュメントに `setup_remote_docker` を発見。
これで Docker in Dockerが実現するらしい。

```yml:.circleci/config.yml
version: 2
defaults: &defaults
  working_directory: ~/workspace
  docker:
    - image: circleci/python:2.7-node-browsers
      environment:
        AWS_ID: {ここにAWS ACCOUNT ID}

jobs:
  ecr:
    <<: *defaults
    steps:
      - checkout
      # Docker in Docker
      - setup_remote_docker # コレを書くことでDocker上にDockerが立ち上がる
      - run: npm install
      - run: npm run build
      - run: sudo pip install awscli
      - run: $(aws ecr get-login --region ap-northeast-1 --no-include-email)
      - run: docker build -t hogehoge:fuga-$CIRCLE_SHA1 .
      - run: docker tag hogehoge:fuga-$CIRCLE_SHA1 ${AWS_ID}.dkr.ecr.ap-northeast-1.amazonaws.com/hogehoge:fuga-$CIRCLE_SHA1
      - run: docker push ${AWS_ID}.dkr.ecr.ap-northeast-1.amazonaws.com/hogehoge:fuga-$CIRCLE_SHA1

# Workflows
workflows:
  version: 2
  ecr_push:
    jobs:
      - ecr:
          filters:
            branches:
              only: /^master/
```