Search the mirror
Docker Search Specifies the image nameCopy the code
For example, search for centos related mirrors
docker search centos
Copy the code
- NAME: indicates the NAME of the mirror repository source
- DESCRIPTION: indicates the DESCRIPTION of the mirror
- OFFICIAL: Is it OFFICIAL with Docker
- Stars: Similar to Github star, which means to like or like.
- AUTOMATED: Automatic construction.
Pull the mirror
Pull the image to the local server. The default TAG is Laster, the latest version
Docker pull image nameCopy the code
For example, pull redis:latest
docker pull redis:latest
Copy the code
Default to the latest laster
View all mirrors
docker images
Copy the code
Description of each option:
- REPOSITORY: represents the REPOSITORY source of the image
- TAG: indicates the TAG of the mirror
- IMAGE ID: indicates the ID of an IMAGE
- CREATED: indicates the time when a mirror is CREATED
- SIZE: indicates the mirror SIZE
View the middle tier image:
docker images -a
Copy the code
If there is a reuse middle layer between different images, you will see some images without image names and labels. Many images depend on these middle layers, so the middle layer images cannot be deleted at will.
Remove the mirror
When we want to remove unnecessary images, such as hello-world images, we can do:
docker rmi hello-world
Copy the code
Forcibly Deleting a Mirror
If there is a container based on the image, you need to delete the corresponding container before deleting the image. To forcibly delete the image, add the -f parameter:
docker rmi -f <image name>
Copy the code
Clear all hover images
When viewing local images using Docker Images (without the -a parameter), we may also find some images without the image name and label:
Different from the middle layer image mentioned above, this image is called a dangling image. Dangling image can be generated in several situations, such as forcibly deleting an image that is already running, or when docker pull command is used to update the image, the image name and tag are transferred to the new image. The old image will become a dangling image. In addition, if the docker build fails to build the image, the dangling image will also be generated. Generally speaking, the hover image is no longer practical and can be deleted at will. A command can clear all the hover images:
docker image prune
Copy the code
Export the mirror
We use the Docker pull command to download the image from the Docker Hub to the local, if the target environment can not access the Internet cannot download, we can directly export the image file:
docker save -o centos_demo.tar centos
Copy the code
- The -o argument is followed by the output file name.
- Centos is the name of the image to be exported.
Import mirror
Then copy the image file to the target environment and import the image:
docker load -i centos_demo.tar
Copy the code
After importing, you can view the image through Docker Images.
Modify the image name and label
If we want to change the image name to redis.1, execute:
docker tag redis:latest redis.1
Copy the code
You can see that there are two images because the same REPOSITORY can have multiple tags representing different versions of the REPOSITORY. For example, the Redis REPOSITORY has multiple versions of laster, 1.0, etc. We use REPOSITORY:TAG to define different images.
Container operation
Start the container
docker run -it redis
Copy the code
Parameter Description:
- -i: interactive operation.
- – t: terminal.
- Centos: centos mirroring.
Check the container
To view all containers and their status, run the following command:
docker ps -a
Copy the code
Represents the container health, when was it created, how long has it been running and stopped
Start a stopped container
In the figure above, centos has stopped
Start the container
Start a stopped container with docker start:
docker start 9e00da244f59
Copy the code
The background
In most scenarios, we want the Docker service to run in the background. We can specify the container mode by using -d.
file
Note: the -d parameter does not enter the container by default. To enter the container, use the docker exec directive (described below).
2.5 Stopping a container
Docker stop < container ID>
docker stop 9e00da244f59
Copy the code
Docker restart < container ID>
docker restart 9e00da244f59
Copy the code
Into the container
With the -d argument, the container goes into the background after it starts. To enter the container, use the following command:
- docker attach
- Docker exec: It is recommended to use the docker exec command, because this will not cause the container to stop.
Remove the container
Delete container using docker rm command:
docker rm 9e00da244f59
Copy the code
You cannot delete a running container
Forcibly deleting containers
docker rm -f 9e00da244f59
Copy the code
Clean up all terminating containers
docker container prune
Copy the code
Run a Web application in real combat
Run a Web application
There’s nothing special about the container we ran earlier. Let’s try to build a Web application using Docker. We will run a Python Flask application in a Docker container to run a Web application.
Loading a Web Image
docker pull training/webapp
Copy the code
Background startup container
docker run -d -P training/webapp python app.py
Copy the code
Parameter Description:
- -d: Allows the container to run in the background
- -p: maps the network ports used inside the container to the host we use
Viewing the WEB Container
docker ps
Copy the code
Open port 32768 on the server
If the security group on the cloud server needs to enable ports and firewalls before accessing WEB applications using a browser:
Parameter Setting Port
You can also set different ports with the -p parameter:
docker run -d -p 5000:5000 training/webapp python app.py
Copy the code
View WEB application logs
Docker logs [ID or name] can be viewed within the container as standard output:
docker logs e018dabe79b791f8f91388c52930f0c1a09b619b485582dfd9eab3878814e56e
Copy the code
or
docker logs e018dabe79b7
Copy the code
Command to introduce
The docker logs command is used to view container logs
The command format
$ docker logs [OPTIONS] CONTAINER Options: -- Details displays more information -f, --follow traces real-time logs --since string displays logs since a timestamp, or relative time, such as 42m (42 minutes) --tail String Displays how many lines of the log from the end, The default is all-t, --timestamps displays timestamps --until string displays logs from before a timestamp, or relative time, such as 42m (42 minutes).Copy the code
Tracking Real-time Logs
docker logs -f e018dabe79b7
Copy the code
View logs generated in the last 30 minutes:
docker logs --since 30m e018dabe79b7
Copy the code
To view logs after a certain time:
docker logs -t --since="2020-03-08T13:23:37" e018dabe79b7
Copy the code
View the processes of the WEB application container
Docker top to view the processes running inside the container:
docker top e018dabe79b7
Copy the code
Check the WEB application
Use Docker inspect to view the underlying information of docker, and return a JSON file recording the configuration and status information of docker container:
docker inspect e018dabe79b7
Copy the code
Stop the WEB application container
docker stop e018dabe79b7
Copy the code
Restart the WEB application container
A stopped container can be started using the docker start command
docker start e018dabe79b7
Copy the code
Query the last container created
Docker ps -l query for last created container:
docker ps -l
Copy the code
We can use the docker restart command to restart the container.
Remove the WEB application container
Delete unneeded containers using docker rm command (delete containers must be stopped)
docker rm e76c997069bf
Copy the code
Removing a running container forces removal of the Docker RM -f
docker rm -f e76c997069bf
Copy the code
The resources
iii30.cn/Ig8w9u