Some operation skills and common commands of Docker

Original link: https://tstrs.me/result/_niAZIcBIbxacD4Foc2A

Some commands just can’t be memorized. Every time I need to use them, I have to search the Internet. I simply record them on the blog and check them when necessary.

Common commands

View running containers: We can use the command docker ps to view running containers. If we want to see all containers, including stopped ones, we can use:

 docker ps -a

Enter the running container: sometimes you need to execute commands in the container or view the files inside the container, etc., we can use the following command to enter the bash terminal of the container.

 docker exec -it container_name /bin/bash

View the logs of the container: We can use the following command to view the standard output (stdout) and standard error (stderr) of the container.

 docker logs container_name

Pull the image from Docker Hub:

 docker pull image_name:image_tag

View images: We can use the command docker images to view existing images locally.

Delete containers and images: We can use docker rm container name to delete containers and docker rmi imagename to delete images.

Build a mirror: We can use the Dockerfile to define a new mirror and build a new mirror with docker build -t image_name command.

Restart the Docker service: If Docker encounters problems, we can restart the Docker service through systemctl restart docker .

Batch operation command

List all container IDs

 docker ps -aq

stop all containers

 docker stop $(docker ps -aq)

remove all containers

 docker rm $(docker ps -aq)

remove all mirrors

 docker rmi $(docker images -q)

restart all containers

 docker restart $(docker ps -a | awk '{ print $1}' | tail -n +2)

copy files

 docker cp mycontainer:/opt/file.txt /opt/local/
docker cp /opt/local/file.txt mycontainer:/opt/

Remove all unused mirrors

 docker image prune --force --all

remove all stopped containers

 docker container prune -f

This article is transferred from: https://tstrs.me/result/_niAZIcBIbxacD4Foc2A
This site is only for collection, and the copyright belongs to the original author.