Docker study notes (two)-a detailed introduction to the use of docker containers

Use of Docker containers

1. Get the mirror

There is no mirror locally, pull the mirror directly from the docker hub

docker pull ubuntu

2. Start the mirror

Start the mirror in command line mode:

docker run -it ubuntu /bin/bash

-i: interactive operation
-t: terminal

Start the container and run it in the background:

docker run -itd --name ubuntu-test ubuntu /bin/bash 

-d: specify the container to run in the
background -name: specify the NAMES of the container

3. Exit the current container

exit或ctrl+D

4. View the running container

docker ps

5. View all containers

docker ps -a

6. Stop a container

docker stop <容器ID> 或者是 <容器名>

7. Restart a stopped container

docker restart <容器ID> 或者 <容器名>

8. Two ways to enter the container

  • docker attach
  • docker exec: The second method is recommended, because after this method enters the container and runs, it will not stop the container when exiting


Insert picture description hereNote for attach command : If you exit from this container, it will cause the container to stop.


Insert picture description hereNote for exec command : If you exit from this container, it will not stop the container, which is why it is recommended that you use docker exec.

Description: The difference between entering a container and running a container:

1.运行容器:docker run
在第一次使用某个容器时首先需要使用docker使该容器运行起来,运行的方式也有多种,大多数情况下,
我们选择后台运行的方式
2.进入容器:docker exec
当容器以后台方式运行时,我们可以使用docker exec或docker attach的方式重新进入后台运行的容器

9. Export and import containers

Function: Some changes have been made when using the container, and I want to save the changed container as a snapshot to facilitate the next import

Export container snapshot
docker export 1e560fca3906 > ubuntu.tar
Import container snapshot
cat docker/ubuntu.tar | docker import - test/ubuntu:v1

通过指定 URL 或者某个目录来导入
docker import http://example.com/exampleimage.tgz example/imagerepo

10. Delete the container

Special Note : Be sure to confirm that the container has stopped running before deleting the container

docker rm -f 1e560fca3906

The following command can clean up all containers in a terminated state

docker container prune

Guess you like

Origin blog.csdn.net/m0_45388819/article/details/109546568