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

Commit b39d23f

Browse files
authored
Added support for Codespaces
1 parent b196622 commit b39d23f

File tree

6 files changed

+489
-0
lines changed

6 files changed

+489
-0
lines changed

.devcontainer/Dockerfile

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Note: You can use any Debian/Ubuntu based image you want.
2+
#FROM mcr.microsoft.com/vscode/devcontainers/base:0-buster
3+
ARG VARIANT=3.1
4+
FROM mcr.microsoft.com/vscode/devcontainers/dotnetcore:${VARIANT}
5+
6+
# Options
7+
ARG INSTALL_ZSH="true"
8+
ARG UPGRADE_PACKAGES="false"
9+
ARG ENABLE_NONROOT_DOCKER="true"
10+
ARG SOURCE_SOCKET=/var/run/docker-host.sock
11+
ARG TARGET_SOCKET=/var/run/docker.sock
12+
ARG USERNAME=vscode
13+
ARG USER_UID=1000
14+
ARG USER_GID=$USER_UID
15+
16+
# Install needed packages and setup non-root user. Use a separate RUN statement to add your own dependencies.
17+
COPY library-scripts/*.sh /tmp/library-scripts/
18+
RUN apt-get update \
19+
&& /bin/bash /tmp/library-scripts/common-debian.sh "${INSTALL_ZSH}" "${USERNAME}" "${USER_UID}" "${USER_GID}" "${UPGRADE_PACKAGES}" \
20+
# Use Docker script from script library to set things up
21+
&& /bin/bash /tmp/library-scripts/docker-debian.sh "${ENABLE_NONROOT_DOCKER}" "${SOURCE_SOCKET}" "${TARGET_SOCKET}" "${USERNAME}" \
22+
# Install Dapr
23+
&& wget -q https://raw.githubusercontent.com/dapr/cli/master/install/install.sh -O - | /bin/bash \
24+
# Clean up
25+
&& apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/* /tmp/library-scripts/
26+
27+
# Install kubectl
28+
RUN curl -sSL -o /usr/local/bin/kubectl https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl \
29+
&& chmod +x /usr/local/bin/kubectl
30+
31+
# Install Helm
32+
RUN curl -s https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash -
33+
34+
# Script copies localhost's ~/.kube/config file into the container and swaps out
35+
# localhost for host.docker.internal on bash/zsh start to keep them in sync.
36+
COPY copy-kube-config.sh /usr/local/share/
37+
RUN chown ${USERNAME}:root /usr/local/share/copy-kube-config.sh \
38+
&& echo "source /usr/local/share/copy-kube-config.sh" | tee -a /root/.bashrc /root/.zshrc /home/${USERNAME}/.bashrc >> /home/${USERNAME}/.zshrc
39+
40+
# [Optional] Uncomment this section to install additional OS packages.
41+
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
42+
# && apt-get -y install --no-install-recommends <your-package-list-here>
43+
44+
# Setting the ENTRYPOINT to docker-init.sh will configure non-root access to the Docker
45+
# socket. The script will also execute CMD if you need to alter startup behaviors.
46+
ENTRYPOINT [ "/usr/local/share/docker-init.sh" ]
47+
CMD [ "sleep", "infinity" ]

.devcontainer/copy-kube-config.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash -i
2+
3+
# Copies localhost's ~/.kube/config file into the container and swap out localhost
4+
# for host.docker.internal whenever a new shell starts to keep them in sync.
5+
if [ "$SYNC_LOCALHOST_KUBECONFIG" = "true" ] && [ -d "/usr/local/share/kube-localhost" ]; then
6+
mkdir -p $HOME/.kube
7+
sudo cp -r /usr/local/share/kube-localhost/* $HOME/.kube
8+
sudo chown -R $(id -u) $HOME/.kube
9+
sed -i -e "s/localhost/host.docker.internal/g" $HOME/.kube/config
10+
sed -i -e "s/127.0.0.1/host.docker.internal/g" $HOME/.kube/config
11+
12+
# If .minikube was mounted, set up client cert/key
13+
if [ -d "/usr/local/share/minikube-localhost" ]; then
14+
mkdir -p $HOME/.minikube
15+
sudo cp -r /usr/local/share/minikube-localhost/ca.crt $HOME/.minikube
16+
# Location varies between versions of minikube
17+
if [ -f "/usr/local/share/minikube-localhost/client.crt" ]; then
18+
sudo cp -r /usr/local/share/minikube-localhost/client.crt $HOME/.minikube
19+
sudo cp -r /usr/local/share/minikube-localhost/client.key $HOME/.minikube
20+
elif [ -f "/usr/local/share/minikube-localhost/profiles/minikube/client.crt" ]; then
21+
sudo cp -r /usr/local/share/minikube-localhost/profiles/minikube/client.crt $HOME/.minikube
22+
sudo cp -r /usr/local/share/minikube-localhost/profiles/minikube/client.key $HOME/.minikube
23+
fi
24+
sudo chown -R $(id -u) $HOME/.minikube
25+
26+
# Point .kube/config to the correct locaiton of the certs
27+
sed -i -r "s|(\s*certificate-authority:\s).*|\\1$HOME\/.minikube\/ca.crt|g" $HOME/.kube/config
28+
sed -i -r "s|(\s*client-certificate:\s).*|\\1$HOME\/.minikube\/client.crt|g" $HOME/.kube/config
29+
sed -i -r "s|(\s*client-key:\s).*|\\1$HOME\/.minikube\/client.key|g" $HOME/.kube/config
30+
fi
31+
fi

.devcontainer/devcontainer.json

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//////
2+
// For format details, see https://aka.ms/vscode-remote/devcontainer.json or this file's README at:
3+
// https://github.com/microsoft/vscode-dev-containers/tree/v0.134.1/containers/docker-from-docker-compose
4+
{
5+
"name": "Docker from Docker Compose + Kubernetes & Helm",
6+
"dockerFile": "Dockerfile",
7+
//"dockerComposeFile": [
8+
// "docker-compose.yml"]
9+
//,
10+
11+
//"service": "docker-from-docker",
12+
"workspaceFolder": "/workspace",
13+
14+
// Use this environment variable if you need to bind mount your local source code into a new container.
15+
"remoteEnv": {
16+
"LOCAL_WORKSPACE_FOLDER": "${localWorkspaceFolder}",
17+
"SYNC_LOCALHOST_KUBECONFIG": "true"
18+
},
19+
20+
"mounts": [
21+
"source=/var/run/docker.sock,target=/var/run/docker-host.sock,type=bind",
22+
"source=${env:HOME}${env:USERPROFILE}/.kube,target=/usr/local/share/kube-localhost,type=bind"
23+
// Uncomment the next line to also sync certs in your .minikube folder
24+
// "source=${env:HOME}${env:USERPROFILE}/.minikube,target=/usr/local/share/minikube-localhost,type=bind"
25+
],
26+
27+
// Set *default* container specific settings.json values on container create.
28+
"settings": {
29+
"terminal.integrated.shell.linux": "/bin/bash"
30+
},
31+
32+
// Add the IDs of extensions you want installed when the container is created.
33+
"extensions": [
34+
"ms-azuretools.vscode-docker",
35+
"ms-dotnettools.csharp",
36+
"github.github-vscode-theme",
37+
"coenraads.bracket-pair-colorizer-2",
38+
"github.vscode-pull-request-github",
39+
"ms-azuretools.vscode-docker",
40+
"ms-vsliveshare.vsliveshare",
41+
"vscode-icons-team.vscode-icons",
42+
"visualstudioexptteam.vscodeintellicode",
43+
"ms-azuretools.vscode-docker",
44+
"ms-kubernetes-tools.vscode-kubernetes-tools",
45+
"ms-mssql.mssql",
46+
"formulahendry.dotnet-test-explorer"
47+
],
48+
49+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
50+
"forwardPorts": [],
51+
52+
// Use 'postCreateCommand' to run commands after the container is created.
53+
// "postCreateCommand": "kubectl version",
54+
55+
// Uncomment when using a ptrace-based debugger like C++, Go, and Rust.
56+
// "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
57+
58+
// Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
59+
// "overrideCommand": false,
60+
//"remoteUser": "vscode"
61+
}

.devcontainer/docker-compose.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
version: '3'
2+
services:
3+
docker-from-docker:
4+
# Uncomment the next line to use a non-root user for all processes.
5+
# See https://aka.ms/vscode-remote/containers/non-root for details.
6+
# user: vscode
7+
8+
build:
9+
context: .
10+
dockerfile: Dockerfile
11+
args:
12+
# On Linux, you may need to update USER_UID and USER_GID below if not your local UID is not 1000.
13+
USER_UID: 1000
14+
USER_GID: 1000
15+
16+
volumes:
17+
# Update this to wherever you want VS Code to mount the folder of your project
18+
- ..:/workspace:cached
19+
20+
# Forwards the local Docker socket to the container.
21+
- /var/run/docker.sock:/var/run/docker-host.sock
22+
23+
# Uncomment the next four lines if you will use a ptrace-based debuggers like C++, Go, and Rust.
24+
# cap_add:
25+
# - SYS_PTRACE
26+
# security_opt:
27+
# - seccomp:unconfined
28+
29+
# Overrides default command so things don't shut down after the process ends.
30+
entrypoint: /usr/local/share/docker-init.sh
31+
command: sleep infinity
32+
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
INSTALL_ZSH=${1:-"true"}
10+
USERNAME=${2:-"vscode"}
11+
USER_UID=${3:-1000}
12+
USER_GID=${4:-1000}
13+
UPGRADE_PACKAGES=${5:-"true"}
14+
15+
set -e
16+
17+
if [ "$(id -u)" -ne 0 ]; then
18+
echo -e 'Script must be run a root. Use sudo, su, or add "USER root" to your Dockerfile before running this 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+
# Load markers to see which steps have already run
30+
MARKER_FILE="/usr/local/etc/vscode-dev-containers/common"
31+
if [ -f "${MARKER_FILE}" ]; then
32+
echo "Marker file found:"
33+
cat "${MARKER_FILE}"
34+
source "${MARKER_FILE}"
35+
fi
36+
37+
# Ensure apt is in non-interactive to avoid prompts
38+
export DEBIAN_FRONTEND=noninteractive
39+
40+
# Function to call apt-get if needed
41+
apt-get-update-if-needed()
42+
{
43+
if [ ! -d "/var/lib/apt/lists" ] || [ "$(ls /var/lib/apt/lists/ | wc -l)" = "0" ]; then
44+
echo "Running apt-get update..."
45+
apt-get update
46+
else
47+
echo "Skipping apt-get update."
48+
fi
49+
}
50+
51+
# Run install apt-utils to avoid debconf warning then verify presence of other common developer tools and dependencies
52+
if [ "${PACKAGES_ALREADY_INSTALLED}" != "true" ]; then
53+
apt-get-update-if-needed
54+
55+
PACKAGE_LIST="apt-utils \
56+
git \
57+
openssh-client \
58+
less \
59+
iproute2 \
60+
procps \
61+
curl \
62+
wget \
63+
unzip \
64+
nano \
65+
jq \
66+
lsb-release \
67+
ca-certificates \
68+
apt-transport-https \
69+
dialog \
70+
gnupg2 \
71+
libc6 \
72+
libgcc1 \
73+
libgssapi-krb5-2 \
74+
libicu[0-9][0-9] \
75+
liblttng-ust0 \
76+
libstdc++6 \
77+
zlib1g \
78+
locales \
79+
sudo"
80+
81+
# Install libssl1.1 if available
82+
if [[ ! -z $(apt-cache --names-only search ^libssl1.1$) ]]; then
83+
PACKAGE_LIST="${PACKAGE_LIST} libssl1.1"
84+
fi
85+
86+
# Install appropriate version of libssl1.0.x if available
87+
LIBSSL=$(dpkg-query -f '${db:Status-Abbrev}\t${binary:Package}\n' -W 'libssl1\.0\.?' 2>&1 || echo '')
88+
if [ "$(echo "$LIBSSL" | grep -o 'libssl1\.0\.[0-9]:' | uniq | sort | wc -l)" -eq 0 ]; then
89+
if [[ ! -z $(apt-cache --names-only search ^libssl1.0.2$) ]]; then
90+
# Debian 9
91+
PACKAGE_LIST="${PACKAGE_LIST} libssl1.0.2"
92+
elif [[ ! -z $(apt-cache --names-only search ^libssl1.0.0$) ]]; then
93+
# Ubuntu 18.04, 16.04, earlier
94+
PACKAGE_LIST="${PACKAGE_LIST} libssl1.0.0"
95+
fi
96+
fi
97+
98+
echo "Packages to verify are installed: ${PACKAGE_LIST}"
99+
apt-get -y install --no-install-recommends ${PACKAGE_LIST} 2> >( grep -v 'debconf: delaying package configuration, since apt-utils is not installed' >&2 )
100+
101+
PACKAGES_ALREADY_INSTALLED="true"
102+
fi
103+
104+
# Get to latest versions of all packages
105+
if [ "${UPGRADE_PACKAGES}" = "true" ]; then
106+
apt-get-update-if-needed
107+
apt-get -y upgrade --no-install-recommends
108+
apt-get autoremove -y
109+
fi
110+
111+
# Ensure at least the en_US.UTF-8 UTF-8 locale is available.
112+
# Common need for both applications and things like the agnoster ZSH theme.
113+
if [ "${LOCALE_ALREADY_SET}" != "true" ]; then
114+
echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
115+
locale-gen
116+
LOCALE_ALREADY_SET="true"
117+
fi
118+
119+
# Create or update a non-root user to match UID/GID - see https://aka.ms/vscode-remote/containers/non-root-user.
120+
if id -u $USERNAME > /dev/null 2>&1; then
121+
# User exists, update if needed
122+
if [ "$USER_GID" != "$(id -G $USERNAME)" ]; then
123+
groupmod --gid $USER_GID $USERNAME
124+
usermod --gid $USER_GID $USERNAME
125+
fi
126+
if [ "$USER_UID" != "$(id -u $USERNAME)" ]; then
127+
usermod --uid $USER_UID $USERNAME
128+
fi
129+
else
130+
# Create user
131+
groupadd --gid $USER_GID $USERNAME
132+
useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME
133+
fi
134+
135+
# Add add sudo support for non-root user
136+
if [ "${EXISTING_NON_ROOT_USER}" != "${USERNAME}" ]; then
137+
echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME
138+
chmod 0440 /etc/sudoers.d/$USERNAME
139+
EXISTING_NON_ROOT_USER="${USERNAME}"
140+
fi
141+
142+
# Ensure ~/.local/bin is in the PATH for root and non-root users for bash. (zsh is later)
143+
if [ "${DOT_LOCAL_ALREADY_ADDED}" != "true" ]; then
144+
echo "export PATH=\$PATH:\$HOME/.local/bin" | tee -a /root/.bashrc >> /home/$USERNAME/.bashrc
145+
chown $USER_UID:$USER_GID /home/$USERNAME/.bashrc
146+
DOT_LOCAL_ALREADY_ADDED="true"
147+
fi
148+
149+
# Optionally install and configure zsh
150+
if [ "${INSTALL_ZSH}" = "true" ] && [ ! -d "/root/.oh-my-zsh" ] && [ "${ZSH_ALREADY_INSTALLED}" != "true" ]; then
151+
apt-get-update-if-needed
152+
apt-get install -y zsh
153+
curl -fsSLo- https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh | bash 2>&1
154+
echo "export PATH=\$PATH:\$HOME/.local/bin" >> /root/.zshrc
155+
if [ "${USERNAME}" != "root" ]; then
156+
cp -fR /root/.oh-my-zsh /home/$USERNAME
157+
cp -f /root/.zshrc /home/$USERNAME
158+
sed -i -e "s/\/root\/.oh-my-zsh/\/home\/$USERNAME\/.oh-my-zsh/g" /home/$USERNAME/.zshrc
159+
chown -R $USER_UID:$USER_GID /home/$USERNAME/.oh-my-zsh /home/$USERNAME/.zshrc
160+
fi
161+
ZSH_ALREADY_INSTALLED="true"
162+
fi
163+
164+
# Write marker file
165+
mkdir -p "$(dirname "${MARKER_FILE}")"
166+
echo -e "\
167+
PACKAGES_ALREADY_INSTALLED=${PACKAGES_ALREADY_INSTALLED}\n\
168+
LOCALE_ALREADY_SET=${LOCALE_ALREADY_SET}\n\
169+
EXISTING_NON_ROOT_USER=${EXISTING_NON_ROOT_USER}\n\
170+
DOT_LOCAL_ALREADY_ADDED=${DOT_LOCAL_ALREADY_ADDED}\n\
171+
ZSH_ALREADY_INSTALLED=${ZSH_ALREADY_INSTALLED}" > "${MARKER_FILE}"

0 commit comments

Comments
 (0)