#Docker Installation
sudo apt-get update
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
# Add the package repositories
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit
sudo systemctl restart docker
To Verify if docker is installed
docker run hello-world
To Verify is Docker with NVIDIA GPU suppport is installed
#### Test nvidia-smi with the latest official CUDA image
docker run --gpus all nvidia/cuda:10.0-base nvidia-smi
sudo groupadd docker
# Add your user to the docker group.
sudo usermod -aG docker $USER
# Run the following command or Logout and login again and run (that doesn't work you may need to reboot your machine first)
newgrp docker
# Check if docker can be run without root
docker run hello-world
Docker pull from DockerHub
docker pull <repo:tag>
# Example
docker pull ubuntu:20.04
Build from Dockerfile
#This builds the docker image for us
docker build .
# or
docker build --tag=<put some tag here> .
To List all dockers images in the local machine
docker images
To run the docker image, starts it
docker run -t -v local_path:container_path -d --gpus all --name <some name> <from images>
# Example
docker run -t -v /home/lordgrim/Work/mediapipe:/home/mediapipe -d --gpus all --name mp1 mediapipe:latest]
- -it Interactive mode with pseduo-TTY
- -d to keep it detached and running in background
- -v for shared workspace -v path1:path2 ,path 1 on local system and path2 on container are shared now.
More options for Docker Run can be found here
Check status of containers
docker ps -a
Delete container
docker rm <container_id>
To connect(run bash) to the container (Container id can be obtained from : docker ps
)
docker exec -it <name/container_id> bash
# or
docker attach <name/container_id>
Copy from Local disk to docker
docker cp src <container_id/name>:dst
# Example
docker cp ~/hello.txt f1c9e83a63ce:/workspace/
Live stream of container(s) resource usage statistics
docker stats
List images
docker images
Remove Images (Image id can be obtained from : docker images
)
docker rmi <image_id/repo:tag>
# Force removal
docker rmi --force <image_id/repo:tag>
Kill a running container (Container id can be obtained from: docker ps
)
docker kill <container_id>
Container id can be obtained from : docker ps
docker commit <container_id> <repo:tag>
You can also overwrite on existing images
docker run -it --rm -p 8888:8888 <image>
- --rm Remove the container when it exists ( Not the image , just the container )
- -p : port fowarding conatiner_port:local_port
xhost +
docker run -it -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=unix$DISPLAY <image>
This command can be used to create a new terminal within the same container.
docker exec -it <name/container_id> bash
Example:
If in the case, you are working on ROS in your container. There might arise a situation where you need to open a new terminal, in such cases you can use the above command to create a new terminal.