Docker commands and composition how-to guide

basic command

Docker is a popular containerization platform that allows developers to build, package, publish and run applications in an isolated environment. Here are some basic Docker commands:

  1. docker version: Check Docker version information.
  2. docker info: Display Docker system information, such as the number of running containers and images.
  3. docker search <image>: Search for images on Docker Hub.
  4. docker pull <image>: Download the image from Docker Hub.
  5. docker images: List the locally downloaded images.
  6. docker run <image>: Run an image in a container, eg docker run hello-world.
  7. docker ps: List running containers.
  8. docker stop <container>: Stop a running container, where <container>can be the ID or name of the container.
  9. docker rm <container>: Delete a stopped container.
  10. docker rmi <image>: Delete a local mirror.
  11. docker build -t <tag> <directory>: Build a mirror, -tthe option is used to specify the label (tag), <directory>which is the directory containing the Dockerfile.
  12. docker run -p <host-port>:<container-port> <image>: Run the container and map the port of the container to the port of the host.
  13. docker exec -it <container> <command>: Execute the command in the running container, -itthe option is used to enter the interactive mode.
  14. docker logs <container>: View the log output of the container.
  15. docker cp <container>:<path> <host-path>: Copy the files inside the container to the host.
  16. docker network ls: List Docker networks.
  17. docker network create <network>: Create a Docker network.
  18. docker volume ls: List Docker data volumes.
  19. docker volume create <volume>: Create a Docker data volume.

Combination command

Docker provides some combination commands, which can be combined to complete more complex operations by combining multiple single commands. Here are some common Docker combination commands and what they do:

  1. Create and run the container:
docker run -d --name mycontainer <image> <command>

This command will create a new container and run the specified command in the container. -dThe option indicates that the container runs in background mode, and --namethe option specifies the name of the container.
2. Build the image and run the container:

docker build -t myimage <directory> && docker run -d --name mycontainer myimage

This command will first build an image and then run a container on the newly built image. &&operator is used to concatenate two commands to ensure that the container is only run after the image build is successful.
3. Run the container and enter interactive mode:

docker run -it <image> <command>

This command will run the specified command in interactive mode in the container and connect the terminal to the standard input and output of the container.
4. Run the container and mount the local directory:

docker run -v <host-path>:<container-path> <image>

This command will run the container and mount the specified directory on the host to the specified path of the container to realize file sharing between the host and the container.
5. Stop and delete all containers:

docker stop $(docker ps -aq) && docker rm $(docker ps -aq)

This command stops and deletes all running containers. docker ps -aqUsed to list the IDs of all containers and pass them as arguments to docker stopthe and docker rmcommands.
6. Delete all mirrors:

docker rmi $(docker images -q)

This command will delete all local mirrors. docker images -qThe IDs used to list all mirrors are passed as arguments to docker rmithe command.
7. Build the image and push it to Docker Hub:

docker build -t <image> <directory> && docker push <image>

This command builds an image, then pushes that image to Docker Hub, making it accessible and usable by others.
8. Stop and delete all containers, and delete all images:

docker stop $(docker ps -aq) && docker rm $(docker ps -aq) && docker rmi $(docker images -q)

This command stops and deletes all running containers, and then deletes all local images.
9. Run the container and share the network namespace with the host:

docker run -it --net=host <image> <command>

This command will run the specified command in the container, and share the network namespace with the host, so that the container can directly access the network interface on the host.
10. Run the container and set the environment variables:

docker run -e <key>=<value> <image>

This command will run in the container and set one or more environment variables for the application to use inside the container.
11. Execute the command inside the container and copy the result to the host:

docker exec <container> <command> > <host-path>

This command will execute the specified command inside the running container and copy the output of the command to the specified path on the host.
12. If you want to delete an image that has no image name (that is, no tag), you can use the following command:

docker image prune -a

This command will delete all untagged images, including untagged images without associated containers. This will free up disk space and clean up images that are no longer needed. Note that before executing this command, make sure you no longer need these untagged images.
13. To delete dangling images (i.e. images without associated containers), use the following command:

docker image prune

This command deletes all unused images to free up disk space. Note that this will delete all images without associated containers, including old images that are no longer used. Before executing this command, make sure you don't need these images, lest you accidentally delete the ones you need.
In Docker, there are some commonly used combined commands that can help you manage containers, images, and other resources more efficiently. Here are some common Docker composition commands:

  1. Cleanup command:

    • Stop and delete all containers, and delete all images:
      docker stop $(docker ps -aq) && docker rm $(docker ps -aq) && docker rmi $(docker images -q)
      
    • Delete all dangling containers (without associated images):
      docker container prune
      
  2. Build and run commands:

    • Build the image and run the container:
      docker build -t <image> <directory> && docker run -d --name <container> <image>
      
    • Build the image and run the container, and map the port:
      docker build -t <image> <directory> && docker run -d --name <container> -p <host-port>:<container-port> <image>
      
  3. Data volume command:

    • Create a container and mount the data volume:
      docker run -v <host-path>:<container-path> <image>
      
    • Run the container and mount the anonymous data volume:
      docker run -v <container-path> <image>
      
    • Create and manage data volumes:
      docker volume create <volume>  # 创建数据卷
      docker volume ls               # 列出数据卷
      docker volume inspect <volume> # 查看数据卷详细信息
      docker volume rm <volume>      # 删除数据卷
      
  4. Network command:

    • Create a custom network:
      docker network create <network>
      
    • Run the container and connect to the specified network:
      docker run --network=<network> <image>
      
    • List networks:
      docker network ls
      

