Skip to content

Commit 7e00cb0

Browse files
committed
[fix] docker entrypoint with awaiting database
1 parent a3eaee1 commit 7e00cb0

File tree

3 files changed

+61
-13
lines changed

3 files changed

+61
-13
lines changed

Dockerfile

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@ COPY ./ /tmp
1919

2020
RUN set -ex && \
2121
apk add --no-cache --virtual .build-deps gcc py-pip python-dev musl-dev && \
22-
pip list && \
23-
pip install '/tmp' && \
22+
pip --no-cache-dir install '/tmp' && \
2423
apk del .build-deps
2524

2625
VOLUME "/usr/lib/pypi-server"
2726

28-
COPY docker-entrypoint.sh /entrypoint.sh
29-
RUN rm -rf /root/.cache /var/cache/*
27+
COPY docker-entrypoint.py /usr/local/bin/entrypoint.py
28+
RUN chmod a+x /usr/local/bin/entrypoint.py
3029

3130
EXPOSE 80
3231

33-
ENTRYPOINT ["/entrypoint.sh"]
32+
ENTRYPOINT ["/usr/local/bin/entrypoint.py"]

docker-entrypoint.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/python
2+
import socket
3+
import os
4+
import sys
5+
import logging
6+
from time import sleep
7+
from distutils import spawn
8+
9+
try:
10+
from urlparse import urlparse as parse_url
11+
except ImportError:
12+
from urllib.parse import urlsplit as parse_url
13+
14+
logging.basicConfig(level=logging.INFO)
15+
log = logging.getLogger()
16+
17+
18+
def check_port(host, port):
19+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
20+
return sock.connect_ex((host, port)) == 0
21+
22+
23+
DEFAULT_PORTS = {
24+
'postgres': 5432,
25+
'postgresql': 5432,
26+
'mysql': 3306,
27+
}
28+
29+
30+
if __name__ == '__main__':
31+
args = sys.argv[1:]
32+
33+
if not args or args[0].startswith('-'):
34+
db_url = parse_url(os.getenv('DB', ''))
35+
host = db_url.hostname
36+
port = int(db_url.port or DEFAULT_PORTS[db_url.scheme])
37+
38+
args.insert(0, '/usr/bin/pypi-server')
39+
40+
while not check_port(db_url.hostname, port):
41+
log.info('Awaiting database...')
42+
sleep(1)
43+
continue
44+
45+
# Ensure database engine is ready
46+
sleep(5)
47+
48+
executable = args[0]
49+
50+
if not executable.startswith('/'):
51+
executable = spawn.find_executable(executable)
52+
53+
if not executable or not os.path.exists(executable):
54+
log.error('Command not found')
55+
exit(127)
56+
57+
os.execv(executable, args)

docker-entrypoint.sh

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)