docker basic commands
This article is reproduced as follows: Original address
1. Check docker version
docker version
2. View docker details
docker info
3. View the docker help command
docker help
If you forget how to use the specific parameters of a command, follow it with – help
- give an example
How to use the parameters of query ps
docker ps --help
4. Start docker
systemctl start docker
5. Close docker
systemctl stop docker
6. Restart docker
systemctl restart docker
7. Self starting docker
systemctl enable docker
8. Check docker running status
Green indicates that it is currently running
systemctl status docker
docker image command
1. View the current image
docker images
2. Search image
Parameter Description:
Filter query results according to the provided parameters
-f , --filter filter Filter output based on conditions provided
# Search command docker search Image name # For example, search jenkins docker search jenkins # For example, search jenkins to download more than 5000 image files docker search -f
- Search jenkins image file
- Search jenkins image files with start more than 5000
3. Pull the image file
Without tag (version number), the latest version of the image in the docker warehouse is pulled. Latest plus: tag is the specified version
docker pull Image name # Pull MySQL docker pull mysql
- When pulling mysql, it should be noted that if the mirrored version information is not added, the latest version is pulled by default
- View the new mysql image file
Let's try to pull an image file with version information
Official website
Version information
- Pull the mysql image and the version is 5.7.37
docker pull mysql:5.7.37
4. Run the image file
# Pull tomcat image docker pull tomcat # Start tomcat directly without any operation docker run tomcat
After starting the tomcat image, you will find that the cursor disappears and nothing can be done. You can use ctrl+c to jump out (Note: Although this method can exit the container, this command is wrong. See the container command below for details)
5. Delete the image file
The precondition is that the current image file is not used by any container, just like someone in the house. You can't directly open the excavator for demolition
#Delete a single mirror file docker rmi -f Image name/image id # Delete the latest mysql image file docker rmi -f 5b4c624c7fe1 # You can try the following by yourself #Delete multiple image ID s or separate images with spaces docker rmi -f Image name/image ID Image name/image ID Image name/image ID #Delete all images - a means to display all and - q means to display only ID s docker rmi -f $(docker images -aq)
- Force deletion of mirrored files
docker images rm Image name/image ID
That's all for the basic command of the image. A more complex docker run command will be used below to start the container according to the image
6. Save the image file
Save our image as a tar compressed file to facilitate image transfer and saving, and then load the image on any server with docker installed
Syntax:
docker save Image name/image ID -o Where and by what name is the image saved # Back up our jenkins image docker save 55860ee0cd73 -o /tmp/jenkinsbeifen.tar
- Backup jenkins
- View backup files
7. Load the image file tar
Load image files migrated from other places
grammar
docker load -i Image save file location # Load jenkins container docker load -i /tmp/jenkinsbeifen.tar
- Load container
docker container command
1. View the list of currently running containers
docker ps
2. View all containers - including running and stopped containers
docker ps -a
3. Run a container
grammar
# -it means interactive startup with the container, - d means that the container can be run in the background (daemon) # --Name name of the container to run / bin/bash interaction path docker run -it -d --name Alias image name to be taken:Tag /bin/bash
Let's take redis as an example to pull and run the redis container
- Create and run containers
#1. Pull the redis image docker pull redis:5.0.5 #2. Command start docker run -it -d --name redis001 redis:5.0.5 /bin/bash
- View container
This time we see
redis uses port 6379. Can we access it when we turn off the firewall or turn on the security group?
# netstat is a console command. It is a very useful tool for monitoring TCP/IP network. It can display routing table, actual network connection and status information of each network interface device netstat -untlp
- Check the port and no 6379 is found
Use the redis desktop manger connection to test
Why not? It has been confirmed that the redis container in docker is running and occupies port 6379?
Because: the occupied 6379 port is only the internal port in the container and has no connection with the 6379 port of the host. We can access this redis example through the host Ip:6379, which is naturally not found!
By default, we cannot directly access the container through the port of the host (the server where docker is installed), because the port opened by the docker container has no connection with the port of the host
If the external wants to access the container, the port in the container must be bound with the port of the host. This formal concept is called container port mapping
With port mapping, we can bind the host port to the container port. For example, we can bind the 6379 port of the host to the redis 6379 port of the container, and then access the host Ip:6379 to access the corresponding container!
4. Container port mapping
Next, we will demonstrate the container port mapping
Stop the container first
docke stop container id/Container name # Stop redis docker stop redis001
Then delete the container
# Delete container docker rm -f fb4e089b9a67
Port mapping syntax
-p host port: container port
Still use the redis image in front to try to map port 6379 to 8888 of the server. If it is successful, we will access the container redis002 in our docker when we access port 8888 of the server
-p 8888:6379 parsing
Map port 6379 inside the container to port 8888 of docker host (the server where docker is installed is its array machine)
Then you can access port 6379 of docker container through external access to port 8888 of host computer
docker run -itd --name redis002 -p 8888:6379 redis:5.0.5 /bin/bash
Then we'll reconnect and find it successful
So are there any restrictions on container port mapping?
Yes, although the environment between each container is isolated, each port of the host machine is one. Port 8888 is bound by redis002 container, so all other containers can't use port 8888!!!
5. Enter the container
Mode 1:
# grammar docker exec -it Container name/container ID /bin/bash #Enter the redis001 container in front docker exec -it redis001 /bin/bash
- Enter container
Mode 2:
docker attach Container name/container ID # Enter redis docker attach redis001
- Enter container
6. Exit the container
#-----When you exit directly without adding - D (persistent running container), the container will be closed exit # Elegant proposal - the container will not be closed whether the - d parameter is added or not Ctrl + p + q
- immediate withdrawal
- Graceful exit
7. Stop the container
docker stop container ID/Container name # Stop redis001 docker stop redis001
8. Restart the container
docker restart container ID/Container name # Restart redis001 docker restart redis001
9.kill container
docker kill Container name/container id # Kill redis002 container docker kill redis002