10
12

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 3 years have passed since last update.

Docker内の php から SQL Server に接続する(2020年版)

Posted at

はじめに

普段は、MySQLPostgreSQLが多いので、メモとして残しておきます。

docker-compose.yml

php-fpmnginxの構成に、mssql-serverを下記のように追加します。

docker-compose.yml
version: "3"

services:
  php-fpm:
    build: .
    volumes:
      - .:/var/www/app:cached

  nginx:
    image: nginx
    ports:
      - "8000:80"
    volumes:
      - .:/var/www/app:cached
      - ./etc/nginx/nginx.conf:/etc/nginx/nginx.conf
    depends_on:
      - php-fpm

  mssql-server:
    image: mcr.microsoft.com/mssql/server:2017-latest
    environment:
      ACCEPT_EULA: Y
      SA_PASSWORD: P@ssw0rd
    ports:
      - "1433:1433"

Dockerfile

FROM php:7.4-fpm

# Install basic packages and extensions
RUN apt-get update \
  && apt-get install -y zlib1g-dev libzip-dev wget gnupg \
  && docker-php-ext-install zip opcache

# Install SQL Server extensions
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/debian/10/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get update
RUN ACCEPT_EULA=Y apt-get install -y msodbcsql17 mssql-tools
RUN apt-get install -y unixodbc-dev libgssapi-krb5-2
RUN pecl install sqlsrv
RUN pecl install pdo_sqlsrv
RUN docker-php-ext-enable sqlsrv
RUN docker-php-ext-enable pdo_sqlsrv

WORKDIR /var/www/app

参考リンク

おわりに

数年前は、もう少し手順が多く、トラブルも多かった印象ありますが、MicrosoftLinux対応を頑張っているのか、スムーズにセットアップできました。
参考になれば幸いです。
ではでは。

10
12
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
10
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?