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

Added folder for kubernetes helm with docker compose. Include docker-… #284

Open
wants to merge 2 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
75 changes: 75 additions & 0 deletions containers/kubernetes-helm-docker-compose/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#-------------------------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
#-------------------------------------------------------------------------------------------------------------

# You can use any Debian/Ubuntu based image as a base
FROM debian:9

# Avoid warnings by switching to noninteractive
ENV DEBIAN_FRONTEND=noninteractive

# This Dockerfile adds a non-root user with sudo access. Use the "remoteUser"
# property in devcontainer.json to use it. On Linux, the container user's GID/UIDs
# will be updated to match your local UID/GID (when using the dockerFile property).
# See https://aka.ms/vscode-remote/containers/non-root-user for details.
ARG USERNAME=vscode
ARG USER_UID=1000
ARG USER_GID=$USER_UID

# Configure apt and install packages
RUN apt-get update \
&& apt-get -y install --no-install-recommends apt-utils dialog 2>&1 \
#
# Verify git, process tools installed
&& apt-get -y install git openssh-client less iproute2 procps \
#
# Install Docker CE CLI
&& apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common lsb-release \
&& curl -fsSL https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/gpg | (OUT=$(apt-key add - 2>&1) || echo $OUT) \
&& add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]') $(lsb_release -cs) stable" \
&& apt-get update \
&& apt-get install -y docker-ce-cli \
#
# Install kubectl
&& 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 \
&& chmod +x /usr/local/bin/kubectl \
#
# Install Helm
&& curl -s https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash - \
#
# Copy localhost's ~/.kube/config file into the container and swap out localhost
# for host.docker.internal whenever a new shell starts to keep them in sync.
&& echo '\n\
if [ "$SYNC_LOCALHOST_KUBECONFIG" == "true" ]; then\n\
mkdir -p $HOME/.kube\n\
cp -r $HOME/.kube-localhost/* $HOME/.kube\n\
sed -i -e "s/localhost/host.docker.internal/g" $HOME/.kube/config\n\
\n\
if [ -d "$HOME/.minikube-localhost" ]; then\n\
mkdir -p $HOME/.minikube\n\
cp -r $HOME/.minikube-localhost/ca.crt $HOME/.minikube\n\
sed -i -r "s|(\s*certificate-authority:\s).*|\\1$HOME\/.minikube\/ca.crt|g" $HOME/.kube/config\n\
cp -r $HOME/.minikube-localhost/client.crt $HOME/.minikube\n\
sed -i -r "s|(\s*client-certificate:\s).*|\\1$HOME\/.minikube\/client.crt|g" $HOME/.kube/config\n\
cp -r $HOME/.minikube-localhost/client.key $HOME/.minikube\n\
sed -i -r "s|(\s*client-key:\s).*|\\1$HOME\/.minikube\/client.key|g" $HOME/.kube/config\n\
fi\n\
fi' \
>> $HOME/.bashrc \
#
# Create a non-root user to use if preferred - see https://aka.ms/vscode-remote/containers/non-root-user.
&& groupadd --gid $USER_GID $USERNAME \
&& useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME \
# [Optional] Add sudo support for the non-root user
&& apt-get install -y sudo \
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME\
&& chmod 0440 /etc/sudoers.d/$USERNAME \
#
# Clean up
&& apt-get autoremove -y \
&& apt-get clean -y \
&& rm -rf /var/lib/apt/lists/*

# Switch back to dialog for any ad-hoc use of apt-get
ENV DEBIAN_FRONTEND=dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "Kubernetes & Helm wih docker-compose",
// Update the 'dockerComposeFile' list if you have more compose files or use different names.
// The .devcontainer/docker-compose.yml file contains any overrides you need/want to make.
"dockerComposeFile": [
//"../docker-compose.yml", //This could be used to point at an existing docker-compose.yaml in the top directory
"docker-compose.yml"
],
"service": "test-project",
"remoteEnv": {
},
"workspaceFolder": "/workspace",
"mounts": [],
// Set *default* container specific settings.json values on container create.
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
},

// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"ms-azuretools.vscode-docker",
"ms-kubernetes-tools.vscode-kubernetes-tools"
],

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

// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "bash ./test-project/test.sh",

// Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
// "remoteUser": "vscode"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#-------------------------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
#-------------------------------------------------------------------------------------------------------------

version: '3.2'
services:
test-project:
build:
context: .
dockerfile: Dockerfile

volumes:
# Update this to wherever you want VS Code to mount the folder of your project
- .:/workspace:cached

# Forwards the local Docker socket to the container.
- /var/run/docker.sock:/var/run/docker.sock

# mount kube config should consider using env pointing at userprofile
- type: bind
source: $HOME/.kube
target: /root/.kube-localhost

# Uncomment the next line to also sync certs in your .minikube folder
#- type: bind
# source: $HOME/.minikube
# target: /root/.minikube-localhost,type=bind"

environment:
- SYNC_LOCALHOST_KUBECONFIG=true

# Uncomment the next four lines if you will use a ptrace-based debuggers like C++, Go, and Rust.
# cap_add:
# - SYS_PTRACE
# security_opt:
# - seccomp:unconfined

# Overrides default command so things don't shut down after the process ends.
command: sleep infinity
5 changes: 5 additions & 0 deletions containers/kubernetes-helm-docker-compose/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
README.md
test-project
definition-build.json
.vscode
.npmignore
186 changes: 186 additions & 0 deletions containers/kubernetes-helm-docker-compose/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
# Kubernetes and Helm with Docker Compose

## Summary

*Access a local (or remote) Kubernetes cluster from inside a dev container. Includes kubectl, Helm, and the Docker CLI. Uses docker-compose*

| Metadata | Value |
|----------|-------|
| *Contributors* | The VS Code team, Phetsinorath William and [Allan Makura](https://twitter.com/allanmakura)|
| *Definition type* | Docker Compose & Dockerfile|
| *Languages, platforms* | Any |

## Description

Dev containers can be useful for all types of applications including those that also deploy into a container based-environment. While you can directly build and run the application inside the dev container you create, you may also want to test it by deploying a built container image into a local or remote [Kubernetes](https://kubernetes.io/) cluster without affecting your dev container.

This example illustrates how you can do this by using CLIs ([kubectl](https://kubernetes.io/docs/reference/kubectl/overview/), [Helm](https://helm.sh), Docker), the [Kubernetes extension](https://marketplace.visualstudio.com/items?itemName=ms-kubernetes-tools.vscode-kubernetes-tools), and the [Docker extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-docker) right from inside your dev container. This definition builds up from the [docker-in-docker](../docker-in-docker) container definition to add Kubernetes and Helm support. It installs the Docker and Kubernetes extensions inside the container so you can use its full feature set with your project.

The dev container also syncs your local Kubernetes config (`~/.kube/config` or `%USERPROFILE%\.kube\config`) into the container with the necessary modifications to allow it to interact with anything running on your local machine whenever the container or a terminal window is started. This includes interacting with a Kubernetes cluster managed through Docker Desktop or a local Minikube install.

## How it works / adapting your existing dev container config

You can adapt your own existing development container Docker Compose setup to support this scenario by following these steps:

1. First, update your `docker-compose.yml` to forward the local Docker socket and mount the local `.kube` folder in the container so its contents can be reused. From `docker-compose.yml`:

```yaml
..
volumes:
# Update this to wherever you want VS Code to mount the folder of your project
- .:/workspace:cached

# Forwards the local Docker socket to the container.
- /var/run/docker.sock:/var/run/docker.sock

# mount kube config should consider using env pointing at userprofile
- type: bind
source: $HOME/.kube
target: /root/.kube-localhost
environment:
- SYNC_LOCALHOST_KUBECONFIG=true
...
```

If you also want to reuse your Minikube certificates, just add a mount for your local `.minikube` folder as well:

```yaml
# Uncomment the next line to also sync certs in your .minikube folder
- type: bind
source: $HOME/.minikube
target: /root/.minikube-localhost,type=bind"
```

2. Next, update your Dockerfile so the default shell is `bash`, and then add a script to automatically swap out `localhost` for `host.docker.internal` in the container's copy of the Kubernetes config and (optionally) Minikube certificates to `.bashrc`. From `.devcontainer/Dockerfile`:

```Dockerfile
RUN echo '\n\
if [ "$SYNC_LOCALHOST_KUBECONFIG" == "true" ]; then\n\
mkdir -p $HOME/.kube\n\
cp -r $HOME/.kube-localhost/* $HOME/.kube\n\
sed -i -e "s/localhost/host.docker.internal/g" $HOME/.kube/config\n\
\n\
if [ -d "$HOME/.minikube-localhost" ]; then\n\
mkdir -p $HOME/.minikube\n\
cp -r $HOME/.minikube-localhost/ca.crt $HOME/.minikube\n\
sed -i -r "s|(\s*certificate-authority:\s).*|\\1$HOME\/.minikube\/ca.crt|g" $HOME/.kube/config\n\
cp -r $HOME/.minikube-localhost/client.crt $HOME/.minikube\n\
sed -i -r "s|(\s*client-certificate:\s).*|\\1$HOME\/.minikube\/client.crt|g" $HOME/.kube/config\n\
cp -r $HOME/.minikube-localhost/client.key $HOME/.minikube\n\
sed -i -r "s|(\s*client-key:\s).*|\\1$HOME\/.minikube\/client.key|g" $HOME/.kube/config\n\
fi\n\
fi' \
>> $HOME/.bashrc
```

3. Finally, update your Dockerfile to install all of the needed CLIs in the container. From `.devcontainer/Dockerfile`:

```Dockerfile
RUN apt-get update \
#
# Install Docker CE CLI
&& apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common lsb-release \
&& curl -fsSL https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/gpg | apt-key add - 2>/dev/null \
&& add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]') $(lsb_release -cs) stable" \
&& apt-get update \
&& apt-get install -y docker-ce-cli \
#
# Install kubectl
&& 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 \
&& chmod +x /usr/local/bin/kubectl \
#
# Install Helm
curl -s https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash -
```

4. Press <kbd>F1</kbd> and run **Remote-Containers: Rebuild Container** so the changes take effect.

That's it!

## Using this definition with an existing folder

A few notes on the definition:

* The included `.devcontainer/Dockerfile` can be altered to work with other Debian/Ubuntu-based container images such as `node` or `python`. Just, update the `FROM` statement to reference the new base image. For example:

```Dockerfile
FROM node:lts
```

* If you also want to sync your Minikube certificates, open `.docker-compose.yml` and uncomment this line in the `volumes` section :

```yaml
# Uncomment the next lines to also sync certs in your .minikube folder
# - type: bind
# source: $HOME/.minikube
# target: /root/.minikube-localhost,type=bind"
```

* If you want to **disable sync'ing** local Kubernetes config / Minikube certs into the container, remove `SYNC_LOCALHOST_KUBECONFIG=true` from `environment` section in `docker-compose.yml`.

See the section below for your operating system for more detailed setup instructions.

### Windows / macOS

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. Start Docker, right-click on the Docker icon and select "Preferences..."

3. Check **Kubernetes > Enable Kubernetes**

4. To use VS Code's copy of this definition:
1. Start VS Code and open your project folder.
2. Press <kbd>F1</kbd> select and **Remote-Containers: Add Development Container Configuration Files...** from the command palette.
3. Select the Docker in Docker Compose definition.

5. To use latest-and-greatest copy of this definition from the repository:
1. Clone this repository.
2. Copy the contents of `containers/kubernetes-helm-docker-compose/.devcontainer` to the root of your project folder.
3. Copy `container/kubernetes-helm-docker-compose/docker-compose.yml` to the root of your project folder.
4. Start VS Code and open your project folder.

6. After following step 2 or 3, the contents of the `.devcontainer` folder and / or `docker-compose.yml` in your project can be adapted to meet your needs.

7. Finally, press <kbd>F1</kbd> and run **Remote-Containers: Reopen Folder in Container** to start using the definition.

## Linux / Minikube Setup

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. Install [kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/) and [Minikube](https://kubernetes.io/docs/tasks/tools/install-minikube/) on your local OS if you have not already.

3. Start Minikube as follows:
```
minikube start
kubectl config set-context minikube
```

4. To use VS Code's copy of this definition:
1. Start VS Code and open your project folder.
2. Press <kbd>F1</kbd> select and **Remote-Containers: Add Development Container Configuration Files...** from the command palette.
3. Select the Docker in Docker Compose definition.

5. To use latest-and-greatest copy of this definition from the repository:
1. Clone this repository.
2. Copy the contents of `containers/kubernetes-helm-docker-compose/.devcontainer` to the root of your project folder.
3. Copy `containers/kubernetes-helm-docker-compose/docker-compose.yml` to the root of your project folder.
4. Start VS Code and open your project folder.

6. After following step 2 or 3, the contents of the `.devcontainer` folder and / or `docker-compose.yml` in your project can be adapted to meet your needs.

7. Open `docker-compose.yml` and uncomment this line in the `volumes` section:

```yaml
# Uncomment the next line to also sync certs in your .minikube folder
#- type: bind
# source: $HOME/.minikube
# target: /root/.minikube-localhost,type=bind"
```

8. Finally, press <kbd>F1</kbd> and run **Remote-Containers: Reopen Folder in Container** to start using the definition.

## License

Copyright (c) Microsoft Corporation. All rights reserved.

Licensed under the MIT License. See [LICENSE](https://github.com/Microsoft/vscode-dev-containers/blob/master/LICENSE).
Loading