From 295293c1664eb2b9c6e0401371d57c640d8a8990 Mon Sep 17 00:00:00 2001 From: juan131 Date: Thu, 24 Jan 2019 12:38:57 +0100 Subject: [PATCH] Step 0 - Node.js app & basic Dockerfile Signed-off-by: juan131 --- Dockerfile | 12 ++++++++++++ README.md | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- package.json | 16 ++++++++++++++++ server.js | 20 ++++++++++++++++++++ 4 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 Dockerfile create mode 100644 package.json create mode 100644 server.js diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6544279 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM debian +# Copy application files +COPY . /app +# Install required system packages +RUN apt-get update +RUN apt-get -y install curl software-properties-common gnupg vim ssh +RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - +RUN apt-get -y install nodejs +# Install NPM dependencies +RUN npm install --prefix /app +EXPOSE 80 +CMD ["npm", "start", "--prefix", "app"] diff --git a/README.md b/README.md index ef83d16..868224f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,48 @@ -# dockerfile-best-practices -Best Practices writing a Dockerfile +# Best Practices writing a Dockerfile + +This repository is a guide with a set of good practices when writting Dockerfiles. + +Using a **Node.js** application as example, this guide will be a journey from a very basic Dockerfile to make it production ready, describing some of the best practices and common pitfalls that you are likely to encounter when developing Dockerfiles. + +## Before we start... + +On [this blog post]() you'll find detailed information about each of the steps we'll do to improve the Dockerfile. Please use it to follow this tutorial. + +### Enable BuilKit + +Use [BuildKit](https://github.com/moby/buildkit) to build your Docker images. It can be enabled on two different ways: + +- Exporting the `DOCKER_BUILDKIT` environment variable: + +```bash +$ export DOCKER_BUILDKIT=1 +``` + +> TIP: add it to your ~/.bashrc file + +- [Configuring the Docker Daemon](https://docs.docker.com/config/daemon/#configure-the-docker-daemon) to add the **Buildkit** feature: + +```json +{ + “features”: { + “buildkit”: true + } +} +``` + +## How to use this tutorial + +Starting from the 'master' branch, you'll find a branch with the files to use on each step of the tutorial. + +It's only necessary to switch (checkout) to the proper branch. The available branches are: + +- [1-cache-improvements](https://github.com/juan131/dockerfile-best-practices/blob/1-cache-improvements) +- [2-unused-dependencies](https://github.com/juan131/dockerfile-best-practices/blob/2-unused-dependencies) +- [3-minideb](https://github.com/juan131/dockerfile-best-practices/blob/3-minideb) +- [4-maintained-images](https://github.com/juan131/dockerfile-best-practices/blob/4-maintained-images) +- [5-multi-stage](https://github.com/juan131/dockerfile-best-practices/blob/5-multi-stage) +- [6-non-root](https://github.com/juan131/dockerfile-best-practices/blob/6-non-root) +- [7-workdir](https://github.com/juan131/dockerfile-best-practices/blob/7-workdir) +- [8-mounted-configuration](https://github.com/juan131/dockerfile-best-practices/blob/8-mounted-configuration) +- [9-logs](https://github.com/juan131/dockerfile-best-practices/blob/9-logs) +- [10-entrypoint](https://github.com/juan131/dockerfile-best-practices/blob/10-entrypoint) diff --git a/package.json b/package.json new file mode 100644 index 0000000..c62d44f --- /dev/null +++ b/package.json @@ -0,0 +1,16 @@ +{ + "name": "express_example", + "version": "1.0.0", + "description": "Express App Example", + "author": "Juan Ariza ", + "main": "server.js", + "engines": { + "node": ">=10" + }, + "scripts": { + "start": "node server.js" + }, + "dependencies": { + "express": "^4.16.4" + } +} diff --git a/server.js b/server.js new file mode 100644 index 0000000..abb4d60 --- /dev/null +++ b/server.js @@ -0,0 +1,20 @@ +'use strict'; + +const express = require('express'); +const fs = require('fs'); + +const myLogFileStream = fs.createWriteStream('/var/log/app.log'); +const myConsole = new console.Console(myLogFileStream, myLogFileStream); + +// Constants +const serverHost = '127.0.0.1'; +const serverPort = 80; + +// Express app +const app = express(); +app.get('/', (req, res) => { + res.send('Hello world\n'); +}); + +app.listen(serverPort); +myConsole.log(`Running on http://${serverHost}:${serverPort}`);