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

Commit 2e31d04

Browse files
authored
Adding MariaDB support to C++ Dev Container (#1241)
1 parent 7c6917c commit 2e31d04

File tree

15 files changed

+667
-0
lines changed

15 files changed

+667
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
MARIADB_ROOT_PASSWORD=mariadb
2+
MARIADB_DATABASE=mariadb
3+
MARIADB_USER=mariadb
4+
MARIADB_PASSWORD=mariadb
5+
MARIADB_HOSTNAME=localhost
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# [Choice] Debian / Ubuntu version (use Debian 11/9, Ubuntu 18.04/21.04 on local arm64/Apple Silicon): debian-11, debian-10, debian-9, ubuntu-21.04, ubuntu-20.04, ubuntu-18.04
2+
ARG VARIANT=debian-11
3+
FROM mcr.microsoft.com/vscode/devcontainers/cpp:0-${VARIANT}
4+
5+
# Everything below this is needed for installing MariaDB
6+
# Instructions are copied and modified from: https://mariadb.com/docs/clients/mariadb-connectors/connector-cpp/install/
7+
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
8+
&& apt-get -y install curl
9+
10+
COPY ./install-mariadb.sh /
11+
RUN chmod +x /install-mariadb.sh && ./install-mariadb.sh
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "C++ and MariaDB",
3+
"dockerComposeFile": "docker-compose.yml",
4+
"service": "app",
5+
"workspaceFolder": "/workspace",
6+
7+
// Set *default* container specific settings.json values on container create.
8+
"settings": {},
9+
10+
// Add the IDs of extensions you want installed when the container is created.
11+
"extensions": [
12+
"ms-vscode.cpptools",
13+
"ms-vscode.cmake-tools",
14+
"ms-vscode.cpptools-extension-pack",
15+
],
16+
17+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
18+
// This is the recommended way to access the container from the host
19+
// "forwardPorts": [3306],
20+
21+
// Use 'postCreateCommand' to run commands after the container is created.
22+
// "postCreateCommand": "gcc -v",
23+
24+
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
25+
"remoteUser": "vscode"
26+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
version: '3.8'
2+
3+
volumes:
4+
mariadb-data:
5+
6+
services:
7+
app:
8+
build:
9+
context: .
10+
dockerfile: Dockerfile
11+
args:
12+
# Update 'VARIANT' to pick a version of CPP
13+
# See the README for more information on available versions.
14+
VARIANT: debian-11
15+
env_file:
16+
- .env
17+
18+
# Security Opt and cap_add allow for C++ based debuggers to work.
19+
# See `runArgs`: https://github.com/Microsoft/vscode-docs/blob/main/docs/remote/devcontainerjson-reference.md
20+
# security_opt:
21+
# - seccomp:unconfined
22+
# cap_add:
23+
# - SYS_PTRACE
24+
25+
volumes:
26+
- ..:/workspace:cached
27+
28+
# Overrides default command so things don't shut down after the process ends.
29+
command: sleep infinity
30+
31+
# Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
32+
network_mode: service:db
33+
34+
# Uncomment the next line to use a non-root user for all processes.
35+
# user: vscode
36+
37+
# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
38+
# (Adding the "ports" property to this file will not forward from a Codespace.)
39+
40+
db:
41+
image: mariadb:10.3.32-focal
42+
restart: unless-stopped
43+
volumes:
44+
- mariadb-data:/var/lib/MARIADB
45+
env_file:
46+
- .env
47+
# Add "forwardPorts": ["3306"] to **devcontainer.json** to forward DB locally.
48+
# (Adding the "ports" property to this file will not forward from a Codespace.)
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/bin/bash
2+
set -e
3+
4+
OSURL=""
5+
OSTAG=""
6+
7+
find_os_props() {
8+
. /etc/os-release
9+
case $ID in
10+
debian)
11+
case $VERSION_CODENAME in
12+
stretch)
13+
OSTAG="1683458"
14+
OSURL="debian-9-stretch-amd64"
15+
;;
16+
*)
17+
OSTAG="1683461"
18+
OSURL="debian-buster-amd64"
19+
;;
20+
esac
21+
;;
22+
ubuntu)
23+
case $VERSION_CODENAME in
24+
bionic)
25+
OSTAG="1683439"
26+
OSURL="ubuntu-bionic-amd64"
27+
;;
28+
groovy)
29+
OSTAG="1683454"
30+
OSURL="ubuntu-groovy-amd64"
31+
;;
32+
*)
33+
OSTAG="1683444"
34+
OSURL="ubuntu-focal-amd64"
35+
;;
36+
esac
37+
;;
38+
*)
39+
echo "Unsupported OS choice."
40+
exit 1
41+
;;
42+
esac
43+
}
44+
45+
MARIADB_REPO_SETUP=mariadb_repo_setup
46+
MARIADB_REPO_SETUP_SHA256=mariadb_repo_setup.sha256
47+
TMP_DIR=$(mktemp -d -t maria-XXXXXXXXXX)
48+
MARIADB_CONNECTOR=""
49+
50+
cleanup() {
51+
EXIT_CODE=$?
52+
set +e
53+
if [[ -n ${TMP_DIR} ]]; then
54+
cd /
55+
rm -rf ${TMP_DIR}
56+
fi
57+
exit $EXIT_CODE
58+
}
59+
trap cleanup EXIT
60+
61+
#Set up external repository and install C Connector
62+
cd ${TMP_DIR}
63+
curl -Ls "https://downloads.mariadb.com/MariaDB/${MARIADB_REPO_SETUP}" -o ${MARIADB_REPO_SETUP}
64+
curl -Ls "https://downloads.mariadb.com/MariaDB/${MARIADB_REPO_SETUP_SHA256}" -o ${MARIADB_REPO_SETUP_SHA256}
65+
sha256sum -c ${MARIADB_REPO_SETUP_SHA256}
66+
chmod +x ${MARIADB_REPO_SETUP}
67+
./${MARIADB_REPO_SETUP} --mariadb-server-version="mariadb-10.6"
68+
apt install -y libmariadb3 libmariadb-dev
69+
70+
#Depending on the OS, install different C++ connectors
71+
find_os_props
72+
# Instructions are copied and modified from: https://mariadb.com/docs/clients/mariadb-connectors/connector-cpp/install/
73+
MARIADB_CONNECTOR=mariadb-connector-cpp-1.0.1-$OSURL
74+
curl -Ls https://dlm.mariadb.com/$OSTAG/connectors/cpp/connector-cpp-1.0.1/${MARIADB_CONNECTOR}.tar.gz -o ${MARIADB_CONNECTOR}.tar.gz
75+
tar -xvzf ${MARIADB_CONNECTOR}.tar.gz && cd ${MARIADB_CONNECTOR}
76+
install -d /usr/include/mariadb/conncpp
77+
78+
#Header Files being copied into the necessary directories
79+
cp -R ./include/mariadb/* /usr/include/mariadb/
80+
cp -R ./include/mariadb/conncpp/* /usr/include/mariadb/conncpp
81+
cp -R ./include/mariadb/conncpp/compat/* /usr/include/mariadb/conncpp/compat
82+
83+
install -d /usr/lib/mariadb
84+
install -d /usr/lib/mariadb/plugin
85+
86+
#Shared libraries copied into usr/lib
87+
cp lib/mariadb/libmariadbcpp.so /usr/lib
88+
cp -R lib/mariadb/plugin/* /usr/lib/mariadb/plugin

containers/cpp-mariadb/.gitignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Compiled Object files
5+
*.slo
6+
*.lo
7+
*.o
8+
*.obj
9+
10+
# Precompiled Headers
11+
*.gch
12+
*.pch
13+
14+
# Compiled Dynamic libraries
15+
*.so
16+
*.dylib
17+
*.dll
18+
19+
# Fortran module files
20+
*.mod
21+
*.smod
22+
23+
# Compiled Static libraries
24+
*.lai
25+
*.la
26+
*.a
27+
*.lib
28+
29+
# Executables
30+
*.exe
31+
*.out
32+
*.app
33+
34+
# build dir
35+
build/

containers/cpp-mariadb/.npmignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
README.md
2+
test-project
3+
history
4+
definition-manifest.json
5+
library-scripts
6+
.vscode
7+
.npmignore
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch Main",
9+
"type": "cppdbg",
10+
"request": "launch",
11+
"program": "${workspaceFolder}/test-project/main.out",
12+
"args": [],
13+
"stopAtEntry": false,
14+
"cwd": "${workspaceFolder}/test-project",
15+
"environment": [],
16+
"externalConsole": false,
17+
"MIMode": "gdb",
18+
"setupCommands": [
19+
{
20+
"description": "Enable pretty-printing for gdb",
21+
"text": "-enable-pretty-printing",
22+
"ignoreFailures": true
23+
}
24+
],
25+
"preLaunchTask": "Build Main"
26+
}
27+
]
28+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"files.associations": {
3+
"*.psm": "powershell",
4+
"iostream": "cpp",
5+
"array": "cpp",
6+
"bit": "cpp",
7+
"*.tcc": "cpp",
8+
"cctype": "cpp",
9+
"clocale": "cpp",
10+
"cmath": "cpp",
11+
"compare": "cpp",
12+
"concepts": "cpp",
13+
"cstdint": "cpp",
14+
"cstdio": "cpp",
15+
"cstdlib": "cpp",
16+
"cwchar": "cpp",
17+
"cwctype": "cpp",
18+
"deque": "cpp",
19+
"list": "cpp",
20+
"map": "cpp",
21+
"unordered_map": "cpp",
22+
"vector": "cpp",
23+
"exception": "cpp",
24+
"functional": "cpp",
25+
"initializer_list": "cpp",
26+
"iosfwd": "cpp",
27+
"istream": "cpp",
28+
"limits": "cpp",
29+
"memory": "cpp",
30+
"new": "cpp",
31+
"optional": "cpp",
32+
"ostream": "cpp",
33+
"ranges": "cpp",
34+
"stdexcept": "cpp",
35+
"streambuf": "cpp",
36+
"string": "cpp",
37+
"string_view": "cpp",
38+
"system_error": "cpp",
39+
"type_traits": "cpp",
40+
"tuple": "cpp",
41+
"typeinfo": "cpp",
42+
"utility": "cpp"
43+
},
44+
"cmake.sourceDirectory": "${workspaceRoot}/test-project"
45+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"label": "Build Main",
8+
"type": "shell",
9+
"command": " cd test-project && g++ -g main.cpp -o main.out -lmariadbcpp",
10+
"group": {
11+
"kind": "build",
12+
"isDefault": true
13+
}
14+
}
15+
]
16+
}

0 commit comments

Comments
 (0)