Docker from entry to application (3): Docker common commands

Docker from entry to application (3): Docker common commands

help command

  • docker versionDisplay docker version information
  • decker infoDisplay docker system information, including the number of image containers
  • docker --helpshow docker help command

Common mirror commands

  • docker imagesList
    common parameter descriptions for local mirroring

    • -aList all mirrors, including hidden intermediate mirrors
    • -qShow only image ID
    • --no-truncShow verbose information without truncating output
    • --digestsshow summary
      insert image description here
      insert image description here
  • docker search <oprions> 镜像名称Query
    the description of common parameters of a mirror

    • --no-truncshow full image description
    • -fQuery images based on query conditions, common conditions is-automated: whether to build automatically, is-official: whether to be an official image, stars: number of stars
      insert image description here
      insert image description here
      NAME: image name
      DESCRIPTION: image description
      STARS: number of likes
      OFFICIAL: whether to be an official image
      AUTOMATED: whether to automatically build
  • docker pull 镜像名称[:tag]Download mirror, if no tag is provided, the latest version will be downloaded by default
    insert image description here

  • docker rmi -f 镜像名称或者镜像idDelete image -fis a mandatory deletion parameter, and the image deletion that has been used needs to be added-f

    • Delete a single imagedocker rmi -f 镜像名称:TAG或IMAGE ID
    • Delete multiple mirrors docker rmi -f 镜像名1:tag 镜像名2:tagSeparate multiple mirrors with spaces
    • delete all images docker rmi -f $(docker images -qa)
      insert image description here
      insert image description here

container command

Containers are built based on mirror images. Containers can only be created with mirror images. Similar to the relationship between classes and objects in Java, objects can only be created with classes.

Docker uses containers to run one or a group of applications independently. A container is a running instance created from an image. It can be started, started, stopped, deleted. Each container is an isolated and secure platform.

The container can be regarded as a simple version of the Linux environment (including root user authority, process space, user space and network space, etc.) and the applications running in it.

The definition of a container is almost the same as that of an image, the only difference is that the top layer of the container is readable and writable.

  • docker run [options] image [command][args]Create and start the container

    Description of common options

    • --nameGive the container a new name
    • -d Important , run the container in the background and return the container id, that is, start the daemon container and
      insert image description here
      run it in the background, but find that the container has exited. Note: When a Docker container runs in the background, there must be a foreground process . If the command the container runs is not the command that has been suspended (such as top, tail), it will automatically exit.
      This is a mechanism problem of docker, such as yours The web container takes Nginx as an example. Under normal circumstances, we only need to start the corresponding service when we configure and start the service, such as service nginx start. However, in doing so, Nginx is already running in the background mode, resulting in no running applications in the docker foreground. The container will kill itself immediately after starting in the background, because docker thinks he has nothing to do, so the best solution is to run the program you want to run as a foreground process.
    • -i Run the container in interactive mode, typically -tused with
    • -tReassigns a pseudo-input terminal for the container, typically -iused with
    • -P capital P , random port mapping
    • -p The lowercase p specifies the port mapping, which has the following four formats
      - ip:hostPort:containerPort
      - ip::containerPort
      - hostPort:containerPort
      - containerPort
      insert image description here
      insert image description here
      port mapping, accessing the local port 8083 can access the corresponding port 8080 of tomcat running in Docker
      insert image description here
  • docker psView descriptions of common options for all running docker containers

    • -aShow running and historically run
    • -llast running container
    • -n Xof the last X runs, e.g. -n 3 the last three runs
    • -qShow only the container number
  • exitclose the container and exit

  • ctrl+p+qExit the container without closing it

  • docker start <容器id或容器名>Start the container

  • docker restart <容器id或容器名>Restart the container

  • docker stop <容器id或容器名>Stop container stop: Slowly stop

  • docker kill <容器id或容器名>Force stop the container

  • docker rm <容器id或容器名>delete stopped container -f force delete (stop first then delete)

  • docker rm -f ${docker ps -aq}or docker ps-aq|xargs docker -rmdelete all containers

  • docker run -d<container name> daemonized start container

  • docker logs -f -t --tail 容器idCheck the container log -t join time -f keep adding ----tail last few lines

  • docker top 容器idView container process

  • docker inspect 容器idView the internal details of the container and display the container information in the form of a json string

  • docker exec -it 容器ID bashShellEnter a running container and interact with the command line
    insert image description here

  • docker attach 容器IDThe difference between re-entering the container
    docker execanddocker attach
    attach is to directly enter the terminal of the container startup command, and will not start a new process
    exec is to open a new terminal in the container, and can start a new process

    exec can be understood as entering the container and executing the command, and returning the result, which is more powerful than attach, for exampleinsert image description here

  • docker cp 容器id:容器内路径 目的主机路径Copy files from the container to the host docker container id:/container path host path
    insert image description here

Guess you like

Origin blog.csdn.net/Hong_pro/article/details/123205292