Open10

Dockerのナレッジ

SoraSora

ふーん、マルチビルドは軽くなっていいらしい。docker docsの公式でも進められてると。

Multi-stage builds allow you to drastically reduce the size of your final image, without struggling to reduce the number of intermediate layers and files.

https://qiita.com/Tsuyozo/items/c706a04848c3fbbaf055

# syntax=docker/dockerfile:1
FROM golang:1.16-alpine AS build

# Install tools required for project
# Run `docker build --no-cache .` to update dependencies
RUN apk add --no-cache git
RUN go get github.com/golang/dep/cmd/dep

# List project dependencies with Gopkg.toml and Gopkg.lock
# These layers are only re-built when Gopkg files are updated
COPY Gopkg.lock Gopkg.toml /go/src/project/
WORKDIR /go/src/project/
# Install library dependencies
RUN dep ensure -vendor-only

# Copy the entire project and build it
# This layer is rebuilt when a file changes in the project directory
COPY . /go/src/project/
RUN go build -o /bin/project

# This results in a single layer image
FROM scratch
COPY --from=build /bin/project /bin/project
ENTRYPOINT ["/bin/project"]
CMD ["--help"]
SoraSora

なるほど。開発中のDockerfileはRunを複数に切ってbuildを早く。リリース時はRunを&&で纏める方が良いと。

dockerのimageを一度buildすると作成されたLayerはdocker cacheという場所に配置される。
そして再度imageのbuildを行った際に、RUNのcommandの内容が、前回のbuildのRUNのcommandの内容と同じ場合、
前回のimageのbuildで作成されたLayerを使用する。

docker docsにもそのように記載されている

After building the image, all layers are in the Docker cache.
Docker sees the initial and modified instructions as identical and reuses the cache from previous steps.

つまり中間のLayerがあったほうがdocker cacheを利用して、
差分buildを行うことができるので早くbuildのトライアンドエラーができる。
そのため開発環境で最初は開発効率を損なわないようにするために、すべてのステップでRUNでcommandを呼び出し、
build内容が決まってきたら、RUNの回数を減らすために&&を使用するようにするのが良い。

SoraSora

確かに。package won't be updated that often but the application is. Makes sense and sounds pretty useful tips.

SoraSora

Build, image に含めたくなかったら、.dockerignoreを使うといいと。
ただしワイルドカード多用すると、ファイルのパースに時間かかるから、要注意と。

SoraSora

how to check docker's File Description

[s.daibu@ec2~]$ sudo cat /etc/sysconfig/docker
# The max number of open files for the daemon itself, and all
# running containers.  The default value of 1048576 mirrors the value
# used by the systemd service unit.
DAEMON_MAXFILES=1048576

# Additional startup options for the Docker daemon, for example:
# OPTIONS="--ip-forward=true --iptables=true"
# By default we limit the number of open files per container
OPTIONS="--default-ulimit nofile=1024:4096"

# How many seconds the sysvinit script waits for the pidfile to appear
# when starting the daemon.
DAEMON_PIDFILE_TIMEOUT=10

https://documentation.sisense.com/8-6/linux/dockerlimits.htm#gsc.tab=0

SoraSora

https://qiita.com/Keyskey/items/a1baa04d6850cc50727c

Go で Docker MySQL に接続しようとして、dial tcp 172.21.0.2:3306: connect: connection refused になるとき

エラー内容

go_1  | 2021/10/08 09:47:03 dial tcp 172.21.0.2:3306: connect: connection refused

原因

api server の方にmysql の環境変数を指定してなく、
api server側でmysqlの情報がわからなかったから。

BEFORE

version: '3'
services:
  api:
    build:
      context: .
      dockerfile: Dockerfile
    command: /bin/sh -c "go run app/main.go"
    stdin_open: true
    tty: true
    volumes:
      - .:/app
    ports:
      - 8080:8080

  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: hogehoge
      MYSQL_USER: admin
      MYSQL_PASSWORD: password
      TZ: 'Asia/Tokyo'
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    volumes:
      - db-data:/var/lib/mysql
      - ./db/my.cnf:/etc/mysql/conf.d/my.cnf
    ports:
      - 3306:3306


volumes:
  db-data:
    driver: local

AFTER

変更点:api の方にmysqlの環境変数を追加した

version: '3'
services:
  api:
    build:
      context: .
      dockerfile: Dockerfile
    command: /bin/sh -c "go run app/main.go"
    stdin_open: true
    tty: true
    volumes:
      - .:/app
    ports:
      - 8080:8080
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: hogehoge
      MYSQL_USER: admin
      MYSQL_PASSWORD: password


  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: hogehoge
      MYSQL_USER: admin
      MYSQL_PASSWORD: password
      TZ: 'Asia/Tokyo'
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    volumes:
      - db-data:/var/lib/mysql
      - ./db/my.cnf:/etc/mysql/conf.d/my.cnf
    ports:
      - 3306:3306


volumes:
  db-data:
    driver: local