Docker common commands¶
Delete docker objects¶
After we created docker image, container, volume, network, we may need to delete them to clear the working space
Purging all unused or dangling resources¶
The first command is system prune, which will delete all unused Docker objects:
- containers
- images
- networks
- volumes
Below are some command example
# remove all unused objects
docker system prune
# with the --filter option, we can filter which objects we want to delete.
# the below example deletes containers that have been stopped for more than 24 hours.
# -a option can clear the build cache and the intermediate image.
docker system prune -a --filter "until = 24h"
Deleting container¶
Below commands only delete containers
# remove a single container
docker rm <container_id/name>
# remove multiple containers
docker rm container_id1 container_id2
# remove all stopped containers
docker container prune
# when you run a container, you can add option -rm to delete the container when it exists.
docker run -rm image_id/name
# show all container id as a list
docker ps -a -q
# stop all container
docker stop $(docker ps -a -q)
# remove all container
docker rm $(docker ps -a -q)
Delete container image¶
# delete a docker image
docker rmi image_name/id
# delete multiple docker image
docker rmi image_id1 image_id2
# remove image by using tag
docker rmi -f tag_name
# remove all dangling image
docker image prune
# remove all unused images(not linked to an existing container)
docerk image prune -a
# remove all image
docker rmi $(docker images -a -q)
A dangling image just means that you've created the new build of the image, but it wasn't given a new name. So the old images you have becomes the "dangling image". Those old image are the ones that are untagged and displays "
" on its name when you run docker images.
Delete container volume¶
# delete one volume by using its name
docker volume rm volume_name
# delete multiple volume
docker volume rm vol1 vol2
# remove all unused volume
docker volume prune
# add filter to remove
docker volume prune --filter "label=test"
Delete docker networks¶
docker network rm network_name/id
docker network rm net1 net2
# remove all unused network
docker network prune
# add a filter
docker network prune --filter "until=24h"
Remove docker compose deployment¶
The below command example removes containers, images, volumes, networks, and undefined containers.
# --rmi all Remove all images
# -v Remove the named volumes declared in the volumes section of docker-compose.yml and the anonymous volumes attached to the container
# --remove-orphans Remove containers not defined in docker-compose.yml
docker-compose down --rmi all -v --remove-orphans
You can not a delete a volume in use, if you try to delete, an error message
volume is in usewill be printed
Docker build¶
Docker build takes a docker file and build a docker image
The -t option is recommended.
# general form
docker build -t <image-name>:<tag-name> <docker-file-path>
# We have write a docker file and config file in test_image
test_image/
├── config.sh
└── Dockerfile
# an example
docker build -t my-img:0.0.1 ./test_image
Dockerfile
FROM busybox:latest
LABEL MAINTAINER=pengfei.liu@casd.eu
LABEL version="1.0"
COPY config.sh /etc/spark/config.sh
RUN cat /etc/spark/config.sh
The content of the config.sh
export JAVA_HOME=/opt/java/java_8
Check the result
docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
my-img 0.0.1 b4ecf828f680 27 seconds ago 1.24MB
Docker tag¶
With the above command we tagged our image with my-img:0.0.1. If we want to push it to a remote repository, we need to change the tag
# general form
docker tag old_name[:TAG] new_name[:TAG]
For different image registry, the tag convention is different. We will show two different example
- docker hub
- harbor
Tag and push the local image to docker hub¶
The general form for the docker hub tag is my-img:0.0.1 to dockerhub liupengfei99/test:v2
The repo
testmust be created before push
# change the tag for docker hub
docker tag my-img:0.0.1 liupengfei99/test:v2
# check the result
docker images
# output
REPOSITORY TAG IMAGE ID CREATED SIZE
my-img 0.0.1 b4ecf828f680 13 minutes ago 1.24MB
liupengfei99/test v2 b4ecf828f680 13 minutes ago 1.24MB
# login to docker hub
docker login
# push the image
docker push liupengfei99/test:v2
In your docker hub web ui, you should see the newly pushed image
Tag and push the local image to Harbor¶
In below example, we will push the local image my-img:0.0.1 to harbor reg.casd.local/test/test-img:v1
The general form for the harbor tag is # change the tag for harbor
docker tag my-img:0.0.1 reg.casd.local/test/test-img
# check the new tag
docker images
# output
REPOSITORY TAG IMAGE ID CREATED SIZE
my-img 0.0.1 b4ecf828f680 25 minutes ago 1.24MB
liupengfei99/test v2 b4ecf828f680 25 minutes ago 1.24MB
reg.casd.local/test/test-img latest b4ecf828f680 25 minutes ago 1.24MB
# login to harbor
docker login reg.casd.local
# push the image
docker push reg.casd.local/test/test-img
Now you can check your harbor web UI, in the project test, you should see the image test-img.
Docker login¶
In the above command, we have used docker login. This will create create a file ~/.docker/config.json which stores the user credential to connect to the remote server.
{
"auths": {
"demo.goharbor.io": {
"auth": "changeMe"
},
"https://index.docker.io/v1/": {
"auth": "changeMe"
},
"reg.casd.local": {
"auth": "changeMe"
}
}
}
Note the user credential is stored in plain text in the config.json. It's not recommended for production envrionment. Please use this doc to setup a secret store.