In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to view Docker container logs". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Verify that Docker starts properly
Use the docker info command to return the number of containers and images, the docker version, the execution and storage drivers used, and the basic configuration of Docker, etc. As mentioned earlier, Docker is based on client-server architecture. It has a docker program that can do both client and server. As a client, the docker program sends a request to the Docker daemon and then processes the returned result of the request.
Run the first container
Containers can be created using the docker run command. It provides the creation to startup function of the container. Running
Docker run-I-t ubuntu / bin/bash
Parameter-I ensures that STDIN is enabled in the container
The parameter-t tells Docker to assign a pseudo tty terminal to the container to be created. So that the newly created container can provide an interactive shell
Next, tell Docker what image to use to create the container. In the above example, the ubuntu image is used, which is a basic image provided by Docker and saved on Docker Hub Registry.
Finally, tell Docker what command to run in the new container, and in this case run the / bin/bash command in the container to start a Bash shell.
After running the command, Docker will check whether a ubuntu image exists locally. If not, it will connect to Docker Hub Registry to see if it exists. Once it is found, it will download it and save it to the local host.
Docker then uses this image to create a new container inside the file system. It has its own IP address and a bridging network interface to communicate with the host.
When the container is created, Docker executes the / bin/bash command in the container, and you can see the shell in the container.
Use the first container
By launching, we logged in to the new container Red as the root user. This is a complete ubuntu system.
Type exit to return from the container to the host command line. At the same time, the container stops running. But the container still exists. You can use the docker ps-a command to view a list of containers on the current system.
The docker ps command displays only the containers that are running, adding the-a parameter to list all containers. The-l parameter lists the containers that were last run, including those that are running and those that have stopped.
You can see that there are three ways to uniquely refer to a container: long UUID, short UUID, and name.
Container naming
Docker automatically generated a random name when the container was created earlier. If you want to specify a name when creating a container, you can use the-- name parameter.
Docker run-- name ivan_container-I-t ubuntu / bin/bash
A valid container name can only contain the following characters: lowercase letters, uppercase letters, numbers, underscores, dots, and dashes.
The naming of the container is unique
The docker rm container name can delete the container.
Restart the stopped container
Docker start container name docker start container ID
Attach to the container
When the Docker container is restarted, it runs with the parameters specified in the docker run command, so the container runs an interactive session shell when it is restarted.
You can reattach to the session of the container using the "docker attach container name / container ID" command. After running the command, you need to press enter to enter the session.
Create a guardian container
In addition to the interactive containers created above, you can also create long-running guarded containers. It has no interactive sessions and is ideal for running applications and services. Most of the time you need to run the container in guardian mode.
Docker run-name daemon_dave-d ubuntu / bin/sh-c "while true; do echo hello world; sleep 1 done"
Using the parameter-d on docker run, the container is put to run in the background.
View container log
Docker logs Container name
-f parameter monitoring container real-time log
Docker logs-f container name
Use Ctrl + C to exit log monitoring.
-- number of tail lines to get the last few lines of log
Docker logs-- tail 10 container name
Use
Docker logs-- tail 0-f container name
Monitor the latest log of a container without having to read the entire log file.
Use the-t parameter to timestamp each log entry.
Docker logs-ft container name
View the processes in the container
Docker top Container name
Run the process inside the container
After Docker 1.3, additional new processes can be started in the container through the docker exec command. There are two types of processes that can be run within a container: background tasks and interactive tasks.
Examples of background tasks:
Docker exec-d container name touch / etc/new_config_file
-d indicates that a background process needs to be run. This is followed by specifying the container to run and the command to execute. In this example, a new empty file is created.
Examples of interactive tasks:
Docker exec-t-I container name / bin/bash
Stop guarded container
Docker stop Container name
The docker stop command sends a SIGTERM signal to the Docker container process. If you want to stop a container quickly, you can use the docker kill command to send a SIGKILL signal to the container process.
The docker ps-n x command displays the last x containers, whether the container is running or has been stopped.
Automatically restart the container
When you create a container, you can have Docker restart the container automatically with the-- restart parameter. The restart flag checks the exit code of the container and decides whether to restart the container accordingly.
Docker run-restrart=always-name daemon_dave-d ubuntu / bin/sh-c "while true; do echo hello world; sleep 1; done"
The restart flag is set to always, and Docker automatically restarts the container regardless of its exit code. In addition to always, you can also set it to on-failure so that it will restart automatically only if the container's exit code is non-zero worth it. In addition, on-failure also receives an optional reboot parameter:
-- restart=on-failure:5
In this way, when the container exit code is not 0, Docker will try to restart the container automatically, and finally restart it 5 times.
-- restart was introduced in version 1.2.0.
Detailed container information
Docker inspect Container name
View the details of the container. Use-f-- format to select what you want to view:
Docker inspect-- format=' {{.State.Running}} 'container name
View the running status of the container.
Docker inspect-- format'{{.NetworkSettings.IPAddress}} 'Container 1 / Container 2
By looking at the IP address of the container, you can make multiple containers at the same time, as shown in the above example.
-- format-f supports complete Go language templates.
You can also browse the / var/lib/docker directory to learn more about how Docker works. This directory holds the Docker image, container, and container configuration. All containers are stored in the / var/lib/docker/containers directory.
Delete Container
Use docker rm to delete the container.
A running docker container cannot be deleted. The container must be stopped through the docker stop or docker kill command before it can be deleted.
Currently, there is no way to delete all containers at once, but you can delete them with a few tricks:
Docker rm 'docker ps-a-Q'
-a means to list all containers, and-Q means that only the ID of the container needs to be returned and no other information is returned.
That's all for "how to View Docker Container Log". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.