by Devin Yang

建立於: 4年前 ( 更新: 4年前 )

本文介紹如何在D-Laravel上使用SQLServer,並且重build fpm image.
如果您不想重build,也可以直接使用如下檔案:
deviny/fpm:7.4.4odbc


D-Laravel啟動sqlserver
Dockerfile範本

FROM php:7.4.4-fpm
#Docker官方說明文件
#https://hub.docker.com/_/php/
RUN apt-get update && apt-get install -y \
                libfreetype6-dev \
                libjpeg62-turbo-dev \
                autoconf \
                libc-dev \
                pkg-config \
                libmcrypt-dev \
                libsnmp-dev \
                libsmi2-common \
                libsmi2-dev \
                libperl-dev \
                snmp \
                libpng-dev \
                ca-certificates \
                curl \
                xz-utils \
                sudo \
                cron \
                inotify-tools \
                git \
                wget \
                libmagickwand-dev \
                libldb-dev \
                libldap2-dev \
                libsasl2-dev \
                python \
                vim \
                unzip \
                mariadb-client \
                zip \
                libgeoip-dev \
                libpq-dev \
                libzip-dev \
                libbz2-dev \
                libgd-dev \
                libjpeg-dev \
                libgif-dev \
                libxml2-dev \
                apt-utils \
                gnupg \
                supervisor 
RUN docker-php-ext-install -j$(nproc) pdo_mysql mysqli ldap pgsql pdo_pgsql gettext sockets ctype xml zip pcntl bcmath intl gd \
                && docker-php-ext-configure gd 

#docker-php-ext-install 可安裝外掛大概如下:
#bcmath bz2 calendar ctype curl dba dom enchant exif fileinfo filter ftp gd gettext gmp iconv imap interbase intl json ldap mbstring mysqli oci8 odbc opcache pcntl pdo pdo_dblib pdo_firebird pdo_mysql pdo_oci pdo_odbc pdo_pgsql pdo_sqlite pgsql phar posix pspell readline recode reflection session shmop simplexml snmp soap sockets sodium spl standard sysvmsg sysvsem sysvshm tidy tokenizer wddx xml xmlreader xmlrpc xmlwriter xsl zend_test zip

# install ODBC driver
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/ubuntu/19.10/prod.list > /etc/apt/sources.list.d/mssql-release.list
ARG DEBIAN_FRONTEND=noninteractive
RUN export DEBIAN_FRONTEND=noninteractive && apt-get update && ACCEPT_EULA=Y apt-get install -y msodbcsql17 unixodbc-dev

RUN  pecl install redis \
     pecl install sqlsrv pdo_sqlsrv \
     pecl install xdebug \
     pecl install swoole \
     pecl install imagick 
     

#建立Dlaravel的使用者
RUN adduser --disabled-password --gecos "" dlaravel &&\
echo "dlaravel ALL=(root) NOPASSWD:ALL" > /etc/sudoers.d/dlaravel && \
chmod 0440 /etc/sudoers.d/dlaravel

