Skip to content

Commit

Permalink
trying slim dockerfile for devcontainer
Browse files Browse the repository at this point in the history
  • Loading branch information
hellt committed Nov 28, 2024
1 parent 5dada68 commit 2e7697a
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ RUN mkdir -p /home/vscode/.docker && echo "{}" > /home/vscode/.docker/config.jso
COPY ./.devcontainer/zsh/.zshrc /home/vscode/.zshrc
COPY ./.devcontainer/zsh/.p10k.zsh /home/vscode/.p10k.zsh
COPY ./.devcontainer/zsh/install-zsh-plugins.sh /tmp/install-zsh-plugins.sh
RUN bash -c "/tmp/install-zsh-plugins.sh"
COPY ./.devcontainer/zsh/install-tools-completions.sh /tmp/install-tools-completions.sh
RUN bash -c "/tmp/install-zsh-plugins.sh && /tmp/install-tools-completions.sh"

# Setup pyenv virtual environment for clab tests
COPY tests/requirements.txt /tmp/requirements.txt
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// For format details, see https://aka.ms/devcontainer.json.
{
"build": {
"dockerfile": "./Dockerfile",
"context": "..",
"dockerfile": "../Dockerfile",
"context": "../..",
"args": {
"CLAB_VERSION": "${localEnv:CLAB_VERSION}"
}
Expand Down
39 changes: 39 additions & 0 deletions .devcontainer/docker-in-docker_slim/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// For format details, see https://aka.ms/devcontainer.json.
{
"build": {
"dockerfile": "../slim.Dockerfile",
"context": "../..",
"args": {
"CLAB_VERSION": "${localEnv:CLAB_VERSION}"
}
},
"features": {
"ghcr.io/devcontainers/features/docker-in-docker:2": {
"version": "26.1.4",
"dockerDashComposeVersion": "none",
"moby": "false"
},
// Add sshd to support gh cli codespace cp.
"ghcr.io/devcontainers/features/sshd:1": {
"version": "latest"
}
},
"remoteUser": "vscode",
"customizations": {
"vscode": {
"extensions": [
"ms-azuretools.vscode-docker",
// Python.
"ms-python.python",
// Errors and highlighters.
"mechatroner.rainbow-csv",
"redhat.vscode-yaml",
// markdown
"yzhang.markdown-all-in-one",
"davidanson.vscode-markdownlint"
]
}
},
"workspaceMount": "source=${localWorkspaceFolder},target=/${containerWorkspaceFolder},type=bind",
"workspaceFolder": "/clab"
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"--privileged"
],
"mounts": [
"type=bind,src=/run/netns,dst=/run/netns"
"type=bind,src=/run/netns,dst=/run/netns",
"type=bind,src=/var/lib/docker,dst=/var/lib/docker"
],
"features": {
"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {
Expand Down
48 changes: 48 additions & 0 deletions .devcontainer/docker-outside-of-docker_slim/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// For format details, see https://aka.ms/devcontainer.json.
{
"build": {
"dockerfile": "../slim.Dockerfile",
"context": "../..",
"args": {
"CLAB_VERSION": "${localEnv:CLAB_VERSION}"
}
},
"runArgs": [
"--network=host",
"--pid=host",
"--privileged"
],
"mounts": [
"type=bind,src=/run/netns,dst=/run/netns",
"type=bind,src=/var/lib/docker,dst=/var/lib/docker"
],
"features": {
"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {
"version": "26.1.4",
"dockerDashComposeVersion": "none",
"moby": "false"
},
// Add sshd to support gh cli codespaces cp.
"ghcr.io/devcontainers/features/sshd:1": {
"version": "latest"
}
},
"remoteUser": "vscode",
"customizations": {
"vscode": {
"extensions": [
"ms-azuretools.vscode-docker",
// Python.
"ms-python.python",
// Errors and highlighters.
"mechatroner.rainbow-csv",
"redhat.vscode-yaml",
// markdown
"yzhang.markdown-all-in-one",
"davidanson.vscode-markdownlint"
]
}
},
"workspaceFolder": "${localWorkspaceFolder}",
"workspaceMount": "source=${localWorkspaceFolder},target=${localWorkspaceFolder},type=bind"
}
57 changes: 57 additions & 0 deletions .devcontainer/slim.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
FROM mcr.microsoft.com/devcontainers/python:3.11-bookworm

ARG CLAB_VERSION

# Add the netdevops repository
RUN echo "deb [trusted=yes] https://netdevops.fury.site/apt/ /" | \
tee -a /etc/apt/sources.list.d/netdevops.list

# Install necessary packages, including curl
RUN apt-get update && apt-get install -y --no-install-recommends \
direnv \
btop \
iputils-ping \
tcpdump \
iproute2 \
qemu-kvm \
dnsutils \
telnet \
curl

# Install Containerlab
RUN bash -c "$(curl -sL https://get.containerlab.dev)" -- -v ${CLAB_VERSION}

# Install GitHub CLI directly from the latest release
RUN bash -c 'ARCH=$(uname -m | sed "s/x86_64/amd64/" | sed "s/aarch64/arm64/") && \
VERSION=$(curl -s https://api.github.com/repos/cli/cli/releases/latest | \
grep -oP "\"tag_name\": \"\K[^\"]+") && \
CLEAN_VERSION=${VERSION#v} && \
DOWNLOAD_URL="https://github.com/cli/cli/releases/download/${VERSION}/gh_${CLEAN_VERSION}_linux_${ARCH}.tar.gz" && \
curl -L "$DOWNLOAD_URL" | tar xz -C /tmp && \
mv /tmp/gh_${CLEAN_VERSION}_linux_${ARCH}/bin/gh /usr/local/bin/ && \
chmod +x /usr/local/bin/gh && \
rm -rf /tmp/gh_${CLEAN_VERSION}_linux_${ARCH}'

# Add empty docker config files to avoid clab warnings for root user
RUN mkdir -p /root/.docker && echo "{}" > /root/.docker/config.json

# Maintain SSH_AUTH_SOCK env var when using sudo
RUN mkdir -p /etc/sudoers.d && echo 'Defaults env_keep += "SSH_AUTH_SOCK"' > /etc/sudoers.d/ssh_auth_sock

# Switch to the vscode user provided by the base image
USER vscode

# Copy dclab script used to run the local containerlab build after `make build`
COPY ./.devcontainer/dclab /usr/local/bin/dclab

# Create SSH key for vscode user to enable passwordless SSH to devices
RUN ssh-keygen -t ecdsa -b 256 -N "" -f ~/.ssh/id_ecdsa

# Add empty docker config files to avoid clab warnings for vscode user
RUN mkdir -p /home/vscode/.docker && echo "{}" > /home/vscode/.docker/config.json

# Setup Zsh and plugins
COPY ./.devcontainer/zsh/.zshrc /home/vscode/.zshrc
COPY ./.devcontainer/zsh/.p10k.zsh /home/vscode/.p10k.zsh
COPY ./.devcontainer/zsh/install-zsh-plugins.sh /tmp/install-zsh-plugins.sh
RUN bash -c "/tmp/install-zsh-plugins.sh"
6 changes: 6 additions & 0 deletions .devcontainer/zsh/install-tools-completions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
gnmic completion zsh > "/home/vscode/.oh-my-zsh/custom/plugins/zsh-autocomplete/Completions/_gnmic"
# generate gnoic completions
gnoic completion zsh > "/home/vscode/.oh-my-zsh/custom/plugins/zsh-autocomplete/Completions/_gnoic"
# generate gh
gh completion -s zsh > "/home/vscode/.oh-my-zsh/custom/plugins/zsh-autocomplete/Completions/_gh"
6 changes: 0 additions & 6 deletions .devcontainer/zsh/install-zsh-plugins.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,3 @@ git clone --depth 1 https://github.com/z-shell/F-Sy-H.git ${ZSH_CUSTOM:-$HOME/.o
/usr/bin/containerlab completion zsh > "/home/vscode/.oh-my-zsh/custom/plugins/zsh-autocomplete/Completions/_containerlab"
# add clab alias to the completions
sed -i 's/compdef _containerlab containerlab/compdef _containerlab containerlab clab/g' /home/vscode/.oh-my-zsh/custom/plugins/zsh-autocomplete/Completions/_containerlab
# generate gnmic completions
gnmic completion zsh > "/home/vscode/.oh-my-zsh/custom/plugins/zsh-autocomplete/Completions/_gnmic"
# generate gnoic completions
gnoic completion zsh > "/home/vscode/.oh-my-zsh/custom/plugins/zsh-autocomplete/Completions/_gnoic"
# generate gh
gh completion -s zsh > "/home/vscode/.oh-my-zsh/custom/plugins/zsh-autocomplete/Completions/_gh"
16 changes: 13 additions & 3 deletions .github/workflows/build-devcontainer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,24 @@ jobs:
runs-on: ubuntu-22.04
strategy:
matrix:
variant: ["dind", "dood"]
variant:
- "dind"
- "dind-slim"
- "dood"
- "dood-slim"
include:
- variant: dind
devContFile: .devcontainer/devcontainer.json
devContFile: .devcontainer/docker-in-docker/devcontainer.json
image-suffix: ""
- variant: dind-slim
devContFile: .devcontainer/docker-in-docker_slim/devcontainer.json
image-suffix: "-slim"
- variant: dood
devContFile: .devcontainer/dood/devcontainer.json
devContFile: .devcontainer/docker-outside-of-docker/devcontainer.json
image-suffix: "-dood"
- variant: dood-slim
devContFile: .devcontainer/docker-outside-of-docker_slim/devcontainer.json
image-suffix: "-dood-slim"
steps:
- name: Checkout repository
uses: actions/checkout@v4
Expand Down
5 changes: 3 additions & 2 deletions docs/macos.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ You can see this workflow demonstration in this [YT video][yt-demo].

## Devcontainer

Another option to run containerlab on ARM or Intel Macs is to use the Docker in Docker approach which is enabled by our Devcontainer.
Another convenient option to run containerlab on ARM/Intel Macs and Windows is to use the [Devcontainer](https://docs.github.com/en/codespaces/setting-up-your-project-for-codespaces/adding-a-dev-container-configuration/introduction-to-dev-containers) feature that works great with VS Code and other IDE's.

Containerlab's devcontainer was created to power [containerlab in codespaces](manual/codespaces.md), but it is a perfect fit for running containerlab on **any macOS** as it uses the docker-in-docker method where an isolated instance of a docker daemon is created inside a container.
A Devcontainer is a specification for a container that is used to create a development environment. By creating the `devcontainer.json` file, you define the development environment for your project. Containerlab project maintains a pre-built multi-arch devcontainer image that users can reuse to run containerlabs.
It was initially created to power [containerlab in codespaces](manual/codespaces.md), but it is a perfect fit for running containerlab on **wide range of OSes**.

/// note
Starting with **Containerlab v0.60.0**, you can use the devcontainer with ARM64 macOS to run containerlab.
Expand Down

0 comments on commit 2e7697a

Please sign in to comment.