This article has participated in the call for good writing activities, click to view: back end, big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!
Hello everyone, I’m Brother Chen!
Previous article (2300+ words! Install Docker on a different system!) Teach you how to install Docker on the system, today let’s learn the basic use of Docker.
In this article, Brother Chen will introduce various use commands of Docker in detail, such as: create container, pull image, enter container, build environment, exit container, backup, restore and so on. This article explains how to use Docker, make sure you have learned docker, the next tweet will show you how to deploy your own Python built website, crawler, etc. ‘
01. Basic commands
Docker operations are completed by command, Linux directly through the terminal to execute, on Windows we use powershell to execute (Powershell is a command terminal on Windows, not only can execute the command on Windows, You can also execute commands on Linux.
Let’s start with a few basic commands
1. Check the Docker version
docker version
2. View docker details
docker info
3, Check the Docker help command
docker –help
02. Operation commands
1. View the existing mirror
docker images
You can also pull any mirror image, so it’s empty, but just to give you a sense of what each of these fields means
PEPOSITORY: repository source of the IMAGE TAG: TAG of the IMAGE IMAGE ID: ID of the IMAGE CREATED: time when an IMAGE is CREATED SIZE: SIZE of the IMAGE
2. Pull the mirror
docker image pull :
Or docker pull:
Demo: Pull a Python image
docker image pull python
Copy the code
Now look at the mirror
You can see that the pulled Python image already exists ~~~
3. Create and start the container
Docker run-dit –name pythonFirst mirror ID /bin/
Using the Python image created above as an example, create a container
docker run -dit --name pythonFirst d92f72c38f7e /bin/bash
Copy the code
4. View information about all containers
docker container ls
Or docker ps
5. Enter the container
docker exec -it /bin/bash
docker exec -it pythonFirst /bin/bash
Copy the code
You can also enter the container through the container ID
Docker container -it < container ID> bash
docker exec -it ba0cd022165c bash
Copy the code
Either way you can go into the container and then use Python
The container here is actually a Linux system with a Python environment (this is another image with different ids).
6. Exit the container
exit
Or CTRL + d
7. Close, start, and restart the container
Docker stop < container ID>
Docker start< container ID>
Docker restart< container ID>
8. Delete the container
Docker rm < container ID>
Note: Make sure that the container is closed before deleting the date. Otherwise, the container cannot be deleted.
Close the container, delete it, and check whether the container is successfully deleted
Need to delete all containers, add $(docker Ps-a-q)
docker rm $(docker ps -a -q)
9. Delete the mirror
Docker rmI < image ID>
docker rmi d92f72c38f7e
Copy the code
View the mirror, delete the mirror, and finally check whether the mirror is successfully deleted
10. View the container logs
Docker logs -f -t –since=” date “–tail= quantity container id or name
docker logs -f -t --since="2021-07-08" --tail=5 pythonFirst
Copy the code
Description:
-t: displays the date when a log is generated. –since: displays logs generated after a specified date. –tail=5: displays the last five logs
03. Container backup and restore
1. Backup containers
Docker commit -p < container ID> Backup name
Take a look at the container you want to back up
Current mirror
Start backup (user-defined backup name: chenge_docker_backup)
docker commit -p 50af135a4ba8 chenge_docker_backup
Copy the code
Look at mirror
You can view that the backup is successful ~~~, and export the backup image to the local PC
docker save chenge_docker_backup > d:\chenge_docker_backup.tar
Copy the code
Export the package to disk D and save it as chenge_docker_backup.tar.
2. Restore the container
Docker load < local container path
Or docker load -i local container path
Delete image chenge_docker_backup before backup
Began to recover
docker load -i d:\chenge_docker_backup.tar
Copy the code
The import is successful, as shown in the following figure:
That concludes the basic introduction to docker!
04, subtotal
This article introduces various commands of docker in detail, such as: create container, pull image, enter container, build environment, exit container, backup, restore and so on. The basic use of docker in the last and this article has been introduced, I believe everyone has learned, the next article directly on Python, I believe docker+ Python must be very fun.
Be sure to try ****! Be sure to try ****! Do try it!