本文介绍如何在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