by Devin Yang
(This article was automatically translated.)

Published - 4 years ago ( Updated - 4 years ago )

This article introduces how to use SQLServer on D-Laravel and rebuild fpm image.
If you don't want to rebuild, you can also directly use the archive like this:
deviny/fpm:7.4.4odbc


D-Laravel starts sqlserver
Dockerfile template

FROM php:7.4.4-fpm
#Docker Official Documentation
#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 The plug-ins that can be installed are roughly as follows:
#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


#Create a Dlaravel user
RUN adduser --disabled-password --gecos "" dlaravel &&\
echo "dlaravel ALL=(root) NOPASSWD:ALL" > /etc/sudoers.d/dlaravel && \
chmod 0440 /etc/sudoers.d/dlaravel

#install 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; \
#Join dlaravel user
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; \
#Add composer environment variable
echo 'export TERM=xterm-256color' >> /root/.bashrc; \
echo 'export PATH=/root/.composer/vendor/bin:$PATH' >> /root/.bashrc;

EXPOSE 9000
USER draravel
CMD ["php-fpm"]
There are more major differences:
install 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
Article Source:
https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15

Added sqlserver service in docker-compose.yml.
 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
For the environment variables of sqlserver, you can refer to the official image description of docker hub
https://hub.docker.com/_/microsoft-mssql-server?tab=description

Add in etc/php/dlarave.ini
priority=20
extension=sqlsrv.so
priority=30
extension=pdo_sqlsrv.so
Restart D-Laravel to test it.
If you use ./console ext at this time, you will find that there are more sqlsrv and pdo_sqlsrv.


Directly use the ./console command to enter the container. Then, let’s build a test database casually.


After entering the container, execute the command:
sqlcmd -S sqlserver -U sa -P 'yourStrong(!)Password'
Why the above -S parameter is to use sqlserver, because we defined the service name of SQLServer in docker-compose.yml as sqlserver.

PHP template
"test", "UID"=>"sa", "PWD"=>"yourStrong(!)Password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
echo "Connection established.
"; }else{ echo "Connection could not be established.
"; echo print_r( sqlsrv_errors(), true); }
No problem, connected successfully :)


We can create a sqlserver.yml in the service directory by ourselves
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:
Note!! If the SA password is too simple, it will cause sqlserver to fail to start. Here is a simple example. My password is set to easy-password. The command is as follows to see what error will be spit out:
docker run --rm  -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=easy-password'  mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04


Back to the topic, with sqlserver.yml, you can use D-Laravel's dotenv to start the service
.env is placed in the D-Laravel directory, and the redis and sqlserver services are additionally started here.
LARAVEL_INSTALLER='container'
#Choose service file from services folder, but without extension name.
DOCKER_SERVICES='redis sqlserver'
Execute ./console restart to restart, and then use ./console ps to see.


Finally, how to create a console alias c can be viewed with ./console alias.
For example, by adding it to bash_profile, you can use the c alias to execute the ./console command anywhere :).




 

Tags: docker sqlserver

Devin Yang

Feel free to ask me, if you don't get it.:)

No Comment

Post your comment

Login is required to leave comments

Similar Stories


docker, tinkerwell, tinker, laravel

Application of Tinkerwell and docker environment

In fact, I don't use Tinkerwell recently because it keeps costing me money to update. If you want to test the direct ssh host, it’s done, isn’t it?

docker

Wenchat about the evolution history of my network

Hear about the evolution of my network. My GCP costs $46.35 per month, and the current free trial balance is $111.05, I've almost spent $300, and I'm about to move back to a self-managed host.

docker

About how to change the owner of FPM image on GCE

The CCC website has been upgraded to php 7.2.0. With a Docker environment, it is easy to upgrade php. There are still some precautions you should know about the change of Image on Linux. If you install D-Laravel on a Linux system through docker like me, when you change a different image, Perhaps you may encounter, doubt! Why composer does not have permission to write to the folder error. directory is not writable