---
# System prepended metadata

title: Setup ROS 2 with VSCode and Docker on MAC

---

# Setup ROS 2 with VSCode and Docker on MAC

## Install XQuartz and socat
1. Install XQuartz from [website](https://www.xquartz.org) or using homebrew
```shell
brew cask install xquartz
```
2. Install socat
```shell
brew cask install socat
```
3. Run XQuartz 
4. In the XQuartz preferences, go to the Security tab and make sure Allow connections from network clients is ticked.
5. Open XQuartz terminal and type the following:
```shell
defaults write org.xquartz.X11 enable_iglx -bool YES
```
6. Exit XQuartz 

## Access docker container gui from OSX
1. Open MAC terminal and type the following:
```shell
socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIENT:\"$DISPLAY\"
```
2. Run XQuartz
3. Open XQuartz terminal and type the following:
```shell
xhost +
```

## Open and Build Development Container
1. Install vscode and docker
2. Edit devcontainer.json
```json
{
	"name": "osrf/ros:humble-desktop-full",
	"build": {
		"dockerfile": "Dockerfile"
	},
	"runArgs": [
		// "--cap-add=SYS_PTRACE", // Allows the container to trace processes using ptrace system call
		// "--security-opt=seccomp=unconfined", // Disables seccomp security profiles for the container
		// "--ipc=host", // Shares the host's IPC namespace with the container
		// "--network=host", // Shares the host's network namespace with the container
		// "--pid=host", // Shares the host's PID namespace with the container
		// "--privileged", // Gives the container full access to the host's devices and allows it to run with root privileges
	],
	"workspaceMount": "source=${localWorkspaceFolder},target=/root/${localWorkspaceFolderBasename},type=bind",
	"workspaceFolder": "/root/${localWorkspaceFolderBasename}",
	"containerEnv": {
		"DISPLAY": "host.docker.internal:0"
	},
	"mounts": [
		//* Uncomment to allow the container to access the X server on the host e.g. to run Rviz and other GUI tools
		{
			"source": "/tmp/.X11-unix",
			"target": "/tmp/.X11-unix",
			"type": "bind"
		},
		/*
		{
			"source": "${localEnv:XAUTHORITY:-$HOME/.Xuthority}",
			"target": "/root/.Xauthority",
			"type": "bind"
		},
		/* Uncomment to persist bash history between sessions
		{
			"source": "${localEnv:HOME}${localEnv:USERPROFILE}/.bash_history",
			"target": "/root/.bash_history",
			"type": "bind"
		}
		*/
	],
	"customizations": {
		"vscode": {
			"settings": {},
			"extensions": [
				"mhutchie.git-graph",
				// "eamodio.gitlens",
				// "donjayamanne.githistory",
				// "ms-python.vscode-pylance",
				// "ms-python.python"
				// "ms-vscode.cpptools",
				// "ms-vscode.cpptools-extension-pack"
			]
		}
	}
}
```
3. Edit Dockerfile
```dockerfile
FROM osrf/ros:humble-desktop-full

SHELL [ "/bin/bash" , "-c" ]

# Upgrade all packages
RUN sudo apt update && sudo apt upgrade -y

# Install essential packages
RUN sudo apt install -y wget x11-apps python3-pip net-tools
RUN pip3 install pygame

# Create overlay workspace
# WORKDIR /root/ros_ws/src
# COPY packages.repos .

# RUN vcs import < packages.repos; \
#    cd ..; \
#    rosdep install --from-paths src --ignore-src -r -y; \
#    source /opt/ros/${ROS_DISTRO}/setup.bash; \
#    colcon build;

# Add sourcing ROS setup.bash to .bashrc
RUN echo "source /opt/ros/${ROS_DISTRO}/setup.bash" >> ~/.bashrc
RUN echo "export LIBGL_ALWAYS_INDIRECT=1" >> ~/.bashrc
```