Skip to content

Commit

Permalink
Step 0 - Node.js app & basic Dockerfile
Browse files Browse the repository at this point in the history
Signed-off-by: juan131 <[email protected]>
  • Loading branch information
juan131 committed Jan 24, 2019
1 parent 6876794 commit 295293c
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
50 changes: 48 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "express_example",
"version": "1.0.0",
"description": "Express App Example",
"author": "Juan Ariza <[email protected]>",
"main": "server.js",
"engines": {
"node": ">=10"
},
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.16.4"
}
}
20 changes: 20 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -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}`);

0 comments on commit 295293c

Please sign in to comment.