Commit cb80a8d9 authored by Alexander Lercher's avatar Alexander Lercher

Improved build script and Docker builds


Caching pip packages as suggested by Alfonso.
parent 31f80acb
import os import os
import shutil import shutil
import sys import sys
from typing import Dict
DOCKER_COMPOSE_NAME = "Dockerfile" DOCKER_BUILD_NAME = "Dockerfile"
ROOT = './' ROOT = './'
SOURCEPATH = f'{ROOT}src/' SOURCEPATH = f'{ROOT}src/'
DOCKER_USERNAME = "alexx882" DOCKERHUB_REPO_OWNER = "alexx882"
p_image_name = None
if len(sys.argv) == 2: def print_help():
# parameter contains image to build print("""
p_image_name = sys.argv[1] Use this script to build Docker images and upload them to Docker Hub.
For the upload to work, the current user must be logged in on the Docker CLI and must have push access to the repositories.
paths = []
for r, _, f in os.walk(SOURCEPATH): Parameters: [image_name]
for filename in f:
if DOCKER_COMPOSE_NAME == filename: image_name ... only build this image
paths.append(os.path.normpath(r)) """)
command_args = [{'path': path,
'name': str(path).split(os.path.normpath('/'))[-1]} def get_dockerfile_paths_and_names() -> Dict[str, str]:
for path paths = []
in paths] for r, _, f in os.walk(SOURCEPATH):
for filename in f:
if p_image_name is not None: if DOCKER_BUILD_NAME == filename:
command_args = [x for x in command_args if x['name']==p_image_name] paths.append(os.path.normpath(r))
error:int = 0 command_args = [{'path': path,
res_str = [] 'name': str(path).split(os.path.normpath('/'))[-1]}
for command_arg in command_args: for path
path = command_arg['path'] in paths]
image_name = f"{DOCKER_USERNAME}/{command_arg['name']}"
return command_args
# copy Dockerfile into root dir to have access to modules folder
shutil.copy2(os.path.join(path, DOCKER_COMPOSE_NAME), ROOT)
if __name__ == '__main__':
# build then remove Dockerfile p_image_name = None
exit_val = os.system(f"docker image build -t {image_name} {ROOT}")
os.remove(os.path.join(ROOT, DOCKER_COMPOSE_NAME)) if len(sys.argv) == 2:
arg = sys.argv[1]
res_str.append(f"{image_name} built with exit code {exit_val}") if arg == 'help':
print_help()
if exit_val != 0: sys.exit(0)
error = exit_val else:
# parameter contains image name to be build
else: p_image_name = arg
# push created Docker image
exit_val = os.system(f"docker push {image_name}") command_args = get_dockerfile_paths_and_names()
res_str.append(f"{image_name} pushed with exit code {exit_val}") if p_image_name is not None:
# filter for argument image name
command_args = [x for x in command_args
if (p_image_name in x['name'])]
error: int = 0
res_str = []
for command_arg in command_args:
path = command_arg['path']
image_name = f"{DOCKERHUB_REPO_OWNER}/{command_arg['name']}"
# copy Dockerfile into root dir to have access to modules folder
shutil.copy2(os.path.join(path, DOCKER_BUILD_NAME), ROOT)
# build then remove Dockerfile
exit_val = os.system(f"docker image build -t {image_name} {ROOT}")
os.remove(os.path.join(ROOT, DOCKER_BUILD_NAME))
res_str.append(f"{image_name} built with exit code {exit_val}")
if exit_val != 0: if exit_val != 0:
error = exit_val error = exit_val
else:
# push created Docker image
exit_val = os.system(f"docker push {image_name}")
res_str.append(f"{image_name} pushed with exit code {exit_val}")
if exit_val != 0:
error = exit_val
print(f"Found {len(command_args)} images") print(f"Found {len(command_args)} images")
for s in res_str: for s in res_str:
print(s) print(s)
sys.exit(1 if error > 0 else 0) sys.exit(1 if error > 0 else 0)
\ No newline at end of file
...@@ -9,10 +9,13 @@ RUN apt-get update ...@@ -9,10 +9,13 @@ RUN apt-get update
EXPOSE 5000 EXPOSE 5000
WORKDIR /app WORKDIR /app
COPY src/modules/ /app/
COPY src/data-hub/community-detection-microservice/app/ /app/
# https://www.aptible.com/documentation/faq/deploy/dockerfile-caching/pip-dockerfile-caching.html
COPY src/data-hub/community-detection-microservice/app/requirements.txt /app/
RUN pip install -r requirements.txt RUN pip install -r requirements.txt
COPY src/modules/ /app/
COPY src/data-hub/community-detection-microservice/app/ /app/
RUN chmod a+x main.py RUN chmod a+x main.py
CMD ["python", "./main.py"] CMD ["python", "./main.py"]
\ No newline at end of file
...@@ -9,10 +9,12 @@ RUN apt-get update ...@@ -9,10 +9,12 @@ RUN apt-get update
EXPOSE 5000 EXPOSE 5000
WORKDIR /app WORKDIR /app
COPY src/data-hub/semantic-linking-microservice/app/ /app/
COPY src/modules/ /app/
COPY src/data-hub/semantic-linking-microservice/app/requirements.txt /app/
RUN pip install -r requirements.txt RUN pip install -r requirements.txt
COPY src/data-hub/semantic-linking-microservice/app/ /app/
COPY src/modules/ /app/
RUN chmod a+x main.py RUN chmod a+x main.py
CMD ["python", "./main.py"] CMD ["python", "./main.py"]
\ No newline at end of file
...@@ -9,10 +9,12 @@ RUN apt-get update ...@@ -9,10 +9,12 @@ RUN apt-get update
EXPOSE 5000 EXPOSE 5000
WORKDIR /app WORKDIR /app
COPY src/rest-gateway/app/ /app/
COPY src/modules/ /app/
COPY src/rest-gateway/app/requirements.txt /app/
RUN pip install -r requirements.txt RUN pip install -r requirements.txt
COPY src/rest-gateway/app/ /app/
COPY src/modules/ /app/
RUN chmod a+x main.py RUN chmod a+x main.py
CMD ["python", "./main.py"] CMD ["python", "./main.py"]
\ No newline at end of file
...@@ -9,10 +9,12 @@ RUN apt-get update ...@@ -9,10 +9,12 @@ RUN apt-get update
EXPOSE 5000 EXPOSE 5000
WORKDIR /app WORKDIR /app
COPY src/transaction-hub-in/trace-retrieval-microservice/app/ /app/
COPY src/modules/ /app/
COPY src/transaction-hub-in/trace-retrieval-microservice/app/requirements.txt /app/
RUN pip install -r requirements.txt RUN pip install -r requirements.txt
COPY src/transaction-hub-in/trace-retrieval-microservice/app/ /app/
COPY src/modules/ /app/
RUN chmod a+x main.py RUN chmod a+x main.py
CMD ["python", "./main.py"] CMD ["python", "./main.py"]
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment