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?

More than 1 year has passed since last update.

DockerでPHP+PostgreSQLを使うとき用Tips

Posted at

この記事は

  • とても初心者向けです
  • Docker + PHP + PostgreSQLの構成を試したらハマり所が多かったのでメモを残しておく

例Dockerfile/docker-compose.yml

Dockerfile

FROM "php:8.1.16-apache"

ENV COMPOSER_ALLOW_SUPERUSER=1
WORKDIR /var/www/html
COPY composer.json /var/www/html/
COPY composer.lock /var/www/html/
RUN apt-get update
RUN apt-get install zip unzip
ADD https://raw.githubusercontent.com/mlocati/docker-php-extension-installer/master/install-php-extensions /usr/local/bin/
RUN chmod uga+x /usr/local/bin/install-php-extensions && sync && \
    install-php-extensions pdo_pgsql
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

docker-compose.yml

version: "3"
services:
  php:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: php
    volumes:
      - ./app:/var/www/html
    ports:
      - 8080:80
    command: bash -c "composer install && docker-php-entrypoint apache2-foreground"
  pgsql:
    image: postgres:11.19-alpine
    container_name: pgsql
    volumes:
      - ./db/data:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=root
      - POSTGRES_USER=root
      - POSTGRES_PASSWORD=root
    ports:
      - 5432:5432

解説

Dockerfile

RUN apt-get install zip unzip

  • これがないとcomposerがそもそも開けない。元から入ってないのでインストールしておく

ADD ~ && install-php-extensions pdo_pgsql

docker-compose.yml

command: bash -c "composer install && docker-php-entrypoint apache2-foreground"

  • composer install をDockerfileの中でやっても意味がないと気づいた末のcommand
    • DockerfileのWorkdirの中でやると、結局そのディレクトリがマウントされて用意した /vender が上書き・不可視になってしまうので、autoloaderのエラーが起きてしまう
    • なのでここでディレクトリマウントしてからinstallすることで/venderがないエラーを回避
    • 毎回の起動にちょっと時間がかかってしまうのでもっと良いやり方はありそう
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?