Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Docker Docker containers w/ MongoDB MongoDB

Go 1.26.4 MongoDB 7.0 Docker No plaintext secrets

A small Go web service that writes a timestamp into MongoDB every time you hit /, and reads them all back on /logs. The point of this repository is not the app: it is packaging it properly with containers, on a private network, with persistent data and no secret in the git history.

Full brief in docs/CONSIGNES.md.

Architecture

Architecture: client to app over a published port, app to MongoDB over a private bridge network, MongoDB to a named volume

Two containers on one user defined bridge network. Only the app publishes a port to the host. MongoDB stays unreachable from outside the network, and its data lives in a named volume so it survives a rebuild or a reboot.

Endpoints

Method Path What it does
GET / Inserts { created_at } in MongoDB, returns the new document id
GET /logs Returns every document in the collection
GET /healthz Returns {"status":"healthy"}, used as the container healthcheck

Configuration

Every setting comes from the environment. The app refuses to start if one is missing, which is intentional: a container that boots half configured is worse than one that fails loudly.

Variable Required Notes
MONGODB_HOST yes Service name on the docker network, not localhost
MONGODB_PORT yes 27017
MONGODB_USER yes
MONGODB_PASSWORD one of the two Plain value, handy for local go run
MONGODB_PASSWORD_FILE one of the two Path to a file holding the password, this is the container path
PORT no HTTP listen port, defaults to 8080

MONGODB_PASSWORD_FILE is the one that matters here. It lets the password arrive as a mounted file instead of an environment variable, so it never shows up in docker inspect, in the image layers, or in the repository.

Getting started

Requirements: Docker with the Compose plugin, and Go 1.26+ only if you want to run the app outside a container.

git clone git@gitlab.com:WhiteMuush/simplon-containers-tp.git
cd simplon-containers-tp
cp .env.example .env          # then edit the credentials in .env

Boot the whole stack (app + MongoDB) with one command:

docker compose up -d          # start in the background
docker compose logs -f api    # follow the app logs
docker compose down           # stop, data survives in the mongo-data volume

Then check it answers:

curl localhost:8080/healthz   # {"status":"healthy"}
curl localhost:8080/          # inserts one document
curl localhost:8080/logs      # reads them all back

Updating

After changing the Go code, rebuild and redeploy the API only. Compose keeps MongoDB and its volume untouched, so no data is lost:

docker compose up -d --build api

Build the image by hand (without starting anything), for example to tag a release:

docker build -t sct-api:dev .
# or pin a version, which is what compose reads from APP_VERSION
APP_VERSION=1.0.0 docker compose build api

Running outside a container

Only needs Go 1.26+. Point it at a throwaway MongoDB:

docker run -d --name mongo-dev -p 27017:27017 \
  -e MONGO_INITDB_ROOT_USERNAME=app \
  -e MONGO_INITDB_ROOT_PASSWORD=devpassword \
  mongo:7.0

MONGODB_HOST=localhost MONGODB_PORT=27017 \
MONGODB_USER=app MONGODB_PASSWORD=devpassword \
go run .

Project layout

.
├── main.go            # wiring: env, mongo client, routes
├── handlers.go        # the three HTTP handlers
├── docs/
│   ├── CONSIGNES.md   # the brief
│   └── assets/        # diagrams
└── .gitignore         # keeps .env and secrets out of git

Notes

main is a protected branch: pushing to it directly is disabled for everyone, so work happens on a branch and lands through a merge request.

About

TP Simplon ~ (Mirror of GitLab) Built around a small Go web service, packaged with a multi-stage Dockerfile and Docker Compose for reproducible local runs. It exposes a /healthz endpoint wired to the container's, healthcheck, demonstrating containerization best practices.

Topics

Resources

Stars

Watchers

Forks

Contributors

Languages