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学習

Last updated at Posted at 2025-02-21

学習にオススメの記事

入門Docker

docker公式日本語訳

チームにdockerを布教することになったので、布教の教材を作ってみた。

play with docker

Dockerコマンド一覧

Dockerのお掃除

DockerFileの例

# DockerHubにあるイメージファイルを指定
FROM php:8.0-fpm

# RUNコマンド:buildしたときに元となるdockerイメージに対して実行する処理を記述
# \(バックスラッシュ):複数行に分けることが可能。
# ライブラリのインストール
RUN apt-get update
RUN apt-get install -y \
      sudo \
      libpq-dev \
      git \
      zip \
      unzip \
      vim
RUN docker-php-ext-install \
      pdo \
      bcmath \
      pdo_mysql

# Xdebugのインストール
RUN pecl install xdebug
RUN docker-php-ext-enable xdebug

# Composerのインストール & パス通す
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# nodeインストール(aptだとNodejsは古いため、パッケージマネージャの n を使用してインストール)
RUN apt install -y npm \
    && npm install n -g \
    && n stable

# 設定の置換
COPY php.ini /usr/local/etc/php/php.ini

# RUN , CMD , ENTRYPOINT , COPY , ADD を実行する時のワーキングディレクトリを指定
WORKDIR /var/www

docker-compose.ymlの例

version: "3"

services:
  #  PHPコンテナ
  app:
    # コンテナの名前を指定
    container_name: app
    # Dockerファイルを使って、コンテナをビルドするという指定
    build: ./docker/php
    # ローカルのディレクトリとコンテナ上のディレクトリをリンクを設定
    volumes:
      - .:/var/www
    # x86_64アーキテクチャのイメージを強制的に使用(Mac用)
    platform: linux/x86_64
    depends_on:
      - db
  # nginxコンテナ
  nginx:
    image: nginx:1.24-alpine
    container_name: nginx
    # ホストの3000ポートをコンテナの3000ポートにマッピング
    ports:
      - 8000:80
    volumes:
      - .:/var/www
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
    platform: linux/amd64
    working_dir: /var/www
    depends_on:
      - app
  # DBコンテナ
  db:
    # MySQL 5.7の公式イメージを使用
    image: mysql:5.7
    container_name: db
    environment:
      # MySQLのrootユーザーのパスワード
      MYSQL_ROOT_PASSWORD: root
      # 作成するデフォルトデータベース
      MYSQL_DATABASE: database
      # 作成するユーザー
      MYSQL_USER: db-user
      # 作成するユーザーのパスワード
      MYSQL_PASSWORD: db-pass
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    volumes:
      - ./docker/db/data:/var/lib/mysql
      - ./docker/db/my.cnf:/etc/mysql/conf.d/my.cnf
      - ./docker/db/sql:/docker-entrypoint-initdb.d
    platform: linux/x86_64
    ports:
      - 3306:3306

Docker起動

rootディレクトリにて、以下のコマンドを実行する
docker-compose up -d --build
PHPのコンテナに入る
docker exec -it app /bin/bash
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?