Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

Added devcontainer for PHP and MS SQL #1451

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions containers/php-mssql/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# [Choice] PHP version (use -bullseye variants on local arm64/Apple Silicon): 8, 8.1, 8.0, 7, 7.4, 7.3, 8-bullseye, 8.1-bullseye, 8.0-bullseye, 7-bullseye, 7.4-bullseye, 7.3-bullseye, 8-buster, 8.1-buster, 8.0-buster, 7-buster, 7.4-buster
ARG VARIANT=8-bullseye
FROM mcr.microsoft.com/vscode/devcontainers/php:${VARIANT}

# [Choice] Node.js version: none, lts/*, 16, 14, 12, 10
ARG NODE_VERSION="none"
RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi

# Install MS SQL ODBC
# Ref: https://docs.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-ver15#installing-on-debian
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN rel=$(lsb_release -r -s) && curl https://packages.microsoft.com/config/debian/$rel/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& ACCEPT_EULA=Y apt-get -y install --no-install-recommends msodbcsql18 mssql-tools18 unixodbc-dev libgssapi-krb5-2

# Instal PHP MS SQL Drivers
# Ref: https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15#debian18
RUN pecl install sqlsrv && pecl install pdo_sqlsrv
RUN printf "; priority=20\nextension=sqlsrv.so\n" > /usr/local/etc/php/conf.d/020-sqlsrv.ini \
&& printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /usr/local/etc/php/conf.d/030-pdo_sqlsrv.ini

ENV PATH="$PATH:/opt/mssql-tools18/bin"

# [Optional] Uncomment this line to install global node packages.
# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1

43 changes: 43 additions & 0 deletions containers/php-mssql/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "PHP & MSSQL (Community)",
"dockerComposeFile": "docker-compose.yml",
"service": "app",
"workspaceFolder": "/workspace",

// Set *default* container specific settings.json values on container create.
"settings": {
"php.validate.executablePath": "/usr/local/bin/php",
"mssql.connections": [
{
"server": "localhost",
"database": "",
"authenticationType": "SqlLogin",
"user": "sa",
"password": "A_STR0NG_Passw0rd!",
"emptyPasswordInput": false,
"savePassword": true,
"profileName": "php-mssql-container"
}
]
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"xdebug.php-debug",
"ms-mssql.mssql"
],

// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [ 8080, 1433 ],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "sudo chmod a+x \"$(pwd)\" && sudo rm -rf /var/www/html && sudo ln -s \"$(pwd)\" /var/www/html"

// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode",

// Supported features
"features": {
"github-cli": "latest",
"azure-cli": "latest"
}
}
44 changes: 44 additions & 0 deletions containers/php-mssql/.devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
version: '3.8'

services:
app:
build:
context: .
dockerfile: Dockerfile
args:
# Update 'VARIANT' to pick a version of PHP version: 8, 8.1, 8.0, 7, 7.4
# Append -bullseye or -buster to pin to an OS version.
# Use -bullseye variants on local arm64/Apple Silicon.
VARIANT: "8.1-bullseye"
# Optional Node.js version
NODE_VERSION: "none"


volumes:
- ..:/workspace:cached

# Overrides default command so things don't shut down after the process ends.
command: sleep infinity

# Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
network_mode: service:db

# Uncomment the next line to use a non-root user for all processes.
# user: vscode

# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
# (Adding the "ports" property to this file will not forward from a Codespace.)

db:
image: mcr.microsoft.com/mssql/server:2019-latest
restart: unless-stopped
ports:
- "1433:1433"
volumes:
- mssql_volume:/var/opt/mssql
environment:
ACCEPT_EULA: Y
SA_PASSWORD: A_STR0NG_Passw0rd!

volumes:
mssql_volume:
7 changes: 7 additions & 0 deletions containers/php-mssql/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
README.md
test-project
history
definition-manifest.json
.devcontainer/library-scripts
.vscode
.npmignore
48 changes: 48 additions & 0 deletions containers/php-mssql/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Built-in web server",
"type": "php",
"request": "launch",
"runtimeArgs": [
"-dxdebug.mode=debug",
"-dxdebug.start_with_request=yes",
"-S",
"localhost:0"
],
"program": "",
"cwd": "${workspaceRoot}",
"port": 9000,
"serverReadyAction": {
"pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
"uriFormat": "http://localhost:%s/test-project/sqlsrv.php",
"action": "openExternally"
}
},
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9000
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 0,
"runtimeArgs": [
"-dxdebug.start_with_request=yes"
],
"env": {
"XDEBUG_MODE": "debug,develop",
"XDEBUG_CONFIG": "client_port=${port}"
}
}
]
}
66 changes: 66 additions & 0 deletions containers/php-mssql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# PHP & Azure SQL/SQL Server (Community)

## Summary

*Develop PHP based applications using Azure SQL DB, Azure SQL MI or SQL Server. Includes needed tools, extensions, dependencies and samples*

| Metadata | Value |
|----------|-------|
| *Contributors* | [Davide Mauri]() |
| *Categories* | Community, Languages |
| *Definition type* | Docker Compose |
| *Published images* | mcr.microsoft.com/vscode/devcontainers/php |
| *Container host OS support* | Linux, macOS, Windows |
| *Container OS* | Debian |
| *Languages, platforms* | PHP, MS SQL Server |

## Description

