Step by step guide to build a docker image with Node + Django + Postgres

My motivation to write this blog so that you save some time on searching similar stuff again on multiple forums. Especially, first time users of docker

Some pre-requisites

You must be familiar with following:-

  1. Install docker locally: https://docs.docker.com/engine/installation/
  2. Build your own Docker image: https://docs.docker.com/engine/userguide/dockerimages/
  3. Publish your Docker image to Docker Hub: https://docs.docker.com/engine/userguide/dockerimages/#push-an-image-to-docker-hub
  4. Publish your Docker image to your self-hosted registry: https://docs.docker.com/engine/userguide/containers/dockerimages/#push-an-image-to-docker-hub

You could go through my other article which is specially for newbies who wants to use docker from scratch.

My learning & precautions:

— Please check all default packages which comes as part of Ubuntu image are updated as per latest stable version or at least what you need in your project.

— Make sure you finally have latest versions of Postgres, Node, Yarn, Python3, pip3 and related setuptools

— Split your docker file into logically if you are into try and debug mode. For e.g. if you gonna experiment with how to install django, node, postgres then it’s better you do one by one by commenting our rest of config.


FROM ubuntu:14.04
RUN apt-get update && apt-get install -y locales && rm -rf /var/lib/apt/lists/* \
&& localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
ENV LANG en_US.utf8
CMD ls

To build docker image using Dockerfile

$sudo docker build -t deepakshakya/ubuntu .
Password:
Sending build context to Docker daemon 3.072 kB
Step 1/4 : FROM ubuntu:14.04
14.04: Pulling from library/ubuntu
30d541b48fc0: Pull complete
8ecd7f80d390: Pull complete
46ec9927bb81: Pull complete
2e67a4d67b44: Pull complete
7d9dd9155488: Pull complete
Digest: sha256:62a5dce5ceccd7f1cb2672a571ebee52cad1f08eec9b57fe4965fb0968a9602e
Status: Downloaded newer image for ubuntu:14.04
— -> 7c09e61e9035
Step 2/4 : RUN apt-get update && apt-get install -y locales && rm -rf /var/lib/apt/lists/* && localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
— -> Running in 6480c0e38576
.
...
Fetched 22.5 MB in 7s (3011 kB/s)
Reading package lists…
Reading package lists…
Building dependency tree…
Reading state information…
locales is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 9 not upgraded.
— -> d0d2fbf93099
Removing intermediate container 6480c0e38576
Step 3/4 : ENV LANG en_US.utf8
— -> Running in f38b2eed4f22
— -> 631834b1ce09
Removing intermediate container f38b2eed4f22
Step 4/4 : CMD ls
— -> Running in b7ab1c29b13a
— -> 578685956775
Removing intermediate container b7ab1c29b13a
Successfully built 578685956775
$

Verify build:

$docker images

Remember what do we expect to see when we run this image?

We have mentioned ‘ls’ command to be run in Dockerfile

$docker run <Image ID>

So it works fine! Hurray!

Now let’s write our Dockerfile.

FROM ubuntu:14.04# Create app directory
RUN mkdir -p /usr/src/<project-dir>
WORKDIR /usr/src/<project-dir>
Adding some initial packages/libraries required:# Bundle app source
COPY . /usr/src/<project-dir>
Note: Dot (.) above assuming that your docker file exists in root folder of your project directory. Change it accordingly.RUN apt-get update && apt-get install -y \
locales \
python3-pip \
libgdal1-dev \
libxft-dev \
libfreetype6-dev \
libffi-dev \
vim \
nodejs \
npm \
curl \
wget

Note: You can append more or less as per your requirements.

Add some env variables related to locale.

This may not be obvious at this time but some libraries need it.

ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8

Framework: Install Node + NPM + YARN

#Installing yarn using npm
RUN sudo npm install -g yarnpkg
#Upgrading node version to latest stable
RUN sudo npm cache clean -f
RUN sudo npm install -g n
RUN sudo n stable
RUN sudo ln -sf /usr/local/n/versions/node/7.8.0/bin/node /usr/bin/node
#Upgrade yarn to latest stable
RUN npm install -g yarn
# Install app dependencies
RUN yarn install

I am going to use Python3 and related setuptools like pip3 and dependancies (using requirements.txt).

Fontend: Install Django + setuptools

#Install python dependancies
RUN pip3 install -U wheel setuptools pip
RUN pip3 install pip-tools
RUN pip-sync api/requirements.txt api/dev-requirements.txt

Backend: Install postgres

#Installing latest or 9.6 version of postgres
RUN sudo add-apt-repository “deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main” &&\
wget — quiet -O — https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add — &&\
sudo apt-get update &&\
sudo apt-get install -y postgresql-9.6 &&\
postgis

Note: above is a set of commands separated by ‘&&’ fed to RUN and ‘\’ is line break.

References