#安裝composer
RUN EXPECTED_SIGNATURE=$(wget -q -O - https://composer.github.io/installer.sig); \
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"; \
ACTUAL_SIGNATURE=$(php -r "echo hash_file('SHA384', 'composer-setup.php');"); \
php composer-setup.php; \
php -r "unlink('composer-setup.php');"; \
mv composer.phar /usr/local/bin/composer; \ 
#加入dlaravel使用者
sudo -u dlaravel /usr/local/bin/composer global require "laravel/installer"; \
sudo -u dlaravel /usr/local/bin/composer global require "phpunit/phpunit=5.5.*"; \
sudo -u dlaravel echo 'export TERM=xterm-256color' >> /home/dlaravel/.bashrc; \
sudo -u dlaravel echo 'export PATH=/home/dlaravel/.composer/vendor/bin:/opt/mssql-tools/bin:$PATH' >> /home/dlaravel/.bashrc; \
#加入composer環境變數
echo 'export TERM=xterm-256color' >> /root/.bashrc; \
echo 'export PATH=/root/.composer/vendor/bin:$PATH' >> /root/.bashrc;  

EXPOSE 9000
USER dlaravel
CMD ["php-fpm"]
主要差異多了:
安裝gnupg
Docker File要安裝
gnugp

# install ODBC driver
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/ubuntu/19.10/prod.list > /etc/apt/sources.list.d/mssql-release.list
ARG DEBIAN_FRONTEND=noninteractive
RUN export DEBIAN_FRONTEND=noninteractive && apt-get update && ACCEPT_EULA=Y apt-get install -y msodbcsql17 unixodbc-dev


RUN pecl install sqlsrv pdo_sqlsrv 
文章來源:
https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15

docker-compose.yml新增sqlserver服務。
 sqlserver:
  image: mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04
  hostname: sqlserver
  ports:
    - "127.0.0.1:1433:1433"
  volumes:
    - ./mssql-data:/var/opt/mssql/data
  environment:
    - ACCEPT_EULA=Y
    - SA_PASSWORD=yourStrong(!)Password
    - MSSQL_PID=Express
  networks:
    - dlaravel_net
sqlserver的環境變數,您可參考docker hub官方image說明
https://hub.docker.com/_/microsoft-mssql-server?tab=description

在etc/php/dlarave.ini中加入
priority=20
extension=sqlsrv.so
priority=30
extension=pdo_sqlsrv.so
重啟D-Laravel可以測試看看囉。
如果這時,使用./console ext,會發現多了sqlsrv及pdo_sqlsrv了。


直接使用./console指令,進入容器。然後,先來隨便建個test資料庫吧。


進入容器後,執行指令:
sqlcmd -S sqlserver -U sa -P 'yourStrong(!)Password'
為何上方-S的參數是用sqlserver,因為我們把docker-compose.yml中定義了SQLServer的服務名稱是sqlserver。

PHP 範本
<?php
$serverName = "tcp:sqlserver,1433";
$connectionInfo = array( "Database"=>"test", "UID"=>"sa", "PWD"=>"yourStrong(!)Password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     echo print_r( sqlsrv_errors(), true);
}
沒問題,成功連到啦:)


我們可以自己建立一個sqlserver.yml在service目錄
version: '3.6'
services:
 sqlserver:
  image: mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04
  hostname: sqlserver
  ports:
    - "127.0.0.1:1433:1433"
  environment:
    - ACCEPT_EULA=Y
    - SA_PASSWORD=yourStrong(!)Password
    - MSSQL_PID=Express
  networks:
    - dlaravel_net

networks:
    dlaravel_net:
注意!!SA密碼太簡單會造成sqlserver啟動失敗,這裡來個簡單的範例,我密碼設定為easy-password,指令如下,看看會吐出什麼 錯誤:
docker run --rm  -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=easy-password'  mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04


拉回正題,有了sqlserver.yml後,就能使用D-Laravel的dotenv啟動該服務哦
.env放在D-Laravel的目錄下,這裡額外啟動redis及sqlserver服務。
LARAVEL_INSTALLER='container'
#Choose service file from services folder, but without extension name.
DOCKER_SERVICES='redis sqlserver'
執行./console restart重啟後,再使用./console ps來瞧瞧。


最後,如何建立console別名c可用./console alias查看。
例如,加到bash_profile中就能在任意地方,使用c別名執行./console命令囉:)。




 

Tags: docker sqlserver

Devin Yang

文章內容無法一一說明,如果您有什麼不了解處,歡迎提問哦:)

No Comment

Post your comment

需要登入才可留言!

類似的文章


docker,phpenv

在Linux上ping不到host.docker.internal

可用docker version檢查,確認版本在Docker v20.10+即可加入extra_hosts如下:

docker

Devin Yang的Docker初學筆記

這是我初學Docker時的筆記,有點粗糙,有調整了一下下。 但我想對於Docker初學者,應該可以提供一些初步的概念, 歡迎參考看看。

docker

聞聊我的網路的演變史

聞聊我的網路的演變史。 我的GCP,每月要$46.35美元,目前免費試用餘額還剩$111.05美元, $300元差不多快被我花光了,差不多要搬回自管理的主機了。