JavaScriptを有効にしてください

【Github】Github Actions で Docker を使う

 ·  ☕ 1 分で読めます

Github Actions Dockerを使う

Github Actions で自分が構築したDockerを使ってテストなどをする方法。

Dokcer を使う

CentOS の Image を pull してバージョンを出力するだけ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
name: Docker

on:
  push:
    branches:
      - master

jobs:
  deploy:
    # Ubuntu
    runs-on: ubuntu-latest
    steps:
      # Docker Set Up
      - name: Docker Set Up
        run: |
                    docker pull centos

      # Docker Exec
      - name: Docker Exec
        run: |
                    docker run centos sh -c "cat /etc/redhat-release"

Docker Compose を使う

Docker で環境を作る際は Docker Compose で設定していることが多いと思いますので実際には使用する際にはこちらの方がメインになると思います。

Docker Compose の用意

サンプルなのでCentOS の Image を作るだけ。

1
2
3
4
5
6
7
version: '3'

services:
  centos:
    image: centos
    container_name: centos
    tty: true

Github Actions で Docker Compose

別途Dockerとソースファイルが別れている場合があると思いますが今回はそのパターンを割愛しています。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
name: Docker Compose

on:
  push:
    branches:
      - master

jobs:
  deploy:
    # Ubuntu
    runs-on: ubuntu-latest
    steps:
      # Checkout
      - name: Checkout
        uses: actions/checkout@v2
        with:
          submodules: true
          fetch-depth: 0

      # Docker Set Up
      - name: Docker Set Up
        run: |
                    docker-compose up -d --build

      # CentOS Version
      - name: CentOS Version
        run: |
                    docker-compose exec -T centos sh -c "cat /etc/redhat-release"

共有

こぴぺたん
著者
こぴぺたん
Copy & Paste Engineer