[Container] basic use of docker

1. Common commands of docker

docker is a containerization platform.
Docker introduction: (official website: https://www.docker.com/get-started)

Docker is an open source application container engine that you can use as a lightweight virtual machine. It allows developers to package their applications and dependencies into a lightweight, portable container, and then publish to any popular operating system, such as Linux/Windows/Mac machines. Docker containers do not have any interfaces with each other, and the overhead of the container itself is extremely low, which makes Docker a very flexible, secure, and highly scalable computing resource platform.

Here are some common Docker commands:

  1. Mirror related commands:
  • docker images: List local mirrors.
  • docker pull <image>: Download the image from the Docker image repository.
  • docker push <image>: Push the local image to the mirror repository.
  • docker rmi <image>: Delete the local image.
  1. Container related commands:
  • docker run <image>: Create and start a container based on the image.
  1. -d
    runs the container in the background and returns the container ID. At this time, it will not enter the interactive interface. If you want to enter the interactive interface, please add -i and -t parameters.
    If the -d parameter is used and the container is not entered, and you want to enter the container, the command: docker exec -it container name /bin/bash;

  2. -i
    -i: run the container in interactive mode, usually used together with -t;

  3. -t
    -t: Reassign a pseudo-input terminal for the container, usually used together with -i;

  4. -P
    -P: Random port mapping, the internal port of the container is randomly mapped to the port of the host

  5. -p
    -p: Specify port mapping, the format is: host (host) port: container port

  6. –name
    –name="xxxxx": Specify a name for the container, the name is xxxxx;

  7. –dns
    –dns 8.8.8.8: Specify the DNS server used by the container, which is the same as the host by default;

  8. -e
    -e username="ritchie": set environment variables;

  9. -cpuset
    –cpuset="0-2" or --cpuset="0,1,2": Bind the container to the specified CPU to run;

  10. -m
    -m : Set the maximum memory used by the container;

  11. -net
    –net="bridge": Specify the network connection type of the container, support bridge/host/none/container: four types;

  12. -link
    –link=[]: add a link to another container;

  13. -expose
    –expose=[]: open a port or a group of ports;

  14. -v
    --volume , -v: bind a volume

Note: -vIn the parameters, the directory before the colon ":" is the host directory, and the directory after it is the directory in the container.
The container directory cannot be a relative path.
The host directory will be automatically generated if it does not exist.

  • docker start <container>: Start a stopped container.
  • docker stop <container>: Stop a running container.
  • docker restart <container>: Restart a container.
  • docker rm <container>: Delete a container.
  • docker ps: List running containers.
  • docker ps -a: List all containers.
  1. Log and output related commands:
  • docker logs <container>: View the logs of the container.
  • docker exec -it <container> <command>: Executes a command in a running container.
sudo docker exec -it your_container_name python a.py 

# 或者直接进入容器后直接python a.py
  • docker attach <container>: Attach to the standard input, output, and error streams of the running container.
  1. Network and port related commands:
  • docker network ls: List Docker networks.
  • docker network create <network>: Create a custom network.
  • docker port <container>: View the port mapping of the container.

Run docker --helpor refer to the official Docker documentation for more detailed command information and usage instructions.

2. Matters needing attention

  • The image can be regarded as a software package (including the code, environment, library files, system tools, etc. required for the program to run)
  • After downloading the image, you can create a container. The container is an instance of the image. The container can be started, stopped, deleted and managed, similar to an isolated process
    • The state of the container can be modified, such as adding files to the container, modifying the configuration, etc.
    • If you need to save the modified container, you can use the image build command provided by docker to convert it to a new image

insert image description here

Reference

[1] Docker official documentation: https://docs.docker.com/engine/reference/run/
[2] How to create, import and export docker images
[3] How to use Docker to deploy containers
[4] Log in to remote servers through pycharm The complete process of docker on
[5] Basic Docker commands. Microsoft documents
[6] Detailed explanation of Docker commands and parameters

Guess you like

Origin blog.csdn.net/qq_35812205/article/details/132124217