Article directory
Preface
Earlier we learned the basic definition of Docker and how to install Docker. Next we will understand the basic operations of Docker. In the previous article, click Initial Docker
1. Mirror operation
1.1 Composition of the image
- The name of the image is generally divided into two parts: [repository]:[tag]
- When no tag is specified, the default is latest, which represents the latest version of the image.
When you want to use a certain image on Docker, you can learn about it through DockerHub (equivalent to a Docker warehouse), such as how to pull it and how to use it.
1.2 Mirror operation
Through the above figure, we can basically understand some basic operations of mirroring
-
docker build: It can help us build some custom images. The Dockerfile is equivalent to a text file, which contains instructions one by one. These instructions are used to describe what to execute to build the image.
-
docker images: View the images you have pulled
-
docker rmi image name or image id: delete the corresponding image
docker rmi nginx:latest
Tips: When we suddenly forget the parameters of a certain command during daily use, the help command is very helpful.
docker 命令 --help
The following is the help document for deleting images
-
docker save [Options] IMAGE: Save an image as a compressed package.
Options: You can write -o to indicate the corresponding output file name, and Image indicates the image you want to package.
docker save -o nginx.tar nginx:latest
-
docker load: Load a compressed package as an image.
After you package an image into a compressed package, you can load your image on the docker of other services through the dokcer load command.docker load -i nginx.tar
-
dokcer push : Push the image to the service
-
docker pull : Pull the image from the service
2. Container operation
2.1 Basic operations of containers
From the above, we know how to pull, load, save, and delete images. But the image is just a carrier. Only when we run the image into a container can we really use it.
The following are the basic operations of containers:
-
docker run : Run the image as a container.
We can see that the following is a help document to query its related parameters. We can see that there are many parameters and it is difficult to understand. At this time, we can query specific usage examples on Doker Hub for the specific image running .
Below are multiple usage examples of Nginx on Docker Hub.
But this still seems a bit difficult. We can learn more about docker run through the specific parameters in the following command.$ docker run --name some-nginx -d -p 8080:80 nginx
- –name: Give the container a name, such as some-nginx
- -d: Run the container in the background
- -p: Maps the host port to the container port. The left side of the colon is the host port and the right side is the container port.
- nginx: image name
-
docker stop container id : Stop the container
-- 其中-t可以不加,它表示多久后关闭 docker stop -t 12 cc4064feb479
-
docker start container id : start a container
-
docker ps : View running containers
It also has some other parameters, which can be consulted and learned by yourself through docker ps --help -
dokcer logs container id: query the logs of the corresponding container
docker logs -f cc4064feb479
-
docker exec -it [container name] [command to be executed]
enters the container and executes a commanddocker exec -it mn1 bash
docker exec: Enter the contents of the container and execute a command
-it: Create a standard input and output terminal for the currently entered container, allowing us to interact with the container
mn1: The name of the container to be entered
bash: The command to be executed after entering the container, bash is A linux terminal interactive command -
docker rm [-f] container id : delete container
docker rm -f cc4064feb479
2.2 Summary of container operations
- docker run image: run a container
- –name: Specify container name
- -p: Specify port mapping
- -d: Let the container run in the background
- Command to view container logs:
- docker logs
- Add the -f parameter to continuously view the logs
- View container status:
- docker ps
- The -a parameter can view all containers (including those that are not running)
- Delete container
- docker rm
- The -f parameter can force the deletion of running containers
- Stop container
- docker stop
- Enter the container
- docker exec -it [container name] [command to be executed]
Summarize
I hope that through this article you can understand the basic operations of images and containers. In fact, remembering the commands is not the most critical. The important thing is that you must learn to use – help to understand the parameters of these operations. When the help document has a lot of content and is difficult to understand, we can use docker hub to find relevant application examples. How to use it is important, but the method and approach to learning is the more important process.