Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Enhance Dockerfile to use multi-stage build

Filip Pytlounbaf7d616136e
diff --git a/Dockerfile b/Dockerfile
old mode 100755
new mode 100644
index 61fe028095..d36957326e
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,18 +1,56 @@
-FROM python:3.6
+### Build python virtualenv
+FROM python:3.6-stretch as build-python
ENV PYTHONUNBUFFERED 1
RUN \
apt-get -y update && \
- apt-get install -y gettext && \
- apt-get clean
+ apt-get install -y gettext python3-virtualenv virtualenv && \
+ apt-get clean && \
+ rm -rf /var/lib/apt/lists/*
+RUN virtualenv -p python3 /app/virtualenv
ADD requirements.txt /app/
-RUN pip install -r /app/requirements.txt
+RUN /app/virtualenv/bin/pip install -r /app/requirements.txt
+
+### Build static assets
+FROM node:8.6.0 as build-nodejs
+# Install node_modules
+ADD webpack.config.js app.json package.json package-lock.json /app/
+WORKDIR /app
+RUN npm install
+
+# Build static
+ADD ./saleor/static /app/saleor/static/
+ADD ./templates /app/templates/
+RUN npm run build-assets && \
+ npm run build-emails
+
+### Final image
+FROM python:3.6-slim-stretch
+
+RUN apt-get update && \
+ apt-get install -y libxml2 libssl1.1 libcairo2 libpango-1.0-0 libpangocairo-1.0-0 libgdk-pixbuf2.0-0 shared-mime-info && \
+ apt-get clean && \
+ rm -rf /var/lib/apt/lists/*
ADD . /app
+COPY --from=build-python /app/virtualenv /app/virtualenv
+COPY --from=build-nodejs /app/saleor/static /app/saleor/static
+COPY --from=build-nodejs /app/webpack-bundle.json /app/
+COPY --from=build-nodejs /app/templates /app/templates
WORKDIR /app
+RUN useradd --system saleor && \
+ mkdir /app/media /app/static && \
+ chown -R saleor:saleor /app/media /app/static
+
+USER saleor
+
+RUN SECRET_KEY=dummy /app/virtualenv/bin/python manage.py collectstatic --no-input
+
EXPOSE 8000
ENV PORT 8000
-CMD ["uwsgi", "/app/saleor/wsgi/uwsgi.ini"]
+ENV PYTHONUNBUFFERED 1
+ENV PROCESSES 4
+CMD ["/app/virtualenv/bin/uwsgi", "/app/saleor/wsgi/uwsgi.ini"]
diff --git a/docker-compose.yml b/docker-compose.yml
old mode 100755
new mode 100644
index f2439e5afc..5122d9202a
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -16,9 +16,11 @@ services:
context: .
dockerfile: Dockerfile
env_file: common.env
- command: celery -A saleor worker --app=saleor.celeryconf:app --loglevel=info
+ command: /app/virtualenv/bin/celery -A saleor worker --app=saleor.celeryconf:app --loglevel=info
volumes:
- - .:/app:Z
+ - ./saleor:/app/saleor:Z
+ - ./locale:/app/locale:Z
+ - ./templates:/app/templates:Z
links:
- redis
depends_on:
@@ -31,8 +33,9 @@ services:
ports:
- '127.0.0.1:9200:9200'
web:
- build: .
- command: python manage.py runserver 0.0.0.0:8000
+ build:
+ context: .
+ dockerfile: Dockerfile
env_file: common.env
depends_on:
- db
@@ -41,4 +44,6 @@ services:
ports:
- '8000:8000'
volumes:
- - .:/app:Z
+ - ./saleor:/app/saleor:Z
+ - ./locale:/app/locale:Z
+ - ./templates:/app/templates:Z