I have a Docker container built from the following docker-compose.yml:
version: "3"
services:
nginx:
build:
context: .
dockerfile: nginx/Dockerfile
ports:
- "8080:80"
networks:
- internal
volumes:
- ./data/:/var/www/html/
- ./logs/nginx:/var/log/nginx/
php:
build:
context: .
dockerfile: php/Dockerfile
networks:
- internal
volumes:
- ./data/:/var/www/html/
- ./logs/php.log:/var/log/fpm-php.www.log
networks:
internal:
driver: bridge
Here is my php/Docker file:
FROM php:fpm-alpine
ENV ACCEPT_EULA=Y
# Install prerequisites required for tools and extensions installed later on.
RUN apk add --update bash gnupg less libpng-dev libzip-dev nano nodejs npm openssl su-exec unzip curl nginx supervisor
# Install yarn as global npm package.
RUN npm install -g yarn
# Install prerequisites for the sqlsrv and pdo_sqlsrv PHP extensions.
RUN curl -O https://download.microsoft.com/download/8/6/8/868e5fc4-7bfe-494d-8f9d-115cbcdb52ae/msodbcsql18_18.1.2.1-1_amd64.apk \
&& curl -O https://download.microsoft.com/download/8/6/8/868e5fc4-7bfe-494d-8f9d-115cbcdb52ae/mssql-tools18_18.1.1.1-1_amd64.apk \
&& curl -O https://download.microsoft.com/download/8/6/8/868e5fc4-7bfe-494d-8f9d-115cbcdb52ae/msodbcsql18_18.1.2.1-1_amd64.sig \
&& curl -O https://download.microsoft.com/download/8/6/8/868e5fc4-7bfe-494d-8f9d-115cbcdb52ae/mssql-tools18_18.1.1.1-1_amd64.sig \
&& curl https://packages.microsoft.com/keys/microsoft.asc | gpg --import - \
&& gpg --verify msodbcsql18_18.1.2.1-1_amd64.sig msodbcsql18_18.1.2.1-1_amd64.apk \
&& gpg --verify mssql-tools18_18.1.1.1-1_amd64.sig mssql-tools18_18.1.1.1-1_amd64.apk \
&& apk add --allow-untrusted msodbcsql18_18.1.2.1-1_amd64.apk mssql-tools18_18.1.1.1-1_amd64.apk \
&& rm *.apk *.sig
# Retrieve the script used to install PHP extensions from the source container.
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/install-php-extensions
# Install required PHP extensions and all their prerequisites available via apt.
RUN chmod uga+x /usr/bin/install-php-extensions \
&& sync \
&& install-php-extensions bcmath ds exif gd intl opcache pcntl pcov pdo_sqlsrv redis sqlsrv zip
# Downloading composer and marking it as executable.
RUN curl -o /usr/local/bin/composer https://getcomposer.org/composer-stable.phar \
&& chmod +x /usr/local/bin/composer
# Setting the work directory.
WORKDIR /var/www/html
Here is my PHP code:
<?php
try {
$conn = new PDO("sqlsrv:Server=$serverName;Database=$database", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->query("SELECT * FROM MyTable");
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($results as $row) {
echo "ID: " . $row['id'] . "<br>";
}
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
When I run docker-compose up everything starts as expected. However, when I try to run a simple query to SQL Server, I get this error:
Error: SQLSTATE[01000]: [unixODBC][Driver Manager]Can't open lib '/opt/microsoft/msodbcsql18/lib64/libmsodbcsql-18.1.so.2.1' : file not found
I've confirmed that the file is there:
81f9341151fb:/# ls -hald /opt/microsoft/msodbcsql18/lib64/libmsodbcsql-18.1.so.2.1
-rwxr-xr-x 1 1001 1001 2.0M Oct 26 2022 /opt/microsoft/msodbcsql18/lib64/libmsodbcsql-18.1.so.2.1
The problem was the specified architecture of
msodbcsqlandmssql-tollspackages. I'm running this on an M1 MacBook Pro. I needed to find thearm64packages. Here's the updated part of the Dockerfile: