first commit
This commit is contained in:
commit
34390002c6
12
Dockerfile
Normal file
12
Dockerfile
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
FROM nginx:alpine
|
||||||
|
|
||||||
|
RUN apk --update add curl
|
||||||
|
|
||||||
|
HEALTHCHECK \
|
||||||
|
--interval=2s \
|
||||||
|
--timeout=2s \
|
||||||
|
--retries=2 \
|
||||||
|
CMD curl -s http://localhost/status | grep -q '^alive$'
|
||||||
|
|
||||||
|
COPY entrypoint.sh /entrypoint.sh
|
||||||
|
ENTRYPOINT ["sh", "/entrypoint.sh"]
|
68
README.md
Normal file
68
README.md
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
## Docker Swarm health check example
|
||||||
|
|
||||||
|
This example shows how health check works in Docker Swarm.
|
||||||
|
|
||||||
|
### What does this container do?
|
||||||
|
|
||||||
|
Container runs a nginx server and it also runs a script which writes a ``dead``
|
||||||
|
status to ``/usr/share/nginx/html/status`` file on every odd run which is then
|
||||||
|
picked by the health check that expects ``alive`` status from the nginx running
|
||||||
|
in the container.
|
||||||
|
|
||||||
|
So every odd run the container gets ``unhealthy`` status.
|
||||||
|
Then you can observe that running this container in Docker Engine alone will
|
||||||
|
not restart it. Hence we will run it as a service in the Docker Swarm which
|
||||||
|
will ensure the container gets restarted until it gets ``healthy`` status.
|
||||||
|
|
||||||
|
### Building image
|
||||||
|
|
||||||
|
```
|
||||||
|
docker build -t healthcheck-test .
|
||||||
|
```
|
||||||
|
|
||||||
|
### Setting up env
|
||||||
|
|
||||||
|
Make sure you have recent Docker Engine installed.
|
||||||
|
|
||||||
|
```
|
||||||
|
docker swarm init
|
||||||
|
```
|
||||||
|
|
||||||
|
### Running the service
|
||||||
|
|
||||||
|
```
|
||||||
|
docker service create \
|
||||||
|
--name test1 \
|
||||||
|
--mount type=bind,src=/tmp,dst=/usr/share/nginx/html \
|
||||||
|
--detach healtcheck-test
|
||||||
|
```
|
||||||
|
|
||||||
|
### Watching the healtcheck
|
||||||
|
|
||||||
|
```
|
||||||
|
docker ps |grep test1
|
||||||
|
docker service ls
|
||||||
|
```
|
||||||
|
|
||||||
|
### Cleaning up
|
||||||
|
|
||||||
|
```
|
||||||
|
docker service rm test1
|
||||||
|
docker swarm leave --force
|
||||||
|
rm -rf -- /tmp/html
|
||||||
|
```
|
||||||
|
|
||||||
|
### Extra
|
||||||
|
|
||||||
|
If you want to restart the container manually, you need to downscale it to 0
|
||||||
|
first and then upscale it back again.
|
||||||
|
|
||||||
|
```
|
||||||
|
docker service scale test1=0
|
||||||
|
docker service scale test1=1
|
||||||
|
```
|
||||||
|
|
||||||
|
### References
|
||||||
|
|
||||||
|
- https://docs.docker.com/compose/compose-file/
|
||||||
|
- https://docs.docker.com/engine/reference/builder/
|
12
entrypoint.sh
Normal file
12
entrypoint.sh
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
# Mount /usr/share/nginx/html must be persistent for this test to work properly
|
||||||
|
# in the Docker Swarm since it does not just restart the container but rather
|
||||||
|
# removes it and then starts it.
|
||||||
|
SFILE=/usr/share/nginx/html/status
|
||||||
|
|
||||||
|
# I am unhealthy until I get restarted and then I will become healthy again.
|
||||||
|
# That way I will be healthy every even run and unhealthy every odd run.
|
||||||
|
grep dead $SFILE && ( echo alive | tee $SFILE ) || ( echo dead | tee $SFILE )
|
||||||
|
|
||||||
|
exec nginx -g "daemon off;"
|
Loading…
Reference in New Issue
Block a user