Drill with Docker Commands
Step into docker commands with its guide.
Docker commands
Some useful docker commands
Manage Docker as a non-root user
Post-installation steps for Linux
Start docker
Stop docker
Restart docker
Check docker version
docker -v
or docker --version
show docker version in one line
docker version
show details informations
List and sort the versions available in your repo
or
search docker image
get tags of a docker image
From How to list all tags of a docker image:
List first 10 tags:
List all tags:
get docker ip
run docker on an external device
How to tell Docker to store and run containers on an external drive
Edit the file /etc/docker/daemon.json
:
{
"graph": "/mnt/external-drive/docker-base"
}
And then service docker restart
.
Manage docker images
Check docker images
docker images -a
TO show full image id
docker images --no-trunc
Build a docker image
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>
Build a docker image from an existing container
docker commit <container-name> <image-name:tag>
Pull a image from dockerhub
docker pull <image-name:tag>
Push your image to dockerhub
Load a docker image from xxx.tar.gz
docker load -i xxx.tar.gz
Save a docker image to xxx.tar.gz
docker save -o xxx.tar.gz <image-name>
Rename a docker image
docker tag <old-image-name> <new-image-name>
Remove a docker image
docker rmi <image-name>
Remove all docker images
docker rmi $(docker images -q)
Manager docker containers
Check docker containers
docker ps -a
Check port forwarding of containers
docker ps --format "{{.ID}}: {{.Ports}}"
or
docker ps --format "{{.Names}}: {{.Ports}}"
Check docker containers' numeric IDs
docker ps -q
Remove all docker containers
docker rm $(docker ps -aq)
Check mounted directories of a container
docker inspect -f '{{ .Mounts }}' <container-name>
Run a docker image(create and then start a container)
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>
Add new mount to existing but stopped container
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
.
Check container restart policy
Is it possible to show the restart policy of a running Docker container?
Disable auto-restart container
[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)
To update all containers:
Start a existing but stopped container
docker start -i <container-name>
Enter a container
docker exec -it <container-name> bash
Exit a container without stopping it
<ctrl+p>
and then <ctrl+q>
Stop a container
docker stop <container-name>
Remove a container
docker rm <container-name>
Stop and remove a container
docker rm -f <container-name>
Remove all exited containers
docker ps -a | grep Exit | cut -d ' ' -f 1 | xargs docker rm
Rename a container
docker rename <old-container-name> <new-container-name>
Docker machine
Create a docker machine
docker-machine create --driver virtualbox <your-vm-name>
List all docker machines
docker-machine ls
Start a docker machine
docker-machine start <your-vm-name>
Stop a docker machine
docker-machine stop <your-vm-name>
Remove a docker machine
docker-machine rm <your-vm-name>
Execute <command> in <your-vm-name>
docker-machine ssh <your-vm-name> "<command>"
Copy <filename> from local to <your-vm-name>
docker-machine scp <filename> <your-vm-name>:~
Configure <your-vm-name>'s shell to host
eval $(docker-machine env <your-vm-name>)
docker-compose
Build containers for a service
docker-compose up
Build containers for a service, specifying the yml file <docker-compose.yml>
docker-compose -f <docker-compose.yml> up
List images used by the created containers
docker-compose images
Stops containers and removes containers, networks, volumes, and images created by up
.
docker-compose down
List containers
docker-compose ps
Removes stopped service containers
docker-compose rm
Scale docker containers
docker-compose scale <service-name>=<num>
Docker swarm
Make the host a manager
docker swarm init
Let other hosts join the swarm as a worker
This command is showed when you execute docker swarm init
on your swarm manager, so don't worry about the token.
(From manager host)List all nodes in swarm
docker node ls
Remove a worker node
For worker node:
or
Remove the last manager node
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
To deploy a service
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>
Check the services on the swarm
docker stack ls
Check the detail of the service <service-name>
docker stack services <service-name>
Check the machines running <service-name>
docker stack ps <service-name>
Remove the service <service-name>
docker stack rm <service-name>
Docker service
List docker services
docker service ls
Check the machines running <service-name>
docker service ps <service-name>
Check the detail of <service-name>
docker service inspect <service-name>
Scale the docker service <service-name> to <N> replicas
docker service scale <service-name>=<N>
Other commands
Copy a file to and from a container
Remove docker networks
Retrieve the logs of a container
docker logs <container-name>
Make a container hang around(not exit immediately)
docker run -td --entrypoint=/bin/bash <image-name>
Installation
Install docker 19.03
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
Install docker-compose
From https://docs.docker.com/compose/install/#install-compose:
Install docker-machine
Install VirtualBox
For Ubuntu:
From How To Install VirtualBox 5.2 on CentOS 7 / RHEL 7.
To uninstall: