Containerizing My First Database App

  • Take a GO application from GIT and containerize it.
  • The app needs a MongoDB database to work, making sense to put this into a container.
  • Figure out a way for the two containers to communicate.
  • The end goal would be a functioning GO app that would allow GET and POST requests to receive/add data to the MongoDB database.
git clone https://github.com/ <insert GIT REPO> go/src/app
cd go/src/app
go build .
wget -qO — https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -sudo apt-get install gnupgwget -qO — https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -echo “deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.listsudo apt-get updatesudo apt-get install -y mongodb-org
export MONGODB_CONNECTION_STRING=mongodb://localhost/notes
./<insert app_name>
By default, the application exposes port 8000 to run and the database it is set to use is called ‘notes’. Since the app is running locally it is at http://localhost:8000/notes
Responses to queries are below the request panel
Example POST request
The expected output of the request — confirms the note was inserted into the DB and assigns a random ID to the request.
GET request after sending POST request to add a note.
FROM golangWORKDIR /go/src/appENV MONGODB_CONNECTION_STRING=mongodb://mongo/notesRUN apt-get update
RUN git clone https://github.com/<insert GIT REPO> /go/src/app
RUN go build .
CMD ["./<insert app name>"]
version: ‘3.3’
services:
goapp:
container_name: ‘goapp’
build: ‘./goapp’
ports:
— ‘8000:8000’
links:
— “mongo”
depends_on:
— mongo
environment:
— MONGODB_CONNECTION_STRING=mongodb://mongo/notes
mongo:
image: ‘mongo:latest’
container_name: ‘mongo’
ports:
— ‘27017:27017’
volumes:
— ‘mongo:/data/db’
volumes:
mongo:
sudo docker-compose buildsudo docker-compose up
Expected terminal output
docker containers ps -a 
docker logs <containerid>
Example of a few notes added with POST and retrieved with GET

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store