Docker¶
Docker Basic Commands¶
Hello world
$ docker run hello-world
Manage images
$ docker image <options>
List images
$ docker images
Build an image from a Dockerfile
$ docker build <option> <Dockerfile>
Run a container
$ docker run <image>
List container
$ docker ps
Start one or more stoped container
$ docker start <options> <container> <container ..>
Run a command in a running container
$ docker exec <option> <container ID> <command>
Stop one or more containers
$ docker stop <options> <containers>
Kill one or more containers
$ docker kill <containers ID>
Writing first Dockerfile¶
Create necessary files.
$ mkdir my_first_docker && cd my_first_docker
$ touch Dockerfile
$ touch docker-compose.yml
$ touch ros_entrypoint.sh
$ chmod +x ros_entrypoint.sh
Edit
Dockerfile.
1ARG ROS_DISTRO=melodic
2FROM ros:${ROS_DISTRO}-robot
3
4ENV DEBIAN_FRONTEND noninteractive
5
6RUN apt-get update && apt-get install -y --no-install-recommends \
7 ros-${ROS_DISTRO}-perception-pcl \
8 && rm -rf /var/lib/apt/lists/*
9
10# setup entrypoint
11ENV ROS_DISTRO ${ROS_DISTRO}
12RUN mkdir -p /usr/local/bin/scripts
13COPY *entrypoint.sh /usr/local/bin/scripts/
14RUN chmod +x /usr/local/bin/scripts/*.sh
15
16ENTRYPOINT ["/ros_entrypoint.sh"]
17CMD ["bash"]
Edit
docker-compose.yml.
1version: '3'
2
3services:
4 ros-melodic-base:
5 build:
6 context: .
7 args:
8 - ROS_DISTRO=melodic
9 image: ros-melodic-base
10 stdin_open: true
11 tty: true
12 privileged: true
13 network_mode: "host"
14 volumes:
15 - /tmp/.X11-unix:/tmp/.X11-unix:ro
16 - /dev/shm:/dev/shm
17 - /dev/*:/dev/*
18 environment:
19 - DISPLAY=$DISPLAY
20 entrypoint: /usr/local/bin/scripts/ros-entrypoint.sh
21 command: bash -c "top"
Edit
ros_entrypoint.sh
1#!/bin/bash
2set -e
3# setup ros2 environment
4source "/opt/ros/$ROS_DISTRO/setup.bash"
5exec "$@"
Docker build.
$ docker-compose up --build