Docker usage commands and tricks

Now that Docker's basic commands and usage methods have been built, of course we have to learn, and some tips such as batch operation image acceleration and how to find suitable Docker-Images are all covered in this section.

attach:

Meow's blog: w-blog.cn

Official Git address: https://github.com/moby/moby

1. Docker basic operation commands

Docker container lifecycle management

  • docker run executes a command in a new container
  • docker start starts one or more stopped containers
  • docker stop stops a running container
  • docker restart restarts a running container
  • docker kill kills a running container
  • docker rm removes one or more containers
  • docker pause pauses all processes of a container
  • docker unpaunse restores all processes of a container

Docker container operation and maintenance

  • docker ps list containers
  • docker inspect lists low-level information about a container or image
  • docker top shows the processes running in a container
  • docker attach attaches a running container
  • docker events get real-time events from services
  • docker log to get the log of a container
  • docker wait blocks until a container stops, then prints its exit code
  • docker export exports a container's filesystem as a tar file
  • docker port lists the ports mapped by the container

docker version management

  • docker commit creates a new image from a container
  • docker cp copies a file or directory from a container to a local directory or stdout
  • docker diff shows changes to a container's filesystem

docker image repository

  • docker login to log in to the warehouse
  • docker pull pulls the image
  • docker push push image
  • docker search search for images

Docker local image management

  • docker images list local images
  • docker rmi delete the local image
  • docker tag tag the local image
  • docker build builds a local image through Dockerfile
  • docker history lists the history of the image
  • docker save saves the local image as a tar file
  • docker import imports images through tar
  • docker load loads the tar image

docker other commands

  • docker info
  • docker version

2. Tips

Bulk operations

Many times it is found that it may be necessary to clean up the Docker environment, but it is too troublesome to stop the containers one by one and delete them one by one. Use the following commands to quickly process batch deletions

> docker ps // 查看所有正在运行容器
> docker stop containerId // containerId 是容器的ID
> docker ps -a // 查看所有容器
> docker ps -a -q // 查看所有容器ID
> docker stop $(docker ps -a -q) //  stop停止所有容器
> docker rm $(docker ps -a -q) //   remove删除所有容器
> docker rmi $(docker images -a -q) //   删除所有的镜像

hub.docker

Since we use docker, we will use a lot of the environment mentioned by others. Often we go to https://hub.docker.com/ to find the mirror we need

Search for the mirror you need

By default, docker will pull from hub.docker when pulling an image. Use the following command to pull the first image above

docker pull richarvey/nginx-php-fpm

Domestic mirror warehouse and mirror source

For domestic mirror warehouses, you can apply for an Alibaba Cloud account and use its free Docker warehouse directly

The corresponding Docker source address needs to go to Alibaba Cloud Container Image Management to apply for and use other domestic Docker sources (if it is not configured, the subsequent K8S installation is very slow)

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://xxxxxx.mirror.aliyuncs.com"]
}
EOF

# 重启
sudo systemctl daemon-reload
sudo systemctl restart docker

Note: The author has limited ability and I hope that everyone can point out the wrong places, and I hope to communicate more!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325378600&siteId=291194637