Common Docker commands

  • Documentation: docs.docker.com/reference/

1. Help command

$ docker version        # show docker version information
$ docker info           Display docker system information including the number of images and containers
$ docker COMMAND --help # universal command
Copy the code

2. Run the mirror command

  1. Docker Images view images on all local hosts
$ docker images [OPTIONS] [REPOSITORY[:TAG]]

#For example,
$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
tomcat       latest    46cfbf1293b1   10 days ago   668MB
nginx        latest    4cdc5dd7eaad   3 weeks ago   133MB

#explainREPOSITORY REPOSITORY source TAG TAG of the IMAGE IMAGE ID ID of the IMAGE CREATED Time when the IMAGE was CREATED SIZE IMAGE SIZE#Optional OPTIONS-a, --all Lists all mirrors. -q, --quiet displays only the Id of the mirrorCopy the code
  1. Docker Search searches for images
$ docker search [OPTIONS] TERM
 
#Search for mysql mirror
$ docker search mysql
#Optional, filter by cache searchFilter =stars=3000 #Copy the code
  1. Docker pull pulls the image
$  docker pull [OPTIONS] NAME[:TAG|@DIGEST]

#1. Download the latest version by default
$Docker Pull Image name [:tag] Version Latest version by default
$ docker pull mysql

#2. Specify the version to download. The official document must have the version
$Docker pull mysql: 5.7
Copy the code
  1. Docker RMI delete image
#Example Delete a mirror with a specified ID
$Docker Rmi-f Image ID 
#Delete multiple mirrors with the specified ID.
$Docker rmi -f Image ID Image ID Image ID Image ID Image ID
#Delete all mirrors recursively
$Docker rmi-f $(docker images-aq) means the id of all images
Copy the code

3. Container command

  • You need an image to create a container
  1. Create a new container and start
$ docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
 
 #Parameters that--name= "name" Container name tomcat01 -d Run in background mode it Run in interactive mode to view the contents of the container -p Specify the port of the container -p 8080:8080 -p IP: host port: container port -p Host port: Container port (common) -p Container port Container port -p Randomly specifies the port
#The test starts and enters the container
$ docker run -it centos /bin/bash 

#Out of the container
$ exit
Copy the code
  1. Lists all running containers
$ docker ps [OPTIONS]
#optionalEmpty # list currently running containers -a # List currently running containers with history content -n=? Display the recently created container? -q # displays only the container number
#Lists currently running containers (none below)
$ docker ps   

#Look at the containers that have been running
$ docker ps -a
Copy the code
  1. Out of the container
$ exit            # the container stops exiting
$ Ctrl + P + Q    # The container does not stop exiting
Copy the code
  1. Remove the container
$Docker RM Container IDRun the rm -f command to delete the specified container
$ ocker rm -f $(docker ps -aq)     Delete all containers
$ docker ps -a -q|xargs docker rm  Delete all containers
Copy the code
  1. Start and stop containers
$Docker start Container ID# start container
$Docker restart Container IDRestart the container
$Docker stop Container ID# Stop the current operation container
$ docker killThe container idForce to stop the current container
Copy the code

4. Other common commands

  1. Background startup container
#Run the docker run -d image name command to start the container in the background
#Docker ps found centos stopped
#Common pit: Docker container background run, you must have a foreground process. Docker finds no command and stops
Copy the code
  1. See the log
$ docker logs -f -t --tail 10  # container id has no log 
#According to the log-tf # Display all logs --tail number # Number of logs to displayCopy the code
  1. View the process information in the container
$Docker Top Container ID
Copy the code
  1. View the metadata of the mirror
$Docker inspect Container ID 
Copy the code
  1. Enter the currently running container
#Containers are usually run in the background, requiring you to go into the container and make some configuration changes

#Command 1: 
$ docker exec-it container ID bashshell 
#Command 2:
$Docker Attach Container ID

#Compare the two methods
$ docker exec-it Container ID: After entering a container, a new terminal can be opened and operated in the container
$Docker Attach container ID: Enters the terminal where the container is executing and does not start a new process
Copy the code
  1. Copy files from a container to a host
$Docker cp Container ID: Inside path Destination host path V
Copy the code

This article extracts or summarizes other notes, notes do not involve any commercial purposes, if infringement, please contact promptly