docker inspectCommands can be combined with other commands to get richer container information or perform more complex operations. Here are some common docker inspectcommands used in combination with the command:

  1. docker inspect + jq

    • jqis a command-line JSON processing tool that can be used to filter and extract JSON data.
    • Example:
      docker inspect <container> | jq .[0].NetworkSettings.IPAddress
      
    • This example uses jqthe filter to extract the container's IP address.
  2. docker inspect + grep

    • grepis a text search tool for matching specific patterns in output.
    • Example:
      docker inspect <container> | grep IPAddress
      
    • This example uses grepthe filter to find the container's IP address.
  3. docker inspect + awk

    • awkis a text processing tool that can be used to segment the output and extract specific fields.
    • Example:
      docker inspect <container> | awk -F'"' '/IPAddress/ {print $4}'
      
    • This example uses to awkextract the container's IP address.
  4. docker inspect+ shell variables:

    • Assign docker inspectthe output of the to a shell variable for further processing and use.
    • Example:
      CONTAINER_INFO=$(docker inspect <container>)
      echo $CONTAINER_INFO
      

container running

When running a container, some basic configuration options are available to customize the container's behavior and environment. Here are some common basic configuration options and their descriptions:

  1. Container name:

    • --name <name>: Specifies the name of the container, used to identify and operate the container. For example:--name mycontainer
  2. Background process:

    • -dOr --detach: run the container in background mode so that it runs in the background without blocking the terminal. For example:-d
  3. Port Mapping:

    • -p <host-port>:<container-port>: Map the port of the container to the port of the host, so that the outside can access the service in the container. For example:-p 8080:80
    • -POr --publish-all: automatically map all ports in the container to random ports on the host.
  4. Environment variables:

    • -e <key>=<value>: Set the environment variable in the container. For example:-e ENV_VAR=example
  5. Data volume mount:

    • -v <host-path>:<container-path>: Mount the directory or file of the host to the specified path of the container to realize file sharing between the host and the container. For example:-v /path/on/host:/path/on/container
  6. Work list:

    • -w <path>: Set the working directory of the container. For example:-w /app
  7. Users and permissions:

    • -u <user>[:<group>]: Specifies the user or user group inside the container. For example:-u 1000:1000
  8. Internet connection:

    • --network <network>: Connect the container to the specified Docker network. For example:--network mynetwork
  9. Interactive mode:

    • -iOr --interactive: keep standard input (stdin) open, allowing interaction with the container.
    • -tOr --tty: Allocate a pseudo terminal (TTY).

These are some common container run configuration options. Please note that , <name>, <host-port>, <container-port>, <key>, <value>, , , , and are parameters that need to be replaced with specific values ​​according to the actual situation <host-path>.<container-path><path><user><group><network>

Troubleshooting Docker

There are some common problems you may encounter while running Docker. Here are some common troubleshooting methods and commands:

  1. View container status:

    • Use docker psthe command to view running containers.
    • If the container is not running properly, you can use docker ps -athe command to view the status of all containers, including stopped ones.
  2. View container logs:

    • Use docker logs <container>the command to view the container's log output. <container>Can be the ID or name of the container.
    • Add -foption to track the container's log output in real time.
  3. Enter the container for debugging:

    • Use docker exec -it <container> <command>the command to enter a running container and execute specific commands inside the container. This allows access to the container for debugging and troubleshooting.
    • For example: docker exec -it <container> bashor docker exec -it <container> shto enter the container's interactive terminal.
  4. Check the container configuration:

    • Use docker inspect <container>the command to view the detailed configuration information of the container, including network settings, environment variables, mounted volumes, etc.
  5. Check container resource usage:

    • Use docker statsthe command to view the resource usage of a running container, including CPU, memory, network, and disk usage.
  6. Restart the container or Docker service:

    • If there is a problem with the container, try restarting the container or the Docker service, which may solve some common problems.
    • Restart the container with docker restart <container>the command.
    • Restart the Docker service using sudo systemctl restart dockerthe command (Linux) or restart Docker Desktop (Windows or macOS).
  7. View the status of the Docker service:

    • sudo systemctl status dockerCheck that the Docker service is running properly by using the command (Linux) or by viewing the status of Docker Desktop (Windows or macOS).
  8. Clean up useless resources:

    • Use docker system prunethe command to clean up useless containers, images, data volumes, etc. to free up disk space.

Guess you like

Origin blog.csdn.net/pengjun_ge/article/details/131590127