Docker

Docker Basic Commands

  1. Hello world

    $ docker run hello-world
    
  2. Manage images

    $ docker image <options>
    
  3. List images

    $ docker images
    
  4. Build an image from a Dockerfile

    $ docker build <option> <Dockerfile>
    
  5. Run a container

    $ docker run <image>
    
  6. List container

    $ docker ps
    
  7. Start one or more stoped container

    $ docker start <options> <container> <container ..>
    
  8. Run a command in a running container

    $ docker exec <option> <container ID> <command>
    
  1. Stop one or more containers

    $ docker stop <options> <containers>
    
  2. 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
  1. 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"]
  1. 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"
  1. Edit ros_entrypoint.sh

1#!/bin/bash
2set -e
3# setup ros2 environment
4source "/opt/ros/$ROS_DISTRO/setup.bash"
5exec "$@"
  1. Docker build.

$ docker-compose up --build