Setup a clean workspace for Neo4j and Golang

Having a clean and productive workflow is essential for every developer, Docker and docker-compose are just the right tools for keeping it clean and avoid installing anything locally.

For the last 15 years I had to work on multiple projects for different clients at the same time, having to install locally different Mysql versions, PHP versions, Java, Nodejs, you name it, obviously this became a real nightmare as the projects kept rolling in. At one point I tried to create a VM for each project using the old way, but I ended up consuming my HD space really fast so it wasn't a long lasting solution.

Adding to the mess of different versions of a particular db, dev lang, etc. If I need to setup an environment for a project, let's say Mongo, Neo4j, Go, Reactjs, I would have to install everything locally, the nightmare continued.

Here is where Docker and docker-compose comes to save the day.

Docker is a great tool for working with containers, isolating the app, db, etc. It enables painless installation of services and sharing development environments with others without the hassle of getting the same version of everything and installing the tools locally.

Lets see with a small example how I work with these two amazing tools to setup a working environment that I can easily share with other developers.

To follow along, you must have installed docker and docker-compose.

In this example will work with Neo4j and Go.

Get started

Create a project folder, lets call it docker-go-neo4j, in the root of that directory create a docker-compose.yml file with the following

version: "3.2"
services:
  neo4j:
    image: neo4j:latest
    container_name: 'neo4j'
    volumes:
      - '$HOME/neo4j/data:/data'
      - '$HOME/neo4j/conf/:/conf/'
    ports:
      - 7474:7474
      - 7687:7687
    restart: on-failure
    networks: 
        - neo4j_go_net
    command: neo4j

networks:
  neo4j_go_net:
    driver: bridge

volumes:
  dgraph:

I like to create seperate networks for each environment that I setup, now save the file and run the following command from the root of your projects folder.

docker-compose up -d

This will download the image for Neo4j and starts the container in the background (remember this command, we will run it each time we add a new service to our docker-compose.yml file

Verify if the containers are running

docker-compose ps

You should see neo4j listed

TIP: You can verify that the database is working by heading to http://localhost:7474

Lets add another service inside the docker-compose.yml, this service will depend on the neo4j service we just created. Add the following bellow the neo4j service

backend:
    container_name: 'api-go'
    build: './backend'
    ports: 
      - '8080:8080'
    volumes: 
      - './backend:/go/src/app'
    depends_on: 
      - 'neo4j'
    networks: 
        - neo4j_go_net

The backend service will use a Dockerfile to setup go and copy the local files into the container, install the dependencies and setup hot reloading with the fresh package, so before running any other command, create a backend folder and inside of that folder create a file named Dockerfile without any extension.

FROM golang:latest

WORKDIR /go/src/app
COPY . .

RUN go get github.com/pilu/fresh
RUN go get -d -v ./...
RUN go install -v ./...

CMD [ "fresh" ]

what this file basically does is fetch the golang image from docker hub, setup a working directory, copy everything inside the backend folder into the working directory and install the needed packages, finally runs the fresh command for hot reloading.

Inside the backend directory you also need to create a main.go file and a public directory with an index.html file inside.

The complete source code can be viewed here docker-go-neo4j

The main.go and index.html files are log so I suggest you check them out in the repo, we are going to be working with the sample code from Neo4j, this way we know everything works.

The Idea behind this was not to create and application from scratch but for just illustrate how you can setup a development environment with Docker and docker-compose to work with Neo4j and Go.

Let's wrap it up

Hope this gives you an idea on how to setup your working enviroments and how easy is to share this with other so they can have the same environment on their machines. If you're new to Docker and the world of containers, give it a try, if not for production at least for development, this will change your life and will make your workflow much faster, avoid the nightmare of installing everything locally.

Checkout the complete sourcode on Github

enjoyed the article?Phanor Collcontact me and lets bring your ideas to life.

Get your copy of "Hands on Gatsbyjs" and learn how to supercharge your websites, embrace JAMStack and add microservices to implement authenticacion, create shopping carts and more. get your copy now

Phanor Coll

Phanor Coll

With more than 20 years of experience, I love to learn every day, hack around and bring crazy ideas to life. Currently immersed in the world of Go(golang), Graph databases(Neo4j, Dgraph) and Rectjs.

Learn more about me..
feedback phanorcoll.com
comments