deathangel908
deathangel908

Reputation: 9709

Docker check if file exists in healthcheck

How do I wait until a file is created in docker? I'm trying the code below, but it doesn't work. If I execute bash -c [ -f /tmp/asdasdasd ] separate from docker shell, it gives me the correct result.

Dockerfiletest:

FROM alpine:3.6
RUN apk update && apk add bash

docker-compose.yml:

version: '2.1'
services:
  testserv:
    build:
      context: .
      dockerfile: ./Dockerfiletest
    command:
       bash -c "rm /tmp/a && sleep 5 && touch /tmp/a && sleep 100"
    healthcheck:
      # I tried adding '&& exit 1',  '|| exit `' it doesn't work.
      test: bash -c [ -f /tmp/a ]
      timeout: 1s
      retries: 20

docker-compose up + wait 10s + docker ps:

: docker ps
STATUS    
Up About a minute (health: starting)

Upvotes: 5

Views: 14150

Answers (2)

deathangel908
deathangel908

Reputation: 9709

It turns out that besides missing quotes I also checked existence of socket via -f when I should do

bash -c '[ -S /tmp/uwsgi.sock ]'

Furthermore interval: 5s could be used to decrease default 5s interval.

Upvotes: 3

BMitch
BMitch

Reputation: 264831

I believe you are missing quotes on the command to run. bash -c only accepts one parameter, not a list, so you need to quote the rest of that line to pass it as a single parameter:

bash -c "[ -f /tmp/a ]"

To see the results of your healthcheck, you can run:

docker inspect $container_id -f '{{ json .State.Health.Log }}' | jq .

Upvotes: 8

Related Questions