Some useful docker commands
Post-installation steps for Linux
# create the group named docker
sudo groupadd docker
# add user to the group
sudo usermod -aG docker $USER
# activate the change to groups
newgrp docker
systemctl start docker
systemctl stop docker
service docker restart
docker -v
or docker --version
show docker version in one line
docker version
show details informations
yum list docker --showduplicates | sort -r
or
yum list docker-ce --showduplicates | sort -r
docker search <image-name>
From How to list all tags of a docker image:
List first 10 tags:
curl 'https://registry.hub.docker.com/v2/repositories/<owner>/<repo>/tags/'|jq '."results"[]["name"]'
Replace / with debian/tags, nvidia/cuda, etc.
List all tags:
i=0
while [ $? == 0 ]
do
i=$((i+1))
curl https://registry.hub.docker.com/v2/repositories/<owner>/<repo>/tags/?page=$i 2>/dev/null|jq '."results"[]["name"]'
done
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id
How to tell Docker to store and run containers on an external drive
chown -R root:root /mnt/external-drive/docker-base
chmod 701 /mnt/external-drive/docker-base
Edit the file /etc/docker/daemon.json
:
{
"graph": "/mnt/external-drive/docker-base"
}
And then service docker restart
.
docker images -a
TO show full image id
docker images --no-trunc
in a directory containing a Dockerfile:
docker build . -t <image-name>
docker build . -t <image-name> -f <dockerfile-name>
you may want to add --no-cache flag in order to make apt update && apt upgrade
in Dockerfile take effect:
docker build . --no-cache -t <image-name>
if you have multiple dockerfiles in a directory, you need to specify your dockerfile name:
docker build . -t <image-name> -f <docker-file-name>
docker commit <container-name> <image-name:tag>
docker pull <image-name:tag>
docker login
docker tag <image-name:tag> <your-docker-hub-id>/<image-name:tag>
docker push <your-docker-hub-id>/<image-name:tag>
docker load -i xxx.tar.gz
docker save -o xxx.tar.gz <image-name>
docker tag <old-image-name> <new-image-name>
docker rmi <image-name>
docker rmi $(docker images -q)
docker ps -a
docker ps --format "{{.ID}}: {{.Ports}}"
or
docker ps --format "{{.Names}}: {{.Ports}}"
docker ps -q
docker rm $(docker ps -aq)
docker inspect -f '{{ .Mounts }}' <container-name>
docker run --name <container-name> -td -p <host-port>:<container-port> \
-v <host-directory>:<container-directory> \
-e <environment-variable-name>=<environment-variable-value> --restart always <image-name>
How can I add a volume to an existing Docker container?
Edit /var/lib/docker/containers/99d...1fb/config.v2.json
,
add a new volume into "MountPoints":{}
.
And then service docker restart
.
Is it possible to show the restart policy of a running Docker container?
docker inspect -f "{{ .HostConfig.RestartPolicy }}" <container-name>
[docker - how do you disable auto-restart on a container?](https://stackoverflow.com/questions/37599128/docker-how-do-you-disable-auto-restart-on-a-containerdocker update --restart=no)
docker update --restart=no <container-name>
To update all containers:
docker update --restart=no $(docker ps -a -q)
docker start -i <container-name>
docker exec -it <container-name> bash
<ctrl+p>
and then <ctrl+q>
docker stop <container-name>
docker rm <container-name>
docker rm -f <container-name>
docker ps -a | grep Exit | cut -d ' ' -f 1 | xargs docker rm
docker rename <old-container-name> <new-container-name>
docker-machine create --driver virtualbox <your-vm-name>
docker-machine ls
docker-machine start <your-vm-name>
docker-machine stop <your-vm-name>
docker-machine rm <your-vm-name>
docker-machine ssh <your-vm-name> "<command>"
docker-machine scp <filename> <your-vm-name>:~
eval $(docker-machine env <your-vm-name>)
docker-compose up
docker-compose -f <docker-compose.yml> up
docker-compose images
docker-compose down
docker-compose ps
docker-compose rm
docker-compose scale <service-name>=<num>
docker swarm init
docker swarm join \
> --token SWMTKN-x-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxx \
> \<manager-ip>:2377
This command is showed when you execute docker swarm init
on your swarm manager, so don't worry about the token.
docker node ls
For worker node:
docker swarm leave #from swarm worker
docker node rm <NODE> #from swarm manager
or
docker node rm -f <NODE> #from swarm manager
docker swarm leave --force
success
docker node demote <NODE>
gives "Error response from daemon: rpc error: code = 9 desc = attempting to demote the last manager of the swarm"
docker node rm -f <NODE>
gives "Error response from daemon: rpc error: code = 9 desc = node ozsxfaadni1glz9yrl2w4o9kl is a cluster manager and is a member of the raft cluster. It must be demoted to worker before removal"
docker stack deploy -c <docker-compose.yml> <service-name>
If you have a .env file, you may want to use the following command instead:
docker stack deploy -c <(docker-compose config) <service-name>
docker stack ls
docker stack services <service-name>
docker stack ps <service-name>
docker stack rm <service-name>
docker service ls
docker service ps <service-name>
docker service inspect <service-name>
docker service scale <service-name>=<N>
docker cp <file-name> <container-name>:/<file-name>
docker cp <container-name>:/<file-name> <file-name>
docker network rm $(docker network ls -q)
docker logs <container-name>
docker run -td --entrypoint=/bin/bash <image-name>
With docker 19.03 installed, then there is no need to use nvidia-docker since NVIDIA GPUs are now natively supported as devices in the Docker runtime.
# 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
From https://docs.docker.com/compose/install/#install-compose:
# Run this command to download the latest version of Docker Compose:
# check https://github.com/docker/compose/releases to replace 1.23.1 with newest version
sudo curl -L "https://github.com/docker/compose/releases/download/1.23.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# Apply executable permissions to the binary
sudo chmod +x /usr/local/bin/docker-compose
# Test the installation
docker-compose --version
# Download the Docker Machine binary and extract it to your PATH
base=https://github.com/docker/machine/releases/download/v0.16.0 &&
curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine &&
sudo install /tmp/docker-machine /usr/local/bin/docker-machine
# Check the installation by displaying the Machine version
docker-machine version
For Ubuntu:
apt-get install virtualbox
From How To Install VirtualBox 5.2 on CentOS 7 / RHEL 7.
# Install header and development tools
yum install -y kernel-devel kernel-headers gcc make perl
# Download and import the Oracle public key
yum -y install wget
wget https://www.virtualbox.org/download/oracle_vbox.asc
rpm --import oracle_vbox.asc
# Download the VirtualBox repository file for CentOS 7 / RHEL 7 and move it into /etc/yum.repos.d/ directory
wget http://download.virtualbox.org/virtualbox/rpm/el/virtualbox.repo -O /etc/yum.repos.d/virtualbox.repo
# Install VirtualBox v5.2
yum install -y VirtualBox-5.2
# Check the status of VirtualBox Linux kernel module service
systemctl status vboxdrv
# Access VirtualBox
virtualbox
To uninstall:
yum remove VirtualBox-<virtualbox-version>.x86_64 # For example, \<virtualbox-version> could be 5.0
nvidia-docker version
The first line will be NVIDIA Docker: <x.x.x>, following are the results from docker version
Note: Not nvidia-docker -v
nor nvidia-docker --version
, they will show the same result as docker -v
and docker --version