preface

In the last article, I had a general understanding of Docker, mainly from the concept, architecture, advantages and process of exposition, installation and experience; Next, we will start to carry out practical operation learning. In the demonstration process, we will summarize and summarize the key knowledge points. Here we start with common commands.

The body of the

1. Preview

Docker and Git we are familiar with are very similar, are through the command to execute related operations, of course, there are some interface management tools (button), but people seem to prefer to directly type the command, after all, this way is more flexible, easier to understand the essence of the operation; Some of the most common commands are shown in a picture. Here’s a look:

If you are not familiar with Docker, you may feel confused when you see this image. Don’t worry, then read it down, and then come back to this image. It definitely feels great.

2. Perform common commands

2.1 Global Commands
  • Docker version: Displays the docker version information.

  • Docker info: View docker details, such as storage information, accelerator configuration information, how many images, how many containers, CPU, memory, etc.

  • Docker events: Obtain real-time events from docker service, which is generally understood as operation logs. For example, operations on mirrors, containers, networks and volumes will record corresponding event information. A maximum of 1000 recent logs can be returned.

    Run a command on a terminal that blocks:

    Start another terminal and execute the command to run the container from the image:

    At this time, the first terminal will output event messages in real time, such as image pull, container start, container end and other information, as follows:

    Since this command can be understood as an operation log, you can certainly view the corresponding data according to the conditions, as follows:

    As shown in the figure, the event information that meets the conditions is displayed first, then the block continues, and if there is an operation on the server, the information is displayed in real time. Common parameters are specified as follows:

    -f: filters events according to conditions. In the preceding figure, events related to the hello-world mirror are specified.

    –since: Displays all events after the specified timestamp, which can be interpreted as the start time. Multiple time formats are supported, and the localhost’s time zone is used by default.

    –until: displays until the specified time, which can be interpreted as the end time.

2.2 Common Mirror Commands

Image can be understood as a lightweight and independently run software package, which contains the application program and other infrastructure required by the operation, such as runtime, configuration files, and dependent libraries. Therefore, the container cannot be started without an image. For example, if there is no class in development, how can we create an instance from that class?

The mirror is read-only, so there are few operation commands, generally adding, deleting, and searching.

  • Docker images: Lists the images on the Docker host

    You can specify parameters. Commonly used parameters are as follows:

    -a: lists all local mirrors (including the middle mirror layer, which is filtered out by default).

    -q: Displays only the mirror ID.

    In the figure above, -aq displays the ids of all mirrors, which are usually used for batch deletion.

  • Docker search: Searches for images from the remote repository

    You can specify criteria for searching, as follows:

    –filter: specifies the search criteria, IS -official or not, and stars specifies the number of stars to search for.

    For search, I prefer to use the interface, intuitive and good-looking:

  • Docker pull: pull the image from the remote repository, followed by the image name and tag, that is, specify the version of the image to pull, if the tag is not specified, the default is latest, latest.

    The layered principle of mirroring is to use UnionFS(Joint file System), which is a layered, lightweight, high-performance file system. Images can be inherited through layers, and specific application images can be created based on the base image, such as the Nginx image we just pulled down, which is understood here first, and will be understood later when we make our own images.

    Docker pull image name :tag: specifies the version to pull;

  • Docker RMI: Delete the specified image, which can be followed by the name or the image ID

    Delete the specified version as follows:

    Delete multiple images according to their ID, separated by Spaces:

    Delete all mirrors, is to find all mirror ids, and then delete the line, of course, not a copy of the mirror ID; **docker images -aq** can display all image ids, so the two commands can be combined as follows:

     docker rmi -f $(docker images -aq)
    Copy the code

    -f: forcibly deletes an image. For example, if some images and containers depend on each other, a message is displayed indicating that the image cannot be deleted directly.

  • Docker Save: Export images. You can copy images offline to other hosts to avoid the scenario where images cannot be downloaded without a network.

    The generated tar file can be copied to the corresponding device as required and loaded for use without online pull, because the Internet is not allowed in many scenarios.

  • Docker load: load the image, according to the copy of the tar file can be directly loaded to the host.

    In this example, delete the image and reload it using the load command as follows:

    Load the image as follows:

    Another way to write it is as follows:

    Options:

    –input, -i: specifies the file to be imported.

    –quiet, -q: Simplifies the output and does not display the specific loading process.

    Note: this command is executed in the TestDockerImage directory, so the tar file is specified in the current directory.

2.3 Common Container Commands

Containers are running instances created with images that can be started, started, stopped, and deleted, each isolated from the other. Think of the container as a combination of a minimalist Linux environment and programs running in it;

Containers and images are almost the same, the only difference being that the image layer is loaded with a writable layer called the container layer.

In the following operations, you can specify either the container name or the container ID. The container ID is used in the demonstration and the screenshots will not be repeated.

  • Docker run: Starts the container based on the image; Docker run [OPTIONS] IMAGE [COMMAND] [ARG…] ;

    Common options are described as follows:

    –name=” container name “: specify a name for the container;

    -d: Runs the container in the background and returns the container ID.

    -I: Runs the container in interactive mode. It is usually used together with -t.

    -t: reallocates a pseudo-input terminal to the container. It is usually used together with -i.

    -p: indicates random port mapping. Ports inside a container are randomly mapped to ports on a host

    -p: specifies the port mapping. The format is: Host port: container port

    The demo is as follows:

    As you can see in the figure above, the terminal is blocked. This mode is called the attached default, which means foreground running, and corresponds to the detached mode, and the background mode, which is shown in the next section. Due to terminal block, here is another terminal to run docker ps command to see the container running, as follows:

    The foreground run-time terminal can easily be shut down, and the started Nginx container will also stop. This is not allowed in many scenarios, so you can specify the run-time as background mode, i.e. detached mode, as follows:

    If you want to access the nginx from the host, you need to perform port mapping as follows:

    Two nginx containers are enabled to listen on port 80, but there are no reports that the port is occupied, so the containers do not affect each other. Port mapping with **-p** allows access to port 80 inside the container through port 9999 on the host, as follows:

    Since we started with the container as a simple Linux version, we should be able to go inside the container and manipulate it as follows:

    Only a few core commands can be executed inside the container. Because it is a minimal version, only important functions are contained inside the container. You can install extensions if you need other functions.

    There are two ways to exit a container:

    A. Run the exit command in the container to stop and exit the container and return to the host.

    B. Use CTRL + P + Q to exit the container without stopping and return to the host;

  • Docker ps [OPTIONS] : displays containers in the host.

    -a: displays all containers, including those that are not running.

    -n: lists the n containers created recently.

    The demo is as follows:

  • Commands to start and stop containers;

    Docker start docker stop docker stop docker restart docker kill docker stop docker stopCopy the code

    **docker stop** Stops the container, which can be followed by one or more container ids:

    Docker start: starts the stopped container, which can be followed by one or more container ids:

    **docker restart command and docker kill** forced stop command do not screenshot

  • ** Docker exec and Docker Attach ** Two ways to enter a running container.

    Many scenario containers run in the background, but sometimes you need to go inside the container to make configuration changes.

    Docker exec: After entering the container, start a new terminal and normally execute Linux related commands.

    Docker attach: Enters the terminal where the container is executing and does not start a new process.

    Exit container mode:

    Run the exit command in the container, and the container stops and exits.

    Use the combination key CTRL + P + Q, the container does not stop exit;

  • Docker logs [OPTIONS] docker logs [OPTIONS]

    Common OPTIONS are as follows:

    -f: tracks the output of logs

    –since: Displays all logs after the specified start time

    -t: displays the timestamp

    –tail: Lists the latest N container logs

    You can specify options to view desired logs as follows:

  • Docker top Container ID: lists the processes in the specified container. You can check whether the application processes in the container are running properly as follows:

  • Docker inspect container ID: inspect the details of the specified container, such as running status, network configuration, volume mounted, etc., as follows:

  • Docker commit: Generates a new image based on the container; Containers are editable, and sometimes you need to make a new image of a changed container for someone else to use.

    Command description:

    -a: submits the mirror author.

    -m: Description of submission;

    In the testCommitimage :v1 command, the image name and tag are user-defined.

    Start the container based on the newly generated image, and there will be a corresponding file created inside the container (the inside of the container can be changed as needed, but this is just a demonstration of creating the file).

  • ** Docker export and docker import** To facilitate offline export and import containers;

    Command description:

    Docker export -o testexport.tar 030AA6fcd7f3 # -o specify the output location and file name # 030AA6fcd7f3 Import testexport. Tar testexportimagename:v2 # Specifies the corresponding tar file # testexportimagename:v2 Image name and version, which can be customizedCopy the code

    The usage of these commands is similar to that of docker save and docker load, but the two methods cannot be mixed, because export only exports the snapshot of the container, while save saves the complete image file.

  • Docker rm container ID: delete the specified container. By default, the running container is not allowed to be deleted.

More than just summarized the comparison of commonly used commands at ordinary times, and not all are listed, more detail can enter the website: docs.docker.com/engine/refe… ;

conclusion

The above content mainly demonstrates and explains common commands for images and containers. Commands related to data volumes, dockerfile, and network will be shared separately later.

Seeing this, I believe that the picture at the beginning of the article has been almost understood by friends, and the module division in the picture, arrow pointing is very meaningful; There are a lot of dry goods in the future, pay attention to “Code variety circle”, with me to learn;