This definition creates two containers, one for PHP and one for Microsoft SQL Server (MSSQL). Code will attach to the PHP container, and from within that container the MSSQL container will be available on localhost port 1433. The MSSQL instance can be managed from the container's command line with:

```
sqlcmd -S localhost -U sa -P A_STR0NG_Passw0rd! -C
```

Or connecting with [Microsoft SQL Server Management Studio](https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms) or [Azure Data Studio](https://docs.microsoft.com/en-us/sql/azure-data-studio/download-azure-data-studio) or the [MSSQL Server VS Code Extension](https://marketplace.visualstudio.com/items?itemName=ms-mssql.mssql). The VS Code Extension is automatically installed and configured for accessing the Microsoft SQL Server instance running in the dedicated container. Look for the profile `php-mssql-container` in the SQL Server tab.

## Using this definition

This definition has been created using the PHP defintion as starting point. It has been tested using the `8.1-bullseye` variant (Debian Buster and PHP 8.1.). Please refer to the PHP devcontainer repository for any additional information on how to customize the image:

https://github.com/microsoft/vscode-dev-containers/tree/main/containers/php


## Testing the definition

This definition includes some test code that will help you verify it is working as expected on your system. Follow these steps:

1. If this is your first time using a development container, please follow the [getting started steps](https://aka.ms/vscode-remote/containers/getting-started) to set up your machine.
2. Clone this repository.
3. Start VS Code, press <kbd>F1</kbd>, and select **Remote-Containers: Open Folder in Container...**
4. Select the `containers/php` folder.
5. After the folder has opened in the container, press <kbd>F5</kbd> to start the project.
6. You should see "Hello remote world!" in the Debug Console after the program executes.
7. From here, you can add breakpoints or edit the contents of the `test-project` folder to do further testing.

## Testing PHP and SQL Server

The defintion include couple of files to test that PHP can successfuly connect and query SQL Server or Azure SQL. The files are in the `test-project` folder: `sqlsrv.php` and `sqlsrvdpo.php`.

They will both try to connect to the MS SQL Server running in the container and will query the server version. To test if everything works you can press F5 so that the debug configuration `Launch Built-in web server` will be launched. It will open a browser and automatically navigate to:

http://localhost:37135/test-project/sqlsrv.php

to check the everything working. If it does you'll see something like:

```
Microsoft SQL Server 2019 (RTM-CU15) (KB5008996) - 15.0.4198.2 (X64) Jan 12 2022 22:30:08 Copyright (C) 2019 Microsoft Corporation Developer Edition (64-bit) on Linux (Ubuntu 20.04.3 LTS)
```

You can also set breakpoints, as dubugging is supported via XDebug.

## License

Copyright (c) Microsoft Corporation. All rights reserved.

Licensed under the MIT License. See [LICENSE](https://github.com/microsoft/vscode-dev-containers/blob/main/LICENSE).
13 changes: 13 additions & 0 deletions containers/php-mssql/test-project/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/*-------------------------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
*-------------------------------------------------------------------------------------------------------------*/

function hello($name) {
echo "Hello $name!";
}

hello('remote world');

?>
3 changes: 3 additions & 0 deletions containers/php-mssql/test-project/phpinfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
phpinfo();
?>
58 changes: 58 additions & 0 deletions containers/php-mssql/test-project/sqlsrv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
$serverName = "localhost";
$connectionOptions = array(
"database" => "tempdb",
"uid" => "sa",
"pwd" => "A_STR0NG_Passw0rd!",
"TrustServerCertificate" => "1"
);

function exception_handler($exception) {
echo "<h1>Failure</h1>";
echo "Uncaught exception: " , $exception->getMessage();
echo "<h1>PHP Info for troubleshooting</h1>";
phpinfo();
}

set_exception_handler('exception_handler');

// Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
if ($conn === false) {
die(formatErrors(sqlsrv_errors()));
}

// Select Query
$tsql = "SELECT @@Version AS SQL_VERSION";

// Executes the query
$stmt = sqlsrv_query($conn, $tsql);

// Error handling
if ($stmt === false) {
die(formatErrors(sqlsrv_errors()));
}
?>

<h1> Azure SQL / SQL Server Connection : </h1>

<?php
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
echo $row['SQL_VERSION'] . PHP_EOL;
}

sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);

function formatErrors($errors)
{
// Display errors
echo "<h1>SQL Error:</h1>";
echo "Error information: <br/>";
foreach ($errors as $error) {
echo "SQLSTATE: ". $error['SQLSTATE'] . "<br/>";
echo "Code: ". $error['code'] . "<br/>";
echo "Message: ". $error['message'] . "<br/>";
}
}
?>
24 changes: 24 additions & 0 deletions containers/php-mssql/test-project/sqlsrvpdo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
function exception_handler($exception) {
echo "<h1>Failure</h1>";
echo "Uncaught exception: " , $exception->getMessage();
#echo "<h1>PHP Info for troubleshooting</h1>";
#phpinfo();
}

set_exception_handler('exception_handler');

?>

<h1> Azure SQL / SQL Server Connection : </h1>

<?php
$conn = new PDO("sqlsrv:server=localhost; Database=tempdb; TrustServerCertificate=true", "sa", "A_STR0NG_Passw0rd!");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "SELECT @@Version AS SQL_VERSION";
$stmt = $conn->query( $query );

while ($row = $stmt->fetch( PDO::FETCH_ASSOC )) {
echo $row['SQL_VERSION'] . PHP_EOL;
}
?>
Loading