Docker technology practice - 02

Keywords: Linux Docker

Docker image operation practice

1. Download Image -- docker pull image name

	docker pull hello-world

2. View the image file

docker images
[root@localhost docker]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED        SIZE
hello-world          latest    feb5d9fea6a5   7 days ago     13.3kB

3. View image details

  • Syntax: docker inspect image name or image id
docker inspect hello-world

4. View image history

  • A mirror image is composed of multiple layers, so how do we know the specific content of each layer? The docker history command can list the creation information of each layer, for example:
    [root@localhost docker]# docker history hello-world
    IMAGE          CREATED      CREATED BY                                      SIZE      COMMENT
    feb5d9fea6a5   7 days ago   /bin/sh -c #(nop)  CMD ["/hello"]               0B
    <missing>      7 days ago   /bin/sh -c #(nop) COPY file:50563a97010fd7ce...   13.3kB
    

5. Export image file

  • Image export (download the image file in the linux system to the local - such as window) and export it to others for use
    docker save  hello-world | gzip > hello-world.tar.gz  
    

6. Delete the image file

  • Syntax: docker image rm image name or image id
    docker image rm hello-world
    

7. Import image

  • Image import (to be executed in the directory where the hello-world.tar.gz file is located)
    docker load < hello-world.tar.gz  
    

8. Run the image file

  • Start container operation based on image
    docker run hello-world
    

Docker container operation practice

  • This time, take CentOS image as an example to explain the basic operation of container.

9. Download Image

  • Download the CentOS image through the docker pull command, for example:
    docker pull centos:7
    
  • After downloading, view the image file of CentOS7
    [root@localhost docker]# docker images
    REPOSITORY           TAG       IMAGE ID       CREATED        SIZE
    centos               7         eeb6ee3f44bd   2 weeks ago    204MB
    

10. Create and start the container

  • Basic syntax:
    docker run -it xxxx bash
    
  • Of which:
    • xxxx - image name, or the first few digits of image id,
    • -it these are two parameters (- i for interactive operation, - t for terminal)
    • bash means to enter the operation terminal and perform relevant operations based on interaction (such as executing linux related instructions).
  • Case: start and run centos7 image through docker
    docker run -it centos:7 bash
    

11. View containers in Docker

  • View containers in docker operation (to execute docker instructions on the host)
    docker ps
    
  • Note: if the docker instruction is executed in the container, the following problems will occur, for example:
  • View all containers in docker operation, - a means all.
    docker ps -a
    

12. View the container logs

  • This command is very important when checking the container startup and operation log. If the container is not started, you should use this command to check the error log.
    docker container logs 5a3  #5a3 is its own container id (usually the first three digits)
    
  • Note: when viewing the operation log of the container, the container should be in a running state

13. Stop or restart the container

  • Stop the container, the code is as follows:
    docker container stop 5a3  #5a3 is its own container id
    
  • Restart the container with the following code:
    docker container restart 5a3  #5a3 is its own container id
    

14. Enter (exec) the specified container

  • When the container is running, you can use the docker exec command to enter the container, for example:
    docker exec -it 5a3 bash #5a3 is the container id
    
  • Note: if the container is in a non running state, the following problems will occur when you execute docker exec to enter the container:

15. Exit from container

  • If you enter the started container from the host, you need to use the exit command to exit the container, for example:
    [root@localhost ~]# docker exec -it 5a3 bash
    [root@5a3b3fe3dea5 /]# exit
    exit
    [root@localhost ~]# 
    

16. Delete (rm) container

  • If the container is not used, you can delete it, for example:
    docker container rm 5a3  #5a3 is the container id
    
  • Note: if the container is running to delete, the following problems will occur, for example:
  • If you want to delete a running container, you need to add the - f parameter to force the deletion, for example:
docker container rm -f 5a3 #5a3 is the container id
  • Clean up all containers in the terminated state, for example:
docker container prune

Docker data management practice

Posted by Garcia on Fri, 01 Oct 2021 11:14:32 -0700