From 6b6314227e2ffdb7aa3f6f0d4503fb0dc580b7a4 Mon Sep 17 00:00:00 2001 From: Antonis Kalipetis Date: Mon, 12 Dec 2016 16:37:23 +0200 Subject: [PATCH] Improve the Docker image 1. Bump Node version to 6.9 - the latest LTS 2. Include the `cpio` binary, used during building 3. Ignore node_modules and .git directories for faster builds 4. Run the tests and build during the image build, to make sure the image is not built if these break 5. Make npm run dev the default command 6. Add an entrypoint to automatically install Node modules if they do nott exist Signed-off-by: Antonis Kalipetis --- .dockerignore | 2 ++ Dockerfile | 25 +++++++++++++++++++++++-- docker-compose.yaml | 9 +++++++++ entrypoint.sh | 8 ++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 .dockerignore create mode 100644 docker-compose.yaml create mode 100755 entrypoint.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..651665bb --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +node_modules +.git diff --git a/Dockerfile b/Dockerfile index 105e997e..ffbcd379 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,25 @@ -FROM node:4-onbuild +FROM node:6.9 MAINTAINER Paris Kasidiaris -EXPOSE 3000 +# Install cpio, used for building +RUN apt-get update \ + && apt-get install -y --no-install-recommends cpio \ + && rm -rf /var/lib/apt/lists/* + +# Set the working directory +WORKDIR /usr/src/app + +# Set an entrypoint, to automatically install node modules +COPY entrypoint.sh /entrypoint.sh +ENTRYPOINT ["/entrypoint.sh"] + +# First, install dependencies to improve layer caching +COPY package.json /usr/src/app/ +RUN npm install + +# Add the code +COPY . /usr/src/app + +# Run the tests and build, to make sure everything is working nicely +RUN npm run test && npm run build +CMD ["npm", "run", "dev"] diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 00000000..22ea2e20 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,9 @@ +version: '2' + +services: + web: + build: ./ + volumes: + - ./:/usr/src/app + ports: + - 3000:3000 diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 00000000..4c63d001 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,8 @@ +#! /bin/bash + +# Install Node modules, if the `node_modules` directory does not exist +if [[ ! -d node_modules ]]; then + npm install; +fi + +exec "$@"