Part 1: Up and running with Kong and Docker Compose

This is part 1 of the series: Kong Up and Running.

Getting Kong Running

This tutorial assumed you've got the docker-tookbox installed.

Let's create a directory for Kong:

mkdir kong && cd kong  

now, create a docker-compose file like so:

docker-compose.yml

version: "2"

services:  
  kong-database:  
    restart: on-failure
    image: cassandra:2.2.5
    container_name: kong-database
    ports:
      - "9042:9042" 
    volumes:
      - "db-data:/var/lib/cassandra"
  web:  
    restart: on-failure
    image: mashape/kong:0.8.0
    container_name: kong
    ports:
      - "8000:8000"
      - "8443:8443"
      - "8001:8001"
      - "7946:7946"
      - "7946:7946/udp"
    links:
      - kong-database:kong-database
    environment:
    - DATABASE=cassandra
    security_opt:
      - label:seccomp:unconfined
  ui:  
    restart: on-failure
    image: pgbi/kong-dashboard
    ports:
      - "8080:8080"

volumes:  
  db-data:

What this does is essentially translate the instructions from Kong's own docker installation guide into a docker-compose file.

Notes:

  • I've also included a neat UI kindly provided by PGBI
  • As of Kong 0.8, You can now use Postgres as a backend. I've included a Gist at the end of this post which provides the full docker-compose files for both Cassandra and Postgres.

docker-compose takes care of:

  • exposing the appropriate ports
  • linking the appropriate images (in this case making sure that the kong image has access to the cassandra db
  • we use the official images provided by Mashape of Kong and Cassandra

Now, if we simply run:

docker-compose up  

We should have a working local installation of Kong.

Note: The start-up order of these containers is important. Kong will fail if the Cassandra container has not loaded up first.

If this is the case, you might need to just restart the Kong container. You can do this with the command: docker-compose restart web, or from the UI in Kitematic. You can keep tabs on the status's of your containers with the command docker ps.

Seeing the results:

If you are running on a non-linux box, you will not be able to access your Kong installation via localhost. You need to find the IP of the docker-machine VM on which this is running.

You can get the IP address of your docker machine with:

docker-machine ip default  

(assuming you're using the default machine .. which you probably are)

If everything went to plan, you should now have the following services available:

If our docker-machine ip is: 1.2.3.4

Next up, we'll configure our API using Ansible

Gist of final docker-compose files (choose either Postgres or Cassandra)