๐Ÿณ Dockerizing My Go CLI App with SQLite Support

๐Ÿ“ฆ About mycli

mycli is a lightweight command-line app built with Cobra in Go. It allows you to:

  • Add, list, or search records

  • Use SQLite as a local DB (via gorm)

  • Run everything inside Docker โ€” no need for Go or SQLite installed on your system

โš™๏ธ Dockerfile

FROM golang:alpine

RUN apk add --no-cache gcc musl-dev sqlite-dev

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY . .

ENV CGO_ENABLED=1

RUN go build -o imdb .

CMD ["./imdb"]

#FROM golang:alpine

  • Use the official Go compiler based on the Alpine Linux distro

  • Why: Small, fast, and perfect for lightweight CLI builds

  • Internally: Docker pulls a tiny Linux image with Go pre-installed

#RUN apk add --no-cache gcc musl-dev sqlite-dev

  • Install system packages needed for SQLite + CGO builds

  • Why:

    • gcc is needed to compile C code via CGO

    • musl-dev provides C stdlib headers used by Go

    • sqlite-dev gives C headers + libs for SQLite
      ๐Ÿ”ง Internally: Adds these packages into the image during the build stage
      ๐Ÿšซ --no-cache avoids caching indexes to keep the image small

#WORKDIR /app

  • Set working directory inside the container

  • Why: So future COPY, RUN, and CMD all run from /app

  • Internally: Docker will cd /app before executing next instructions

  • Files copied with COPY . . will now go into /app

#COPY go.mod go.sum ./

  • Copy module files first to leverage Docker caching

  • Why: Keeps go mod download cached unless dependencies change

  • Internally: Docker copies these files into /app

  • Speeds up builds by avoiding redundant downloads

#RUN go mod download

  • Download Go dependencies (from go.mod)

  • Why: Prepares all packages so the final build works offline

  • Internally: Runs go mod download in /app inside the build layer

  • Caches module dependencies (e.g., gorm, cobra)

#COPY . .

  • Copy all remaining project files (main.go, commands, etc)

  • Why: Add actual source code to be compiled

  • Internally: Everything in your project root is copied into /app

#ENV CGO_ENABLED=1

  • Enable CGO (Go to C interop)

  • Why: Needed to use C-based SQLite library (sqlite3)

  • Internally: Go uses this env var during go build to allow C linking

  • Without this, build will fail with undefined: _sqlite3_open

#RUN go build -o imdb .

  • Build your Go CLI binary

  • Why: Compiles Go code into imdb binary at /app/imdb

  • Internally: Runs the go build command inside container

  • Target output: /app/imdb

#CMD ["./imdb"]

  • Set the default command to run when the container starts

  • Why: Makes it behave like a CLI โ€” can pass list, search etc.

  • Internally: Docker executes /app/imdb by default

  • You can override it by: docker run imdb list โ†’ becomes ./imdb list


INTERNAL WORKING

RUN go build -o imdb .

CMD ["./imdb"]

Running: docker run main list

Docker internally runs: ./imdb list


โš™๏ธ Docker Compose

version: "3.8"

services:
  imdb:
    build: .
    container_name: imdb-cli
    volumes:
      - ./data:/app/data
    entrypoint: ./imdb

# Breakdown

  • build: . โ†’ Builds from the current Dockerfile

  • container_name โ†’ Names the container imdb-cli

  • volumes โ†’ Mounts ./data folder to /app/data to persist SQLite DB

  • entrypoint โ†’ Runs ./imdb as the CLI binary inside the container

๐Ÿ’พ What Does volumes Do?

volumes:
  - ./data:/app/data
  • ./data โ†’ Local folder (host)

  • /app/data โ†’ Inside container
    โœ… This keeps your SQLite DB persistent even after stopping the container.

  • โœ… Why: Persists your SQLite database even after the container stops

  • ๐Ÿ“‚ Your DB file (e.g. todos.db) will stay saved in ./data


๐Ÿงช Usage

Running :-

docker-compose run imdb list
docker-compose run imdb search Inception

Docker internally runs:-

./imdb list
./imdb search Inception

#inside the Docker container, with persistent data saved to ./data.


๐Ÿš€ Run Your CLI with Docker Compose

# Build the image and run the container interactively
docker-compose run imdb

# Run a specific command (e.g. list movies)
docker-compose run imdb list

# Run with arguments (e.g. search a movie)
docker-compose run imdb search Inception

# Clean up (stop and remove containers)
docker-compose down

Thanks for reading! If you found this helpful or have suggestions, feel free to connect or drop a comment.

0
Subscribe to my newsletter

Read articles from Sidharth chauhan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sidharth chauhan
Sidharth chauhan

๐ŸŒŸ Hello! I'm Sidharth Chauhan, a passionate DevOps Engineer dedicated to automating and optimizing processes to enhance software development and deployment. With expertise in Docker, Kubernetes, CI/CD, AWS, and more, I thrive in environments that leverage cutting-edge technologies and tools.I love sharing my knowledge and experiences through blogging, and I'm always eager to connect with like-minded professionals. Whether you're looking to collaborate on a project, need help with DevOps, or just want to chat about the latest tech trends, feel free to reach out!๐Ÿ”— Connect with Me: