Skip to content

Instantly share code, notes, and snippets.

@aswinkumar1999
Last active December 24, 2024 14:37
Show Gist options
  • Save aswinkumar1999/0cb7ed44e309e542aec0d144d9b0de93 to your computer and use it in GitHub Desktop.
Save aswinkumar1999/0cb7ed44e309e542aec0d144d9b0de93 to your computer and use it in GitHub Desktop.
Docker Commands

Docker

Install Docker and NVIDIA Container Toolkit

Docker

#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

NVIDIA Container Toolkit

# 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

Verifying Installation

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
Possible Error : Write Permission Denied
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

Basic Commands

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>

Docker Commit

Container id can be obtained from : docker ps

docker commit <container_id> <repo:tag>

You can also overwrite on existing images

Port fowarding and Jupyter Notebooks

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

GUI and Output Display

xhost +
docker run -it -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=unix$DISPLAY <image>
@rsharanesh2002
Copy link

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.

@aswkumar99
Copy link

Converting Docker containers to Singularity 2.x Images

Singularity 3.x = SIF ( Singularity Image Format )
Singularity 2.x = SIMG ( Singularity IMG - Image )

sudo docker run -t --rm --cap-add SYS_ADMIN -v /var/run/docker.sock:/var/run/docker.sock -v /tmp:/output singularityware/docker2singularity <image_name> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment