Docker container operation

1. Create a container

1. Create a new container

You can use docker [container] createthe command to create a new container.

docker create -it ubuntu:latest
af8f4f922dafee22c8fe6cd2aelld16e2507d61flblfa55b36e94db7ef45178
$ docker ps - a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
af8f4f922daf ubuntu:latest "/bin/bash " 17 seconds ago Created silly_euler

Use docker [container] createthe command of the new vessel is stopped, you can use the docker [container] startcommand to start it.
Insert picture description here

Figure 1. Options related to the Create command and container operating mode

Insert picture description here

Figure 2. Options related to the container environment and configuration of the create command

Insert picture description here
Insert picture description here
Insert picture description here

Create command options related to container resource restrictions and security protection

Other options include:

  • -l, --label =[]: Specify the label information of the container in the form of key-value pairs;
  • --label-file =[]: Read label information from the file.

2. Start the container

Use docker [container] startthe command to start a container has been created. For example, to start the ubuntu container you just created:

$ docker start af
af

In this case, the docker pscommand to view a running to a container:

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
af8f4f922daf ubuntu:latest "/bin/bash" 2 minutes ago Up 7 seconds silly_euler

3. New well start container

In addition to starting the container through the start command after creating the container, you can also directly create and start the container.
Command needed mainly docker [container]run, equivalent to the first execution docker [container] createcommand, and then execute docker [container] startthe command.
When using the docker [container] runtime to create and launch container, standard operating Docker running in the background include:

  • Check whether the specified mirror exists locally, and download it from the public warehouse if it does not exist;
  • Use the image to create a container and start the container;
  • Assign a file system to the container, and mount a read-write layer outside the read-only image layer;
  • Bridge a virtual interface from the bridge interface configured by the host host to the container;
  • Configure an IP address from the address pool of the bridge to the container;
  • Execute the application specified by the user;
  • The container is automatically terminated after execution.

In some cases, execution docker [container] runtime because the command can not perform normal container to be wrong exit, then you can check the error code exit.
By default, common error codes include:

  • 125: Docker daemon execution error, such as specifying unsupported Docker command parameters;
  • 126: The specified command cannot be executed, such as permission error;
  • 127: The command in the container could not be found.

If an error occurs after the command is executed, the exit error code of the command will be returned by default.

4. Running in guard mode

More often, the Docker container needs to be run in the daemonized state in the background. In this case, it can be achieved by adding -dparameters.
For example, the following command will run the container in the background:

Insert picture description here
Returns a unique id after starting the container, it can also docker psor docker container lscommand to view information container:
Insert picture description here
5. See kiln output
to retrieve output information container can be produced by docker [container] logsthe command.
The options supported by this command include:

  • -details: print detailed information;
  • -f, -follow: keep the output continuously;
  • -since string: output logs starting from a certain time;
  • -tail string: output several recent logs;
  • -t, -timestamps: display time stamp information;
  • -until string: Output the log before a certain time.

2. Stop the container

1. Pause the container

You can use docker [container] pause CONTAINER [CONTAINER ... ]the command to suspend operation of a vessel.
State container is paused, you can use the docker [container ] unpause CONTAINER [CONTAINER ... ]command to return to operational status.

2. Terminate the container

You may be used docker [container] stopto terminate the operation of a vessel. The format of the command is docker [container] stop [-t I - -time [=10]] [CONTAINER ... ].
This command will first send a SIGTERM signal to the container, wait for a timeout period (10 seconds by default), and then send a SIGKILL signal to terminate the container:

$ docker stop ce5
ce5

Run the docker container prunecommand will automatically remove any containers in a stopped state.
In addition, it can also docker [container ] killbe sent directly to the vessel forcibly terminated SIGKILL signal.
docker [container] restartThe command will terminate a running container and then restart it.

3. Enter the container

1. attach command

attach is a command that comes with Docker, the command format is:

docker [container] attach [--detach-keys[=[]]] [--no-stdin] [--sig-proxy[=true]] CONTAINER

This command supports three main options:

  • --detach-keys[=[]]: Specify the shortcut key sequence to exit attach mode, the default is CTRL-p, CTRL-q;
  • --no-stdin=true|false: Whether to close the standard input, the default is to keep it open;
  • --sig-proxy=true|false: Whether the agent receives the system signal to the application process, the default is true.

Insert picture description here
However, using the attach command is sometimes inconvenient. When multiple windows are attached to the same container at the same time, all windows will be displayed simultaneously; when a window is blocked by a command, other windows cannot perform operations.

2. exec command

Starting from version 1.3.0 of Docker, Docker provides a more convenient tool exec command, which can execute any command directly in the running container.
The basic format of the command is:

docker [container] exec [-d|--detach] [--detach-keys[=[]] [-i|--interactive] [--piivileged] [-t|--tty] [-u|--user [=USER]] CONTAINER COMMAND [ARG . . . ]

The more important parameters are:

  • -d, --detach: execute commands in the background in the container;
  • –Detach-keys=" ": Specify the key to switch the container back to the background;
  • -e, --env=[]: Specify a list of environment variables;
  • -i, --interactive=true|false: open standard input to accept user input commands, the default value is false;
  • --Privileged=true|false: Whether to give high privileges to execute commands, the default value is false;
  • -t, --tty=true|false: assign pseudo terminal, the default value is false;
  • -u, --user="": The name or ID of the user who executes the command.

4. Delete the container

You can use docker [container] rmthe command to delete the container is terminated or the exit status of command is as follows docker [container] rm [-f|--force] [-l|--link] [-v|--volumes] CONTAINER [CONTAINER ... ].
The main supported options include:

  • -f, --force=false: Whether to forcefully terminate and delete a running container;
  • -l, --link=false: delete the connection of the container, but keep the container;
  • -v, --volumes=false: Delete data volumes mounted by the container.

By default, the docker rm command can only delete containers that have been terminated or exited, and cannot delete containers that are still running.
If you want to directly delete a running container, you can add the -f parameter. Docker will first send a SIGKILL signal to the container, terminate the application in it, and then forcefully delete it.

5. Import and export containers

1. Export container

Exporting a container refers to exporting a created container to a file, regardless of whether the container is running or not. You can use the docker [container] export command, the command format is:

docker [container] export [-o|--output [=""]] CONTAINER

Which can -obe specified to export tar file name option, can be achieved by redirecting directly.

2. Import the kiln

The exported file and can use the docker [container ] importcommand to import into the mirror, the command format is: docker import [-c|--change[=[]] [-m|-- message[=MESSAGE]] file|URL| [REPOSITORY [:TAG]]
user can -c, --change = [] Dockerfile instruction execution options modify the containers at the same time introduced.
In fact, both can use the docker loadcommand to import the file to a local mirror mirrored storage library, you can also use the docker [container] importcommand to import a snapshot of the container to the local image library. The difference between the two is that the container snapshot file will discard all historical records and metadata information (that is, only the snapshot state of the container at that time is saved), while the mirror storage file will save the complete record and is larger . In addition, metadata information such as tags can be re-specified when importing from the container snapshot file.

6. View the container

1. View container details

View details of the container can be used docker container inspect [OPTIONS] CONTAINER [CONTAINER . .. ]subcommand.
For example, to view the specific information of a container, various information including container Id, creation time, path, status, mirroring, configuration, etc. will be returned in json format:
Insert picture description here

2. View the process in the kiln

View inside the container processes can use docker [container] top [OPTIONS] CONTAINER [CONTAINER ... ]sub-command.
This subcommand is similar to the top command in the Linux system, which will print out the process information in the container, including PID, user, time, command, etc. For example, to view the process information in a container, the command is as follows:
Insert picture description here

3. View statistics

View statistics using docker [container] stats [OPTIONS] [CONTAINER ... ]sub-command displays statistics for usage of CPU, memory, storage, networking and so on.
Support options include:

  • -a, -all: output all container statistics, by default only in operation;
  • -format string: format output information;
  • -no-stream: Do not continuously output, the default will automatically update the continuous real-time results;
  • -no-trunc: Do not truncate the output information.

7. Other container commands

1. Copy files

The container cp command supports copying files between the container and the host. The command format is docker [container] cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH |-. Supported options include:

  • -a, -archive: package mode, the copied file will have the original uid/gid information;
  • -L, -follow-link: follow the soft link. When the original path is a soft link, only the link information is copied by default. Using this option will copy the target content of the link.

2. View changes

container diff View changes to the file system in the container. The command format is docker [container] diff CONTAINER.

3. View port mapping

The container port command can view the port mapping of the container. The command format is docker container port CONTAINER [PRIVATE_PORT[/PROTO ]].

4. Update configuration

The container update command can update some runtime configurations of the container, mainly some resource limit shares. The command format is docker [container] update [OPTIONS] CONTAINER [CONTAINER .. . ].

Guess you like

Origin blog.csdn.net/weixin_44826356/article/details/109394433