Public Repository

Last pushed: a year ago
Short Description
Node.js, PostgreSQL and PostGIS for Bitbucket Pipelines
Full Description

Node.js, PostgreSQL and PostGIS for Bitbucket Pipelines

This repository contains a Dockerfile as well as a simple example that shows how you can run your own Docker container with Node.js and PostgreSQL and PostGIS on Bitbucket Pipelines.

The Docker image is using node 6.3, PostgreSQL 9.4 and PostGIS 2.1.4.

Quickstart

Using the image with Bitbucket Pipelines

Just copy/paste the YML below in your bitbucket-pipelines.yml and adapt the script to your needs.

# This is a sample build configuration for Javascript.
# Only use spaces to indent your .yml configuration.
# -----
# You can specify a custom docker image from Dockerhub as your build environment.
image: chillnet/node-postgis-bitbucket-pipeline

pipelines:
  default:
    - step:
        script: # Modify the commands below to build your repository.
          - /etc/init.d/postgresql start # starting the service
          - sudo -u postgres sh -c 'createuser root && createdb testdb' # creating a root user 
          - sudo -u postgres psql -c "ALTER USER root PASSWORD 'password';" # setting up the root password
          - sudo -u postgres psql --dbname=testdb -c "CREATE EXTENSION IF NOT EXISTS postgis;" # Install the PostGis extention on your database
          - npm install pg
          - node test.js

Using this in a script

You'll find a sample script in this repository in test.js. It simply connects to PostgreSQL and list the existing databases.

var pg = require('pg');

var client = new pg.Client('postgres://root:password@localhost:5432/node');

// connect to our database 
client.connect(function (err) {
  if (err) throw err;

  // execute a query on our database 
  client.query('SELECT datname FROM pg_database WHERE datistemplate = false;', function (err, result) {
    if (err) throw err;

    // just print the result to the console 
    console.log(result.rows); 

    // disconnect the client 
    client.end(function (err) {
      if (err) throw err;
    });
  });
});

Create your own image

If you want to use a different version of Node.js you can simply create your own image for it. Just copy the content of the Dockerfile and replace the first line.

This image is built from the official Node.js image at https://hub.docker.com/_/node/ and you can find there all the different versions that are supported.

Your Dockerfile won't need to have an ENTRYPOINT or CMD line as Bitbucket Pipelines will run the script commands that you put in your bitbucket-pipelines.yml file instead.

FROM node:6.3

RUN apt-get update \
  && apt-get install -y postgresql postgresql-contrib postgis \
  && apt-get install sudo \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# This Dockerfile doesn't need to have an entrypoint and a command
# as Bitbucket Pipelines will overwrite it with a bash script.

Build the image
Note: Now that everything looks good, its time to build our Docker Image (don’t forget the . period at the and of the command).:

docker build -t node-postgresql .

The above command takes the Dockerfile in the current folder and builds an image called <your-docker-account>/node-postgresql on your local machine.

Verify your docker image

You can verify that your Docker image is indeed stored on your computer with:

docker images

Run the image locally with bash to make some tests

docker run -i -t node-postgresql /bin/bash

Publishing your Custom Docker Image on Docker Hub

Your next option is to publish the created Docker image on the Docker Hub Repository. To do so, you will need to create an account on the Docker Hub signup webpage where you will provide a name, password, and email address for your account. I should also point out that the Docker Hub service is free for public docker images. Once you have created your account, you can push the image that you have previously created, to make it available for others to use.

To do so, you will need the ID and the TAG of your node-postgresql image.

Run again the docker images command and note the ID and the TAG of your Docker image e.g. 602d96f6a0d8.

Now, with the following command, we will prepare our Docker Image for its journey to the outside world (the accountname part of the command is your account name on the Docker Hube profile page):

docker tag 602d96f6a0d8 <your-docker-account>/node-postgresql:latest

Run the docker images command and verify your newly tagged image.

Next, use the docker login command to log into the Docker Hub from the command line.

Login to your Docker account

The format for the login command is:

docker login --username=yourhubusername 

When prompted, enter your password and press enter.

Push the image back to the DockerHub

Now you can push your image to the newly created repository:

docker push <your-docker-account>/node-postgresql

The above command can take a while to complete depending on your connection's upload bandwidth as it uploads quite a bit of data. Once it has completed, you can go to your profile on Docker Hub and check out your new image.

Downloading your Custom Image

If you want to pull your image from your Docker Hub repository you will need to first delete the original image from your local machine because Docker would refuse to pull from the hub as the local and the remote images are identical.

As you remember from the previous part, to remove a docker image, you must run the docker rmi command. You can use an ID or the name to remove an image:

docker rmi -f 602d96f6a0d8

Now that the image is deleted you can pull and load the image from your repository using the docker run command by including your account name from Docker Hub.

docker run <your-docker-account>/node-postgresql

Since we previously deleted the image and it was no longer available on our local system, Docker will download it and store it in the designated location.

Summary

You've learned how to create your own Docker file based from someone elses repository. We wrote some basic instructions and then builded an image using it as a recipe. We also covered the basics of pushing our new custom image to the Docker Hub registry and then pulling it to our local machine.

I hope this was insightful to you and that it would encourage you to create many docker containers yourself.

Docker Pull Command
Owner
chillnet