Docker Community Forums

Share and learn in the Docker community.

Docker nginx Healthcheck stopping nginx container starting


(tech74) #1

Hi,

Docker Version 18.06 Ubuntu base 16.04LTS
We have a custom nginx container which works fine before we enable the healthcheck in the dockerfile. Here is the Dockerfile. Just removing the HEALTHCHECK statement and everything works OK including curling the endpoint from within the container. As soon as we put the healthcheck in then curling the endpoint just hangs and outside the container we get connection refused

FROM nodesource/xenial:6.3.1

RUN apt-get update -qq && apt-get install -y build-essential nginx

COPY nginx.conf /etc/nginx/nginx.conf

COPY nginxcheck.js /etc/nginx/nginxcheck.js

RUN echo “daemon off;” >> /etc/nginx/nginx.conf

EXPOSE 443

WORKDIR /etc/nginx

HEALTHCHECK --interval=60s --timeout=60s --start-period=120s CMD node nginxcheck.js

CMD [“nginx”]


Here is our healthcheck file which reads in the url from an environemnt variable

var https = require(“https”);
var pushhost = process.env.PUSH_HOST
var postdata = querystring.stringify({ ‘param’ : ‘38388’});

var options = {
host : pushhost,
port : “443”,
path: ‘/health’,
method: ‘POST’,
timeout : 5000,

};

var request = https.request(options, function(res) {
console.log( 'STATUS: ’ + res.statusCode);
if (res.statusCode == 200) {
process.exit(0);
}
else {
process.exit(1);
}
});

request.on(‘error’, function(err) {
console.log('ERROR ’ + err);
process.exit(1);
});

request.write(postdata);
request.end();


Here is nginx.conf location stuff

server {
listen 443;
ssl on;
ssl_certificate /etc/nginx/certs/bundle.crt;
ssl_certificate_key /etc/nginx/certs/bundle.crt;

		location /health {
    		return 200 'alive';
    		add_header Content-Type text/plain;
		}

		location / {
    		proxy_http_version 1.1;
    		proxy_cache_bypass $http_upgrade;
    		proxy_set_header Upgrade $http_upgrade;
    		proxy_set_header Connection 'upgrade';
    		proxy_set_header Host $host;
    		proxy_set_header X-Real-IP $remote_addr;
    		proxy_pass http://localhost:1337/;
		}

}