跳转至

Docker

  • Docker: Securely build, share and run any application, anywhere
  • Docker Hub: the world's easiest way to create, manage, and deliver your teams' container applications
  • docker中文社区

Overview

  • the images and containers are stored by Docker in /var/lib/docker by default

Image vs Container

  • 镜像 (Images): 可以认为是超级轻量级的虚拟机的快照。镜像会有自己的唯一ID,名字和标签,比如ubuntu, 比如ubuntu:18.04等。通常都是在已有的镜像(多数是Linux操作系统的镜像)的基础上构建自己的具有新功能的镜像。

  • 容器 (Containers): 可以认为是超级轻量级的虚拟机,是镜像运行起来所处的可读写的状态。容器里面可以安装、运行程序,还可以把安装好的程序存储起来获得新的镜像。与虚拟机很大的不同在于,一个容器通常只运行一个程序。在Docker中,应用程序和数据文件是分开的,因此可以在不影响数据的情况下快速升级代码或系统。

Tutorial

Install

Config & Check

JSON
// vim /etc/docker/daemon.json
{
    "registry-mirrors":["https://docker.mirrors.ustc.edu.cn"]
}

restart

Bash
sudo systemctl daemon-reload
sudo systemctl restart docker

check

Bash
sudo docker info

VS Code

Error: permission denied while trying to connect to the Docker daemon socket at unix

Bash
sudo chmod 666 /var/run/docker.sock

Image

  • view

    Bash
    sudo docker images
    

  • get

    Bash
    sudo docker pull ros
    
    sudo docker pull ros:kinetic-robot
    

  • create

    Bash
    sudo docker build -t <REPO-NAME:TAG>
    
    sudo docker build -t <REPO-NAME:TAG> -f <path-to-Dockerfile> .
    

  • modify

    Bash
    sudo docker tag <IMAGE-ID> <REPO-NAME>:<TAG>
    

  • delete

    Bash
    sudo docker rmi <REPO-NAME>/<IMAGE-ID>
    

  • get

    Bash
    sudo docker search ubuntu
    
    sudo docker pull ubuntu
    

  • run

    Bash
    sudo docker run --help
    
    # e.g.
    sudo docker run hello-world
    
    sudo docker run -it <REPO-NAME:TAG> /bin/bash
    # default TAG: latest
    sudo docker run -it <REPO-NAME> /bin/bash
    # e.g.
    sudo docker run -it ubuntu:15.10 /bin/bash
    

    • -i: 交互式操作
    • -t: 终端
    • ubuntu:15.10: 这是指用 ubuntu 15.10 版本镜像为基础来启动容器
    • /bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash

Run Examples

Bash
sudo docker run -it --rm --privileged \
    -v /dev/bus/usb:/dev/bus/usb \
    -v /home/gavin/projects/ml/rk/:/rknn_tk \
    rknn-toolkit2:1.6.0-cp38 /bin/bash

with ROS

  • ros-kalibr

    Bash
    sudo docker run --name=ros-kalibr -it --rm -e DISPLAY=$DISPLAY \
      -v /tmp/.X11-unix:/tmp/.X11-unix \
      davvdg/ros-kalibr \
      /bin/bash
    

  • HKUST-Aerial-Robotics/VINS-kidnap

    Bash
    docker run --runtime=nvidia -it \
            --add-host `hostname`:172.17.0.1 \
            --env ROS_MASTER_URI=http://`hostname`:11311/ \
            --env ROS_IP=172.17.0.2 \
            --env CUDA_VISIBLE_DEVICES=0 \
            --hostname happy_go \
            --name happy_go  \
            mpkuse/kusevisionkit:vins-kidnap bash
    

Params

Bash
--net=host

# or
--add-host `hostname`:172.17.0.1 \
--env ROS_MASTER_URI=http://`hostname`:11311/ \
--env ROS_IP=172.17.0.2 \
--hostname happy_go \
--name happy_go  \

Container

  • view

    Bash
     sudo docker ps -a
    

  • start & stop container

    Bash
    # start container and enter
    docker start -i <Container-ID>
    
    # restart
    docker restart <Container-ID>
    
    # stop
    docker stop <Container-ID>
    

  • we can also open another terminal for a running container with the following command

    Bash
    docker exec -it <Container-Name/ID> bash
    
    or
    Bash
    docker attach <Container-Name/ID>
    

  • commit to update IMAGE

    Bash
    sudo docker commit [-m="update dataset" -a="cggos"] <Container-ID> <REPO-NAME:TAG>
    

  • history

    Bash
    sudo docker history <IMAGE-ID>
    

  • delete

    Bash
    sudo docker rm <container-name/ID>
    
    # stop all containers
    sudo docker container stop $(docker container ls -aq)
    # remove all stopped containers
    sudo docker container prune
    

  • copy file

    Bash
    # host to container
    docker cp foo.txt <Container-ID>:/foo.txt
    
    # container to host
    docker cp <Container-ID>:/foo.txt foo.txt
    

Docker Hub

Bash
sudo docker login

sudo docker inspect <USER/REPO-NAME:TAG>

sudo docker push <USER/REPO-NAME:TAG>