2015-12-31 18:50:25 +01:00
#!/bin/bash
2017-11-22 00:19:14 +01:00
# shellcheck disable=SC2155
2015-12-31 18:50:25 +01:00
2016-01-03 12:31:24 +01:00
set -u
2018-01-14 21:45:17 +01:00
DOCKER_PROVIDER = ${ DOCKER_PROVIDER :- docker }
case " ${ DOCKER_PROVIDER } " in
ecs| ECS)
# AWS ECS. Enabled in /etc/ecs/ecs.config (http://docs.aws.amazon.com/AmazonECS/latest/developerguide/container-metadata.html)
if [ [ -n " ${ ECS_CONTAINER_METADATA_FILE :- } " ] ] ; then
export CONTAINER_ID = $( grep ContainerID " ${ ECS_CONTAINER_METADATA_FILE } " | sed 's/.*: "\(.*\)",/\1/g' )
else
echo " ${ DOCKER_PROVIDER } specified as 'ecs' but not available. See: http://docs.aws.amazon.com/AmazonECS/latest/developerguide/container-metadata.html "
exit 1
fi
; ;
*)
export CONTAINER_ID = $( sed -nE 's/^.+docker[\/-]([a-f0-9]{64}).*/\1/p' /proc/self/cgroup | head -n 1)
; ;
esac
2015-12-31 18:50:25 +01:00
if [ [ -z " $CONTAINER_ID " ] ] ; then
echo "Error: can't get my container ID !" >& 2
exit 1
fi
function check_docker_socket {
2016-01-06 19:33:16 +01:00
if [ [ $DOCKER_HOST = = unix://* ] ] ; then
socket_file = ${ DOCKER_HOST #unix : // }
if [ [ ! -S $socket_file ] ] ; then
cat >& 2 <<-EOT
2015-12-31 18:50:25 +01:00
ERROR: you need to share your Docker host socket with a volume at $socket_file
Typically you should run your container with: \` -v /var/run/docker.sock:$socket_file :ro\`
See the documentation at http://git.io/vZaGJ
EOT
2016-01-06 19:33:16 +01:00
exit 1
fi
fi
2015-12-31 18:50:25 +01:00
}
2016-01-06 19:33:16 +01:00
2015-12-31 18:50:25 +01:00
function get_nginx_proxy_cid {
2016-01-06 19:33:16 +01:00
# Look for a NGINX_VERSION environment variable in containers that we have mount volumes from.
local volumes_from = $( docker_api " /containers/ $CONTAINER_ID /json " | jq -r '.HostConfig.VolumesFrom[]' 2>/dev/null)
for cid in $volumes_from ; do
2016-01-03 12:26:37 +01:00
cid = ${ cid % : * } # Remove leading :ro or :rw set by remote docker-compose (thx anoopr)
2016-01-06 19:33:16 +01:00
if [ [ $( docker_api " /containers/ $cid /json " | jq -r '.Config.Env[]' | egrep -c '^NGINX_VERSION=' ) = "1" ] ] ; then
2016-02-11 21:18:20 +01:00
export NGINX_PROXY_CONTAINER = $cid
2016-01-06 19:33:16 +01:00
break
fi
done
2017-07-13 12:44:02 +02:00
if [ [ -z " $( nginx_proxy_container) " ] ] ; then
2016-01-06 19:33:16 +01:00
echo "Error: can't get nginx-proxy container id !" >& 2
2017-04-13 13:09:13 +02:00
echo "Check that you use the --volumes-from option to mount volumes from the nginx-proxy or label the nginx proxy container to use with 'com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy=true'." >& 2
2016-01-06 19:33:16 +01:00
exit 1
fi
2015-12-31 18:50:25 +01:00
}
function check_writable_directory {
local dir = " $1 "
2017-02-13 19:11:53 +01:00
docker_api " /containers/ $CONTAINER_ID /json " | jq ".Mounts[].Destination" | grep -q " ^\" $dir \" $"
2016-08-18 04:16:00 +02:00
if [ [ $? -ne 0 ] ] ; then
echo " Warning: ' $dir ' does not appear to be a mounted volume. "
fi
2015-12-31 18:50:25 +01:00
if [ [ ! -d " $dir " ] ] ; then
echo " Error: can't access to ' $dir ' directory ! " >& 2
2018-02-10 00:11:24 +01:00
echo " Check that ' $dir ' directory is declared as a writable volume. " >& 2
2015-12-31 18:50:25 +01:00
exit 1
fi
touch $dir /.check_writable 2>/dev/null
if [ [ $? -ne 0 ] ] ; then
echo " Error: can't write to the ' $dir ' directory ! " >& 2
echo " Check that ' $dir ' directory is export as a writable volume. " >& 2
exit 1
fi
rm -f $dir /.check_writable
}
2016-01-05 14:03:22 +01:00
function check_dh_group {
if [ [ ! -f /etc/nginx/certs/dhparam.pem ] ] ; then
echo "Creating Diffie-Hellman group (can take several minutes...)"
2016-02-26 19:07:56 +01:00
openssl dhparam -out /etc/nginx/certs/.dhparam.pem.tmp 2048
2016-01-05 14:03:22 +01:00
mv /etc/nginx/certs/.dhparam.pem.tmp /etc/nginx/certs/dhparam.pem || exit 1
2016-01-06 19:33:16 +01:00
fi
2016-01-05 14:03:22 +01:00
}
2016-06-26 00:31:15 +02:00
source /app/functions.sh
2016-01-06 19:33:16 +01:00
2016-01-03 12:31:24 +01:00
[ [ $DEBUG = = true ] ] && set -x
2015-12-31 18:50:25 +01:00
if [ [ " $* " = = "/bin/bash /app/start.sh" ] ] ; then
check_docker_socket
2017-07-13 12:44:02 +02:00
if [ [ -z " $( docker_gen_container) " ] ] ; then
2016-02-11 21:18:20 +01:00
[ [ -z " ${ NGINX_PROXY_CONTAINER :- } " ] ] && get_nginx_proxy_cid
fi
2015-12-31 18:50:25 +01:00
check_writable_directory '/etc/nginx/certs'
2016-01-01 14:32:40 +01:00
check_writable_directory '/etc/nginx/vhost.d'
2015-12-31 18:50:25 +01:00
check_writable_directory '/usr/share/nginx/html'
2016-01-05 14:03:22 +01:00
check_dh_group
2015-12-31 18:50:25 +01:00
fi
exec " $@ "