From 407668671c6bab4b385c5a0d455be1d07cc97051 Mon Sep 17 00:00:00 2001 From: Guillaume Vincent Date: Mon, 6 May 2019 16:31:18 +0200 Subject: [PATCH] Improve containers --- containers/backend/Dockerfile | 18 +++++++++--------- containers/backend/entrypoint.sh | 6 +++--- containers/docker-compose.yml | 19 ++++++++++++++----- containers/test.sh | 9 ++++----- containers/webserver/Dockerfile | 8 +++++--- containers/webserver/entrypoint.sh | 10 +++++----- containers/webserver/generate_apache_conf.py | 2 +- 7 files changed, 41 insertions(+), 31 deletions(-) diff --git a/containers/backend/Dockerfile b/containers/backend/Dockerfile index 2259859..1c3f674 100644 --- a/containers/backend/Dockerfile +++ b/containers/backend/Dockerfile @@ -3,15 +3,15 @@ FROM python:3.6 LABEL name="LessPass Backend" LABEL maintainer="LessPass " -RUN mkdir /opt/app -WORKDIR /opt/app -RUN python -m venv /opt/app/venv -COPY requirements.txt /opt/app/ -RUN /opt/app/venv/bin/python -m pip install --upgrade pip && \ - /opt/app/venv/bin/python -m pip install -r requirements.txt +RUN mkdir /app +WORKDIR /app +COPY requirements.txt /app +RUN python -m pip install --upgrade pip +RUN python -m pip install -r requirements.txt -COPY . /opt/app/ +COPY . /app +RUN python --version -ENTRYPOINT ["/opt/app/entrypoint.sh"] +ENTRYPOINT ["/app/entrypoint.sh"] -CMD ["/opt/app/venv/bin/gunicorn", "lesspass.wsgi:application", "--access-logfile", "-", "--error-logfile", "-", "--log-level", "debug", "--bind", "0.0.0.0:8000"] +CMD ["gunicorn", "lesspass.wsgi:application", "--access-logfile", "-", "--error-logfile", "-", "--log-level", "debug", "--bind", "0.0.0.0:8000"] diff --git a/containers/backend/entrypoint.sh b/containers/backend/entrypoint.sh index bfb4c50..8de9a2f 100755 --- a/containers/backend/entrypoint.sh +++ b/containers/backend/entrypoint.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash -/opt/app/venv/bin/python wait_db.py -/opt/app/venv/bin/python manage.py migrate -/opt/app/venv/bin/python manage.py collectstatic --clear --no-input +python wait_db.py +python manage.py migrate +python manage.py collectstatic --clear --no-input exec "$@" \ No newline at end of file diff --git a/containers/docker-compose.yml b/containers/docker-compose.yml index 445f8ea..dfe8d94 100644 --- a/containers/docker-compose.yml +++ b/containers/docker-compose.yml @@ -1,4 +1,5 @@ version: "3" + services: db: image: postgres:9.5 @@ -10,16 +11,24 @@ services: - '8000' links: - db - env_file: - - .env + environment: + - DATABASE_ENGINE + - DATABASE_HOST + - DATABASE_NAME + - DATABASE_PASSWORD + - DATABASE_PORT + - DATABASE_USER + - DEBUG + - SECRET_KEY webserver: build: ./webserver ports: - 80:80 - 443:443 - env_file: - - .env volumes: - - ./webserver/ssl:/opt/app/ssl + - ./webserver/ssl:/app/ssl + environment: + - FQDN + volumes: postgresql: \ No newline at end of file diff --git a/containers/test.sh b/containers/test.sh index 210064b..6757893 100755 --- a/containers/test.sh +++ b/containers/test.sh @@ -4,9 +4,8 @@ set -e # containers export COMPOSE_PROJECT_NAME=lesspass docker-compose build +docker-compose down -v docker-compose up -d -date -u -docker exec -it lesspass_backend_1 sh -c '/opt/app/venv/bin/python wait_db.py' -date -u -docker exec -it lesspass_backend_1 sh -c '/opt/app/venv/bin/python manage.py test' -docker-compose down +docker exec -it lesspass_backend_1 sh -c 'python wait_db.py' +docker exec -it lesspass_backend_1 sh -c 'python manage.py test' +docker-compose down -v diff --git a/containers/webserver/Dockerfile b/containers/webserver/Dockerfile index 1a4b552..9f3bd61 100644 --- a/containers/webserver/Dockerfile +++ b/containers/webserver/Dockerfile @@ -5,8 +5,10 @@ LABEL maintainer="LessPass " RUN apt-get update && apt-get install -y \ python3 \ - python3-jinja2 + python3-jinja2 \ + openssl \ + && rm -rf /var/lib/apt/lists/* -COPY . /opt/app/ +COPY . /app -ENTRYPOINT ["/opt/app/entrypoint.sh"] \ No newline at end of file +ENTRYPOINT ["/app/entrypoint.sh"] \ No newline at end of file diff --git a/containers/webserver/entrypoint.sh b/containers/webserver/entrypoint.sh index e182b25..857b841 100755 --- a/containers/webserver/entrypoint.sh +++ b/containers/webserver/entrypoint.sh @@ -4,23 +4,23 @@ create_wildcard_certificate () { openssl req -x509 -newkey rsa:4096 -nodes -keyout ${1}.key -out ${1}.crt -days 365 -subj "/C=FR/ST=Gironde/L=Bordeaux/O=LessPass/OU=LessPass/CN=*.${1}" } -if [[ ! -f /opt/app/ssl/${FQDN}.crt || ! -f /opt/app/ssl/${FQDN}.key ]]; then +if [[ ! -f /app/ssl/${FQDN}.crt || ! -f /app/ssl/${FQDN}.key ]]; then echo "${FQDN}.crt or ${FQDN}.key not found! Generate wildcard certificate" - cd /opt/app/ssl + cd /app/ssl create_wildcard_certificate ${FQDN} fi mkdir -p /etc/httpd/ssl chmod 755 /etc/httpd/ssl -cp /opt/app/ssl/${FQDN}.crt /etc/httpd/ssl/ +cp /app/ssl/${FQDN}.crt /etc/httpd/ssl/ chmod 644 /etc/httpd/ssl/${FQDN}.crt mkdir -p /etc/httpd/ssl/private chmod 710 /etc/httpd/ssl/private -cp /opt/app/ssl/${FQDN}.key /etc/httpd/ssl/private/ +cp /app/ssl/${FQDN}.key /etc/httpd/ssl/private/ chmod 640 /etc/httpd/ssl/private/${FQDN}.key -/opt/app/venv/bin/python /opt/app/generate_apache_conf.py +python /app/generate_apache_conf.py cat /etc/httpd/conf.d/lesspass.conf diff --git a/containers/webserver/generate_apache_conf.py b/containers/webserver/generate_apache_conf.py index 98a56d4..207aa18 100644 --- a/containers/webserver/generate_apache_conf.py +++ b/containers/webserver/generate_apache_conf.py @@ -12,6 +12,6 @@ if __name__ == "__main__": "DEBUG": os.environ.get("DEBUG", "0") == "1", } print(context) - jinja_template = Template(open("/opt/app/lesspass.conf.j2").read()) + jinja_template = Template(open("/app/lesspass.conf.j2").read()) with open("/etc/httpd/conf.d/lesspass.conf", "w") as f: f.write(jinja_template.render(context))