建立於: 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命令囉:)。
No Comment
Post your comment