Skip to content

Commit

Permalink
Docker build improvements. (#150)
Browse files Browse the repository at this point in the history
* Add .dockerignore to stop copying .git/, npm_modules/, and dist/ into image.

Remove unneeded and slow recursive chown from Dockerfile.

* Switch from npm to yarn to install packages.

* Dockerfile installs node-v6.11.3 and yarn-1.0.2 as packages.
  • Loading branch information
normangilmore authored Sep 22, 2017
1 parent dcaf940 commit 5a4f76d
Show file tree
Hide file tree
Showing 4 changed files with 7,164 additions and 11,480 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.dockerignore
.git*
dist/
dist/*
node_modules*
59 changes: 42 additions & 17 deletions docker/thresher_api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,46 @@ COPY /docker/bashrc_to_docker /root/.bashrc
ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update -q && apt-get install -q -y \
curl apt-transport-https apt-utils \
gcc \
gettext \
postgresql-client libpq-dev \
vim git curl less psmisc
vim less psmisc \
python

WORKDIR /home/download
ARG NODEREPO="node_6.x"
ARG DISTRO="jessie"
# Only newest package kept in nodesource repo. Cannot pin to version using apt!
# See https://github.com/nodesource/distributions/issues/33
#RUN curl -sSO https://deb.nodesource.com/gpgkey/nodesource.gpg.key
#RUN apt-key add nodesource.gpg.key
#RUN echo "deb https://deb.nodesource.com/${NODEREPO} ${DISTRO} main" > /etc/apt/sources.list.d/nodesource.list
#RUN echo "deb-src https://deb.nodesource.com/${NODEREPO} ${DISTRO} main" >> /etc/apt/sources.list.d/nodesource.list
#RUN apt-get update -q && apt-get install -q -y 'nodejs=6.11.3*'

# So we have to download specific package in order to pin version of node.
# dpkg doesn't install package dependencies automatically, we have to do it.
# This nodejs package has a dependency on the 'python' package, installed above.
# Since this is a python:2.7-slim image, you would think python packages
# are installed. But the Dockerfile installs Python from source, not packages.

# jessie specific version not provided starting with 6.11.3
#ARG NODE_PACKAGE=nodejs_6.11.2-1nodesource1~jessie1_amd64.deb
ARG NODE_PACKAGE=nodejs_6.11.3-1nodesource1_amd64.deb
RUN curl -sSO "https://deb.nodesource.com/node_6.x/pool/main/n/nodejs/${NODE_PACKAGE}"
RUN dpkg -i ${NODE_PACKAGE}

# npm upgrade doesn't work due to bug in nodejs .deb package!
# Must install nodejs at terminal for npm upgrade to work!
#RUN npm install -g npm
#RUN npm --version

RUN curl -sSO https://dl.yarnpkg.com/debian/pubkey.gpg
RUN apt-key add pubkey.gpg
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list

RUN apt-get update -q && apt-get install -q -y 'yarn=1.0.2*'

RUN rm -rf /var/lib/apt/lists/*

Expand All @@ -30,15 +66,6 @@ ARG HOME=/home/thresher
RUN groupadd -g ${GROUP_ID} ${USER} && \
useradd --shell /bin/bash --home ${HOME} -u ${USER_ID} -g ${GROUP_ID} ${USER}

WORKDIR /home/download
# https://nodejs.org/dist/v6.10.3/node-v6.10.3-linux-x64.tar.xz
ARG NODE_VERSION=v6.10.3
ARG NODE_FILE=node-${NODE_VERSION}-linux-x64.tar.xz
# This merges files into /usr/local/{bin,include,lib,share}
# So node is installed for all users
RUN curl -O https://nodejs.org/dist/${NODE_VERSION}/${NODE_FILE}
RUN tar -C /usr/local --strip-components 1 -xf ${NODE_FILE}

# Let Django copy to static dir as regular user
WORKDIR /var/www
RUN chown ${USER}:${USER} /var/www
Expand All @@ -47,20 +74,18 @@ ENV PYTHONPATH /home/thresher
# note: Development config mounts the git repo root on this path.
# Copy minimal files needed to npm install to try
# and re-use cache
ENV WEBPACK_BUILD_DIR /home/thresher
WORKDIR /home/thresher
RUN chown ${USER}:${USER} /home/thresher
COPY /package.json /home/thresher
RUN chown -R ${USER}:${USER} /home/thresher
COPY /yarn.lock /home/thresher

ENV WEBPACK_BUILD_DIR /home/thresher
WORKDIR /home/thresher
USER ${USER}
RUN npm install
RUN yarn install

# Now copy the rest of the repo into the image - any file change breaks build cache
# .dockerignore must exclude node_modules/ and dist/
COPY / /home/thresher
USER root
RUN chown -R ${USER}:${USER} /home/thresher
USER ${USER}
RUN npm run deploy:quiet

COPY /docker/bashrc_to_docker ${HOME}/.bashrc
Expand Down
Loading

0 comments on commit 5a4f76d

Please sign in to comment.