Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Health testing of docker containers

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Overview

Docker

Docker is an open source application container engine that allows developers to package their applications and dependencies into a portable container, then publish them to any popular Linux or Windows machine, or virtualize them. Containers are completely sandboxed and do not have any interface to each other.

Dockerfile

Dockerfile is a text document that contains commands for combining images. You can use to invoke any command from the command line. Docker automatically generates an image by reading instructions in Dockerfile.

The health detection of docker container is to write the detection mechanism into dockerfile when writing dockerfile. Based on the image generated by this docerfile, it will have the function of health detection when running the container.

Format in dockerfile:

HEALTHCHECK [options] CMD: sets the command to check the health of the container. HEALTHCHECK NONE: if the basic image has health check instructions, use this line to block its health check instructions.

The HEALTHCHECK instruction, introduced by Docker 1.12, tells the Docker engine how to determine whether the state of the container is normal.

Before there is no HEALTHCHECK instruction, the Docker engine can only determine whether the container is abnormal by whether the main process in the container exits or not. In many cases this is fine, but if the program enters a deadlock or a dead loop, the application process does not exit, but the container is no longer able to provide services. Before 1.12, Docker will not detect this state of the container, so it will not be rescheduled, resulting in some containers that can no longer provide services but are still accepting user requests.

Since 1.12, Docker has provided the HEALTHCHECK instruction, through which one line of command is specified to determine whether the service state of the container main process is still normal, so as to reflect the actual state of the container.

When the HEALTHCHECK instruction is specified in a mirror, the container is started with it. The initial state will be starting. After the HEALTHCHECK instruction is checked successfully, it will become healthy. If it fails a certain number of times in a row, it will become unhealthy.

HEALTHCHECK supports the following options:

-- the interval between two interval=: health checks defaults to 30 seconds;-- the timeout of the timeout=: health check command runs. If this time is exceeded, this health check is considered to have failed, with a default of 30 seconds;-- retries=: regards the container status as unhealthy after a specified number of consecutive failures, with a default of 3 times.

Like CMD and ENTRYPOINT, HEALTHCHECK can only appear once. If more than one is written, only the last one takes effect.

The commands after HEALTHCHECK [options] CMD are formatted in the same format as ENTRYPOINT, divided into shell format and exec format. The return value of the command determines the success of the health check: 0: success; 1: failure; 2: keep, do not use this value.

Examples of usage:

[root@node02 test] # cat Dockerfile # Dockerfile file as follows: FROM nginx:latestCOPY test.txt / test.txtHEALTHCHECK-- interval=5s-- timeout=3s CMD cat / test.txt | | exit 1

Here, we set a check every 5 seconds (the interval here is very short, so it should be relatively long). If the health check command does not respond for more than 3 seconds, it will be regarded as a failure, and use CMD cat / test.txt | | exit 1 as the health check command.

Build an image:

[root@node02 test] # docker build-t lzj:v6.

Start a container:

[root@node02 test] # docker run-d-name web03 lzj:v6

When the container is running, you can view the running status of the container. The initial status is (health: starting), and when a test is successful, it will be converted to (healthy), as follows:

If the health check fails more than the number of retries, the status changes to (unhealthy). When I enter the container to delete the viewing test.txt file executed by its CMD, the status will be unhealthy, as follows:

To help troubleshoot, the output of the health check command (including stdout and stderr) is stored in the health state and can be viewed with docker inspect.

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report