0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Docker】コンテナ管理と複数コンテナの連携

Posted at

1. Docker Composeの概要

Docker Composeは、複数のコンテナを定義・管理するためのツールです。

  • 特徴:
    • YAMLファイルで設定を記述
    • コンテナの依存関係を管理
  • 主な用途: 開発環境の構築、テスト環境の再現。

2. Docker Composeの基本操作

簡単なWebアプリケーションを例に手順を示します。

  • 準備: ディレクトリを作成し、docker-compose.ymlを以下のように記述します。
version: "3"
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
  db:
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: example
  • 起動: 以下のコマンドでコンテナを一括起動します。
docker-compose up
docker-compose down

3. ボリュームによるデータ永続化

コンテナは一時的であるため、データを永続化するにはボリュームが必要です。

  • ボリュームの作成:
docker volume create my-data
  • コンテナでの使用:
docker run -v my-data:/data ubuntu

 ※ /dataに保存されたデータがmy-dataボリュームに保持。

  • Docker Composeでの設定: docker-compose.ymlに追記します。
version: "3"
services:
  db:
    image: mysql:latest
    volumes:
      - db-data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: example
volumes:
  db-data:

4. ネットワーク設定

コンテナ間の通信を管理します。

  • デフォルトネットワーク: docker-compose upで自動作成されるブリッジネットワークを使用。
  • カスタムネットワークの作成:
docker network create my-network
  • コンテナの接続:
docker run --network my-network -d nginx
docker run --network my-network -it ubuntu bash
  • Composeでの設定例:
version: "3"
services:
  web:
    image: nginx:latest
    networks:
      - app-net
  db:
    image: mysql:latest
    networks:
      - app-net
networks:
  app-net:
    driver: bridge

5. 実践例: WebアプリとDBの連携

以下は、Goアプリケーション(Echo使用)とMySQLを連携させる例です。

  • ディレクトリ構成:
my-app/
├── main.go
├── Dockerfile
└── docker-compose.yml
  • Dockerfile:
FROM golang:1.20
WORKDIR /app
COPY . .
RUN go mod init my-app && go get github.com/labstack/echo/v4 && go build -o app
CMD ["./app"]
  • main.go:
package main

import (
    "database/sql"
    "net/http"
    _ "github.com/go-sql-driver/mysql"
    "github.com/labstack/echo/v4"
)

func main() {
    e := echo.New()
    e.GET("/health", func(c echo.Context) error {
        db, err := sql.Open("mysql", "root:example@tcp(db:3306)/mysql")
        if err != nil {
            return err
        }
        defer db.Close()
        return c.String(http.StatusOK, "DB Connected")
    })
    e.Logger.Fatal(e.Start(":8080"))
}
  • docker-compose.yml:
version: "3"
services:
  app:
    build: .
    ports:
      - "8080:8080"
    depends_on:
      - db
    networks:
      - app-net
  db:
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: example
    volumes:
      - db-data:/var/lib/mysql
    networks:
      - app-net
volumes:
  db-data:
networks:
  app-net:
    driver: bridge

注意点
ボリュームやネットワークの削除を忘れないよう、docker volume rmdocker network rmを使用。
Composeファイルのバージョンに依存する機能を確認し、適切なバージョンを選択。

その他資料

Docker Compose
WebアプリとDBの連携
ボリュームによるデータ永続化
ネットワーク設定
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?