Skip to content

Instantly share code, notes, and snippets.

@zflat
Last active September 4, 2024 17:56
Show Gist options
  • Save zflat/445b2ef3427dd947e2a3d46b61420b9b to your computer and use it in GitHub Desktop.
Save zflat/445b2ef3427dd947e2a3d46b61420b9b to your computer and use it in GitHub Desktop.
devcontainer template for ros2 colcon workspace
  • Create a directory for your new workspace
    mkdir dev_ros2
    cd dev_ros2
  • Download the zip and extract inside your workspace directory as .devcontainer
    wget -qO- https://gist.github.com/zflat/445b2ef3427dd947e2a3d46b61420b9b/archive/main.tar.gz | tar -xz --transform "s/^445b2e.*main/.devcontainer/"
  • Create the workspace volume. Run from the root of your workspace:
    docker volume create "$(basename $(pwd))_colcon_ws_volume"
  • Clone repositories into the workspace
  • Run and attach to the devcontainer
    devcontainer up --workspace-folder .
    devcontainer exec --workspace-folder . bash
;;; Directory Local Variables -*- no-byte-compile: t -*-
;;; For more information see (info "(emacs) Directory Variables")
;;; and the following guides:
;;; https://docs.projectile.mx/projectile/projects.html#storing-project-settings
;;; https://endlessparentheses.com/a-quick-guide-to-directory-local-variables.html
;;; http://joelmccracken.github.io/entries/project-local-variables-in-projectile-with-dirlocals/
((c++-mode . (
;; Command to run from the project root when (project-compile) is invoked:
(compile-command . "./scripts/build.bash")))
(nil . (
;; An alternative to setting a major mode 'compile-command is
;; to set the variable 'projectile-project-compilation-cmd
;; which will be used by (projectile-compile-project) as long
;; as the 'projectile-compilation-cmd-map is cleared.
;;
;; To clear the cached command for a project, run:
;; (remhash (projectile-project-root) projectile-compilation-cmd-map)
(eval . (progn
(remhash (projectile-project-root) projectile-compilation-cmd-map)
(setq projectile-project-compilation-cmd
(concat
(getenv "HOME") "/.asdf/shims/devcontainer exec --workspace-folder "
(projectile-project-root) "../"
" bash -c 'cd /opt/colcon_ws && . /opt/ros/rolling/setup.bash"
" && colcon build --packages-select lib_my_example"
" && . install/setup.bash"
" && colcon test --event-handlers console_direct+ --packages-select lib_my_example'")))))))
#!/bin/bash
mkdir -p /opt/colcon_ws/src
ln -sf $CONTAINER_WS_DIR /opt/colcon_ws/src/
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// examples at: https://github.com/devcontainers/templates
{
"name": "${localWorkspaceFolderBasename}-dev",
"runArgs": [
"--name=${localWorkspaceFolderBasename}-dev",
"--workdir=/opt/colcon_ws"
],
"build": {
"dockerfile": "Dockerfile"
},
"containerEnv": {
"CONTAINER_WS_DIR": "${containerWorkspaceFolder}"
},
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
// 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": "/tmp/create_workspace.sh",
// Configure tool-specific properties.
// "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
"mounts": [
"source=${localWorkspaceFolderBasename}_colcon_ws_volume,target=/opt/colcon_ws,type=volume,consistency=cached"
]
}
FROM osrf/ros:rolling-desktop-full
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
byobu \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
COPY ./create_workspace.sh /tmp/
RUN chmod +x /tmp/create_workspace.sh
RUN echo "alias ws='cd /opt/colcon_ws && [ -e /opt/colcon_ws/install/setup.bash ] && . /opt/colcon_ws/install/setup.bash'" >> /root/.bashrc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment