Skip to content

Commit b2cd42c

Browse files
committed
Add devcontainer for github code-spaces.
1 parent c8051a6 commit b2cd42c

File tree

6 files changed

+290
-0
lines changed

6 files changed

+290
-0
lines changed

.devcontainer/Dockerfile

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Update the VARIANT arg in devcontainer.json to pick a Java version >= 11
2+
ARG VARIANT=11
3+
FROM openjdk:${VARIANT}-jdk-buster
4+
5+
# Options for setup script
6+
ARG INSTALL_ZSH="true"
7+
ARG UPGRADE_PACKAGES="false"
8+
ARG USERNAME=vscode
9+
ARG USER_UID=1000
10+
ARG USER_GID=$USER_UID
11+
12+
# Install needed packages and setup non-root user. Use a separate RUN statement to add your own dependencies.
13+
COPY library-scripts/*.sh /tmp/library-scripts/
14+
RUN apt-get update \
15+
&& /bin/bash /tmp/library-scripts/common-debian.sh "${INSTALL_ZSH}" "${USERNAME}" "${USER_UID}" "${USER_GID}" "${UPGRADE_PACKAGES}" \
16+
&& apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/* /tmp/library-scripts
17+
18+
# [Optional] Install Maven
19+
ARG INSTALL_MAVEN="false"
20+
ARG MAVEN_VERSION=3.6.3
21+
ARG MAVEN_DOWNLOAD_SHA="dev-mode"
22+
ENV MAVEN_HOME /usr/share/maven \
23+
MAVEN_CONFIG /root/.m2
24+
COPY maven-settings.xml /usr/share/maven/ref/
25+
RUN if [ "${INSTALL_MAVEN}" = "true" ]; then \
26+
mkdir -p /usr/share/maven /usr/share/maven/ref \
27+
&& curl -fsSL -o /tmp/apache-maven.tar.gz https://archive.apache.org/dist/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
28+
&& ([ "${MAVEN_DOWNLOAD_SHA}" = "dev-mode" ] || echo "${MAVEN_DOWNLOAD_SHA} */tmp/apache-maven.tar.gz" | sha512sum -c - ) \
29+
&& tar -xzf /tmp/apache-maven.tar.gz -C /usr/share/maven --strip-components=1 \
30+
&& rm -f /tmp/apache-maven.tar.gz \
31+
&& ln -s /usr/share/maven/bin/mvn /usr/bin/mvn; \
32+
fi
33+
34+
# [Optional] Install Gradle
35+
ARG INSTALL_GRADLE="false"
36+
ARG GRADLE_VERSION=5.4.1
37+
ARG GRADLE_DOWNLOAD_SHA="dev-mode"
38+
ENV GRADLE_HOME=/opt/gradle
39+
RUN if [ "${INSTALL_GRADLE}" = "true" ]; then \
40+
curl -sSL --output gradle.zip "https://services.gradle.org/distributions/gradle-${GRADLE_VERSION}-bin.zip" \
41+
&& ([ "${GRADLE_DOWNLOAD_SHA}" = "dev-mode" ] || echo "${GRADLE_DOWNLOAD_SHA} *gradle.zip" | sha256sum --check - ) \
42+
&& unzip gradle.zip \
43+
&& rm gradle.zip \
44+
&& mv "gradle-${GRADLE_VERSION}" "${GRADLE_HOME}/" \
45+
&& ln -s "${GRADLE_HOME}/bin/gradle" /usr/bin/gradle; \
46+
fi
47+
48+
# Allow for a consistant java home location for settings - image is changing over time
49+
RUN if [ ! -d "/docker-java-home" ]; then ln -s "${JAVA_HOME}" /docker-java-home; fi
50+
51+
# [Optional] Install Node.js for use with web applications - update the INSTALL_NODE arg in devcontainer.json to enable.
52+
ARG INSTALL_NODE="false"
53+
ARG NODE_VERSION="lts/*"
54+
ENV NVM_DIR=/usr/local/share/nvm \
55+
NVM_SYMLINK_CURRENT=true \
56+
PATH=${NVM_DIR}/current/bin:${PATH}
57+
COPY library-scripts/node-debian.sh /tmp/library-scripts/
58+
RUN if [ "$INSTALL_NODE" = "true" ]; then \
59+
/bin/bash /tmp/library-scripts/node-debian.sh "${NVM_DIR}" "${NODE_VERSION}" "${USERNAME}" \
60+
&& apt-get clean -y && rm -rf /var/lib/apt/lists/*; \
61+
fi \
62+
&& rm -rf /tmp/library-scripts
63+
64+
# [Optional] Uncomment this section to install additional OS packages.
65+
# RUN apt-get update \
66+
# && export DEBIAN_FRONTEND=noninteractive \
67+
# && apt-get -y install --no-install-recommends <your-package-list-here>

.devcontainer/devcontainer.json

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "Java",
3+
"build": {
4+
"dockerfile": "Dockerfile",
5+
"args": {
6+
// Update the VARIANT arg to pick a Java version >= 11
7+
"VARIANT": "11",
8+
// Options to install Maven or Gradle
9+
"INSTALL_MAVEN": "true",
10+
"MAVEN_VERSION": "3.6.3",
11+
"MAVEN_DOWNLOAD_SHA": "c35a1803a6e70a126e80b2b3ae33eed961f83ed74d18fcd16909b2d44d7dada3203f1ffe726c17ef8dcca2dcaa9fca676987befeadc9b9f759967a8cb77181c0",
12+
"INSTALL_GRADLE": "true",
13+
"GRADLE_VERSION": "5.4.1",
14+
"GRADLE_DOWNLOAD_SHA": "7bdbad1e4f54f13c8a78abc00c26d44dd8709d4aedb704d913fb1bb78ac025dc",
15+
"INSTALL_NODE": "false",
16+
"NODE_VERSION": "lts/*"
17+
}
18+
},
19+
20+
// Set *default* container specific settings.json values on container create.
21+
"settings": {
22+
"terminal.integrated.shell.linux": "/bin/bash",
23+
"java.home": "/docker-java-home"
24+
},
25+
26+
// Add the IDs of extensions you want installed when the container is created.
27+
"extensions": [
28+
"vscjava.vscode-java-pack"
29+
]
30+
31+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
32+
// "forwardPorts": [],
33+
34+
// Use 'postCreateCommand' to run commands after the container is created.
35+
// "postCreateCommand": "java -version",
36+
37+
// Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
38+
// "remoteUser": "vscode"
39+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Warning: Folder contents may be replaced
2+
3+
The contents of this folder will be automatically replaced with a file of the same name in the [vscode-dev-containers](https://github.com/microsoft/vscode-dev-containers) repository's [script-library folder](https://github.com/microsoft/vscode-dev-containers/tree/master/script-library) whenever the repository is packaged.
4+
5+
To retain your edits, move the file to a different location. You may also delete the files if they are not needed.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#!/usr/bin/env bash
2+
#-------------------------------------------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
5+
#-------------------------------------------------------------------------------------------------------------
6+
7+
# Syntax: ./common-debian.sh <install zsh flag> <username> <user UID> <user GID> <upgrade packages flag>
8+
9+
set -e
10+
11+
INSTALL_ZSH=${1:-"true"}
12+
USERNAME=${2:-"$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)"}
13+
USER_UID=${3:-1000}
14+
USER_GID=${4:-1000}
15+
UPGRADE_PACKAGES=${5:-"true"}
16+
17+
if [ "$(id -u)" -ne 0 ]; then
18+
echo 'Script must be run a root. Use sudo or set "USER root" before running the script.'
19+
exit 1
20+
fi
21+
22+
# Treat a user name of "none" as root
23+
if [ "${USERNAME}" = "none" ] || [ "${USERNAME}" = "root" ]; then
24+
USERNAME=root
25+
USER_UID=0
26+
USER_GID=0
27+
fi
28+
29+
# Ensure apt is in non-interactive to avoid prompts
30+
export DEBIAN_FRONTEND=noninteractive
31+
32+
# Install apt-utils to avoid debconf warning
33+
apt-get -y install --no-install-recommends apt-utils 2> >( grep -v 'debconf: delaying package configuration, since apt-utils is not installed' >&2 )
34+
35+
# Get to latest versions of all packages
36+
if [ "${UPGRADE_PACKAGES}" = "true" ]; then
37+
apt-get -y upgrade --no-install-recommends
38+
fi
39+
40+
# Install common developer tools and dependencies
41+
apt-get -y install --no-install-recommends \
42+
git \
43+
openssh-client \
44+
less \
45+
iproute2 \
46+
procps \
47+
curl \
48+
wget \
49+
unzip \
50+
nano \
51+
jq \
52+
lsb-release \
53+
ca-certificates \
54+
apt-transport-https \
55+
dialog \
56+
gnupg2 \
57+
libc6 \
58+
libgcc1 \
59+
libgssapi-krb5-2 \
60+
libicu[0-9][0-9] \
61+
liblttng-ust0 \
62+
libstdc++6 \
63+
zlib1g \
64+
locales
65+
66+
# Ensure at least the en_US.UTF-8 UTF-8 locale is available.
67+
# Common need for both applications and things like the agnoster ZSH theme.
68+
echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
69+
locale-gen
70+
71+
# Install libssl1.1 if available
72+
if [[ ! -z $(apt-cache --names-only search ^libssl1.1$) ]]; then
73+
apt-get -y install --no-install-recommends libssl1.1
74+
fi
75+
76+
# Install appropriate version of libssl1.0.x if available
77+
LIBSSL=$(dpkg-query -f '${db:Status-Abbrev}\t${binary:Package}\n' -W 'libssl1\.0\.?' 2>&1 || echo '')
78+
if [ "$(echo "$LIBSSL" | grep -o 'libssl1\.0\.[0-9]:' | uniq | sort | wc -l)" -eq 0 ]; then
79+
if [[ ! -z $(apt-cache --names-only search ^libssl1.0.2$) ]]; then
80+
# Debian 9
81+
apt-get -y install --no-install-recommends libssl1.0.2
82+
elif [[ ! -z $(apt-cache --names-only search ^libssl1.0.0$) ]]; then
83+
# Ubuntu 18.04, 16.04, earlier
84+
apt-get -y install --no-install-recommends libssl1.0.0
85+
fi
86+
fi
87+
88+
# Create or update a non-root user to match UID/GID - see https://aka.ms/vscode-remote/containers/non-root-user.
89+
if id -u $USERNAME > /dev/null 2>&1; then
90+
# User exists, update if needed
91+
if [ "$USER_GID" != "$(id -G $USERNAME)" ]; then
92+
groupmod --gid $USER_GID $USERNAME
93+
usermod --gid $USER_GID $USERNAME
94+
fi
95+
if [ "$USER_UID" != "$(id -u $USERNAME)" ]; then
96+
usermod --uid $USER_UID $USERNAME
97+
fi
98+
else
99+
# Create user
100+
groupadd --gid $USER_GID $USERNAME
101+
useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME
102+
fi
103+
104+
# Add add sudo support for non-root user
105+
apt-get install -y sudo
106+
echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME
107+
chmod 0440 /etc/sudoers.d/$USERNAME
108+
109+
# Ensure ~/.local/bin is in the PATH for root and non-root users for bash. (zsh is later)
110+
echo "export PATH=\$PATH:\$HOME/.local/bin" | tee -a /root/.bashrc >> /home/$USERNAME/.bashrc
111+
chown $USER_UID:$USER_GID /home/$USERNAME/.bashrc
112+
113+
# Optionally install and configure zsh
114+
if [ "$INSTALL_ZSH" = "true" ] && [ ! -d "/root/.oh-my-zsh" ]; then
115+
apt-get install -y zsh
116+
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
117+
echo "export PATH=\$PATH:\$HOME/.local/bin" >> /root/.zshrc
118+
cp -R /root/.oh-my-zsh /home/$USERNAME
119+
cp /root/.zshrc /home/$USERNAME
120+
sed -i -e "s/\/root\/.oh-my-zsh/\/home\/$USERNAME\/.oh-my-zsh/g" /home/$USERNAME/.zshrc
121+
chown -R $USER_UID:$USER_GID /home/$USERNAME/.oh-my-zsh /home/$USERNAME/.zshrc
122+
fi
123+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env bash
2+
#-------------------------------------------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
5+
#-------------------------------------------------------------------------------------------------------------
6+
7+
# Syntax: ./node-debian.sh <directory to install nvm> <node version to install (use "none" to skip)> <non-root user>
8+
9+
set -e
10+
11+
export NVM_DIR=${1:-"/usr/local/share/nvm"}
12+
export NODE_VERSION=${2:-"lts/*"}
13+
NONROOT_USER=${3:-"vscode"}
14+
15+
if [ "$(id -u)" -ne 0 ]; then
16+
echo 'Script must be run a root. Use sudo or set "USER root" before running the script.'
17+
exit 1
18+
fi
19+
20+
# Ensure apt is in non-interactive to avoid prompts
21+
export DEBIAN_FRONTEND=noninteractive
22+
23+
if [ "${NODE_VERSION}" = "none" ]; then
24+
export NODE_VERSION=
25+
fi
26+
27+
# Install NVM
28+
mkdir -p ${NVM_DIR}
29+
curl -so- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash 2>&1
30+
if [ "${NODE_VERSION}" != "" ]; then
31+
/bin/bash -c "source $NVM_DIR/nvm.sh && nvm alias default ${NODE_VERSION}" 2>&1
32+
fi
33+
34+
echo -e "export NVM_DIR=\"${NVM_DIR}\"\n\
35+
[ -s \"\$NVM_DIR/nvm.sh\" ] && \\. \"\$NVM_DIR/nvm.sh\"\n\
36+
[ -s \"\$NVM_DIR/bash_completion\" ] && \\. \"\$NVM_DIR/bash_completion\"" \
37+
| tee -a /home/${NONROOT_USER}/.bashrc /home/${NONROOT_USER}/.zshrc >> /root/.zshrc
38+
39+
echo -e "if [ \"\$(stat -c '%U' \$NVM_DIR)\" != \"${NONROOT_USER}\" ]; then\n\
40+
sudo chown -R ${NONROOT_USER}:root \$NVM_DIR\n\
41+
fi" | tee -a /root/.bashrc /root/.zshrc /home/${NONROOT_USER}/.bashrc >> /home/${NONROOT_USER}/.zshrc
42+
43+
chown ${NONROOT_USER}:${NONROOT_USER} /home/${NONROOT_USER}/.bashrc /home/${NONROOT_USER}/.zshrc
44+
chown -R ${NONROOT_USER}:root ${NVM_DIR}
45+
46+
# Install yarn
47+
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - 2>/dev/null
48+
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
49+
apt-get update
50+
apt-get -y install --no-install-recommends yarn

.devcontainer/maven-settings.xml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
4+
https://maven.apache.org/xsd/settings-1.0.0.xsd">
5+
<localRepository>/usr/share/maven/ref/repository</localRepository>
6+
</settings>

0 commit comments

Comments
 (0)