๐ณ 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 CGOmusl-dev
provides C stdlib headers used by Gosqlite-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
, andCMD
all run from/app
Internally: Docker will
cd /app
before executing next instructionsFiles 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 changeInternally: 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 layerCaches 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 linkingWithout 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 containerTarget 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 defaultYou 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 Dockerfilecontainer_name
โ Names the containerimdb-cli
volumes
โ Mounts./data
folder to/app/data
to persist SQLite DBentrypoint
โ 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.
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: