Docker image (image) & container (container)

What is a Docker image (image)?

  • The mirror image is composed of multiple layers. After each layer is superimposed, it looks like an independent object from the outside;
  • Inside the image is a streamlined operating system (OS), which also contains the files and dependencies necessary for the application to run;
  • The image can run one or more containers, and the image can also stop the operation of a container and create a new image from it;

[Classification of mirror images (iamge)]


[Mirror (image) common commands]

  • docker image pull = "download image (download from the remote public image warehouse service Docker Hub by default);
  • docker image pull alpine:latest = "Pull [label/version] from the apline warehouse of Docker Hub as the latest image;
  • docker image pull nginx:latest = "Pull [label/version] from the nginx warehouse of Docker Hub as the latest image;
  • docker image pull -a nginx/xxx = "Pull all the images in the warehouse, the parameter -a is all all;
  • docker image ls -a or docker images -a = "List the images stored on the local Docker host (not shown), the parameter -a is all all;
  • docker image history nginx = "View nginx mirror installation information;
  • docker image inspect ubuntu:latest = "View ubuntu image layer information, display all the details of the image (including image layer data and metadata);
  • docker image rm ubuntu:latest = "Delete the ubuntu:latest image;
  • docker image build = "build a brand new image;

[View image management (command) command configuration parameters]

  • docker image [specific command name] --help = "display image corresponding to the configuration parameters of the management command

【Parameter Filter--filter】

  • docker image ls --filter dangling=true=》Return to dangling ( dangling / image without a label is called dangling mirror , shown in the list as <none>:<none>) mirror;

  • docker image prune =》 Remove all suspended virtual images. If the -a parameter is added, Docker will additionally remove unused images (those images that are not used by any container);

Docker currently supports the following filters:
    dangling: You can specify true or false, and only return dangling virtual images (true) or non-dangling virtual images (false).
    before: Requires the image name or ID as a parameter, and returns all the images created before.
    since: Similar to before, but returns all the mirrors created after the specified mirror.
    label: Filter the image based on the name or value of the label.

    docker image ls --filter=reference="*:latest" =》 The reference completes the filtering and only displays the example
    whose label is latest; docker image ls --format "{ {.Size}}" =》 Parameter --format through Go The template formats the output content and only returns the size attribute of the
    image on the Docker host; docker image ls --format "{ {.Repository}}: { {.Tag}}: { {.Size}}" = "Return all Mirror, but only display warehouse, label and size information;

[CLI search Docker Hub] = "Return to official [official] or unofficial [automated] mirror

  • docker search nginx = "simple mode, search all warehouses that contain a specific string in the "NAME=nginx" field;
  • docker search nginx --filters "is-official=true" = "returns the content of nginx and only displays the official image;
  • docker search nginx --filters "is-automated=true" = "returns the content of nginx and only displays unofficial images;

Note: [docker search] By default, Docker only returns 25 rows of results. You can increase the number of rows returned by the parameter --limit, up to 100 rows;

For example: docker search nginx --limit 60

[Docker file operation]

  • vi/vim index.html = "Edit index.html file information, vim may need to be installed, Linux defaults to vi;
  • docker cp index.html 41c15dlacff0://usr/share/nginx/html =》 Copy the index.html file to the docker container, followed by the container ID: storage path;
  • docker rm nginx =》 Delete the image file, which can be followed by the image name or image ID;
  • docker rm -f xxx =》 The parameter -f is forcibly deleted without prompting;
  • docker rm -rf xxx =》 Delete all files in the folder and its subfolders, the parameter -r deletes recursively downwards;
  • docker mv = "move or rename;
  • docker mv Dockerfile ../chait/ =》 Move the Dockerfile to ../chait/;
  • docker mv Dockerfile docfile = "Rename to docfile;
  • docker commit -m'xxx' 41c15dlacff0 newimagename = "Submit the file, the parameter -m file name, followed by the original image ID, the new image name;

[Linux file operation]

  • ls =》 View all files under this path;
  • mkdir xxx =》 Add new folder;
  • vi / vim xxx = "Edit the file;
  • rm -f xxx =》 Delete files, parameter -f is forced to delete;
  • rm -rf xxx =" delete the directory and all subdirectories and files, the parameter -r recursively downwards, -f force deletes;
  • cat xxx =》 View the content of the file;
  • :wq =》 Exit and save the edited content;
  • sudo apt install lrzsz = "install lrzsz file upload tool;
  • rz = "File upload, based on lrzsz;

【View port number】

  • su root = "Switch root user;
  • lsof -i:port = "output port information;
  • netstat -aptn = "View all open port numbers;
  • netstat -nupl = "View all port numbers using udp protocol;
  • netstat -ntpl= "View all port numbers using tcp protocol;

[Linux restart command]
    1. reboot 
    2. shutdown -r now restart immediately
    3. shutdown -r 10 restart automatically after 10 minutes
    4. shutdown -r 20:35 restart at 20:35

    If the restart is set through the shutdown command, you can use the shutdown -c command to cancel the restart;

[Linux shutdown command]
    1. halt shuts down immediately (usually add -p to shut down the power)
    2. poweroff shuts down immediately 
    3. shutdown -h now shuts down immediately
    4. shutdown -h 10 automatically shuts down after 10 minutes 

    If the shutdown is set by the shutdown command, you can use the shutdown -c command to cancel the shutdown;

[Quick cleanup (violence)]

  • rm $(docker image ls -a) -f = "Delete all the image images on the docker host, the parameter -f is forced to delete
  • docker container rm $(docker container ls -aq) =》Delete all containers on the docker host (delete container Id)

Elegant way: stop and then rm=》pause first, delete;

[Restart doker service]

  • systemctl start docker = "Start docker service;
  • sudo systemctl daemon-reload = "Restart the daemon daemon;
  • systemctl restart docker = "Restart the docker service;
  • sudo service docker restart = "Restart the docker service;
  • service docker stop = "Close the docker service;
  • systemctl stop docker = "Shut down the docker service;

[Mirror command set]

  • docker image --help =》View all management commands of image


What is a Docker container (container)?

  • A container is a running instance of an image, and one image can create multiple containers;
  • The difference between a container and a virtual machine: The container starts faster and is more lightweight. Compared with a virtual machine running on a complete operating system, the container shares the operating system/kernel of the host where it is located.

[The difference between mirroring & container]

  • The state is different, the image is a build-time structure, and the container is a run-time structure;
  • The image is read-only and cannot be written, and the container can be read and written;

[Relationship between Mirror & Container]

  • One image can build multiple containers, 1:n one-to-many relationship;
  • The original intention of the container is to be fast and small, so the image is usually relatively small. The commands [docker container run] and [docker service create]=" start one or more containers from a certain image. Once the container is started from the mirror, the relationship between the two becomes mutually dependent, and the mirror cannot be deleted until all the containers started on the mirror are stopped. Attempting to delete an image without stopping or destroying the container that uses it will cause an error;

The mirror image is usually small

The purpose of the container is to run an application or service, which means that the image of the container must contain the operating system and application files necessary for the application/service to run.

However, the container pursues speed and compactness, which means that when building an image, it is usually necessary to cut out unnecessary parts and keep the volume small.

For example, a Docker image usually does not contain 6 different shells for readers to choose—usually there is only a streamlined shell in a Docker image, or even no shell.

 

The image does not contain the kernel yet-the containers are the kernel of the shared Docker host. So it is sometimes said that the container only contains the necessary operating system (usually only operating system files and file system objects). Tip: The Hyper-V container runs on a dedicated lightweight VM while using the operating system kernel inside the VM.

 

The official Docker image, Alpine Linux, is only about 4MB, which can be said to be a typical example of the compactness of the Docker image.

However, the more common state of the image is like the official Ubuntu Docker image, which is about 110MB. Most of the useless content has been cut off in these mirror images.

The Windows image is larger than the Linux image, which is related to the working principle of the Windows OS. For example, the uncompressed latest Microsoft .NET image (microsoft/dotnet:latest) exceeds 1.7GB.

After pulling and decompressing the Windows Server 2016 Nano Server image (microsoft/nanoserver:latest), its volume is slightly larger than 1GB.

 

[Container (container) commonly used commands]

  • docker container run <image> <app> = "Specifies the image required for startup and the application to be run;
  • docker container run -it ubuntu /bin/bash = "Start the ubuntu container and run Bash Shell as its application;
  • docker container run -it microsoft- /powershell:nanoserver pwsh.exe = "Start the nanoserver container and run PowerShell as its application;
  • The docker container run -it ubuntu /bin/bash =" command will start an Ubuntu container in the foreground and run the Bash Shell;

[Ctrl+PQ] will break the link between the Shell and the container terminal, and keep the container running (UP) in the background after exiting.

The parameter -it can connect the current terminal to the shell terminal of the container. The container terminates with the exit of the application running in it. The Linux container will terminate after the Bash Shell exits, and the Windows container will exit after the PowerShell process terminates. The verification method is to start a new container, and run the sleep command to sleep for 10s, the container will start, then run the sleep command, and exit after 10s=》docker container run alpine:latest sleep 10

  • docker container ls = "List all the containers in the running (UP) state. If you use the -a flag, you can also see the container in the Exited state.
  • The docker container exec -it <container-name or container-id> bash =" command will start a Bash Shell process inside the container and connect to the Shell (the image used to create the container must include the Bash Shell. The host shell is very useful when connecting to a running container terminal);
  • docker container stop <container-name or container-id> = "Stop the running container, and set the status to Exited (10s stop);
  • docker container start <container-name or container-id> = "Restart the container in the Exited state;
  • docker container stop <container-name or container-id> = "Delete the stopped container;
  • docker container inspect <container-name or container-id> = "Display the hierarchical configuration details and runtime information of the container; 

[Container command set]

  • docker container --help =》View all management commands of the container

[Check Docker daemon (daemon)]

If an error code is included in the server part, it means that the Docker daemon is probably not running, or the current user does not have permission to access;

[Solve the problem of unauthorized access in Linux]

  1. Confirm whether the current user belongs to the local Docker UNIX group, if not, add it through [usermod -aG docker <user>], log out and log in to Shell again, and the modification will take effect;
  2. The user belongs to the docker user group, which may be caused by the Docker daemon not running. According to the operating system of the Docker host, select the following corresponding command to check the status of the daemon;
//使用 Systemd 在 Linux 系统中执行该命令
$ service docker status
docker start/running, process 29393

//使用 Systemd 在Linux 系统中执行该命令
$ systemctl is-active docker
active

//在Windows Server 2016的 PowerShell 窗口中运行该命令
> Get-Service docker

Status Name DisplayName
------ ---- -----------
Running Docker docker

[Docker instruction book]

  • docker --help = "View all management commands in docker;
  • docker --info = "View docker information;

  •  Management Commands Management Commands:

Guess you like

Origin blog.csdn.net/ChaITSimpleLove/article/details/105211146