Docker Run Go Binary
Fast recall
Quick revision
Core answer
Whether you understand Docker Run Go Binary beyond memorized syntax.
Giving a definition without explaining a practical trade-off.
Connect the concept to a small production example and state its complexity or failure mode.
Interview-ready guide
Interview answer
The commands:
# Build the image from the Dockerfile
docker build -t myapp .
# Run the container
docker run -d -p 8080:8080 --name myapp-1 myapp
# -d: detached
# -p 8080:8080: host:container port mapping
# --name: the handle for logs/stops
# Verify
docker ps # the container is running
curl localhost:8080/health # the app responds
docker logs myapp-1 # the app's stdout
docker stop myapp-1 # graceful stop
docker rm myapp-1 # removeThe binary's relationship to the container: the container IS the process (plus the isolation layers). The Go binary (CMD's target) runs as PID 1 inside the container — the container lives as long as the binary runs. When the process exits, the container exits — the lifecycle is one-to-one.
The port semantics: EXPOSE 8080 in the Dockerfile is DOCUMENTATION (the metadata that says 'this listens on 8080') — it does NOT make the port accessible. The -p 8080:8080 at run time is the actual mapping (the host port → the container port). Without -p, the app is unreachable from outside the container.
The Go-specific signals: a graceful shutdown needs the binary to handle SIGTERM (Go's os/signal) — the container stop sends SIGTERM, waits the grace period, then SIGKILL. Go's signal.NotifyContext integrates with this directly.
Docker Run Go Binary
Direct Answer
Build the image (docker build -t myapp .), run the container (docker run -p 8080:8080 myapp). The EXPOSE documents the port; -p maps it. The container runs the CMD's binary with the process isolated.
Deep Dive & Architecture
PID 1 and the signal problem (the Go-specific production detail): the Go binary runs as PID 1 in the container — PID 1 has special signal semantics (the kernel doesn't apply default handlers). Go's runtime handles SIGTERM forwarding correctly, but a naive binary that doesn't handle signals won't receive the TERM — docker stop times out and SIGKILLs (no graceful shutdown). The fix: signal handling in main (signal.NotifyContext) or an init wrapper (tini/dumb-init).
The docker compose pattern (the next step up): one YAML describing the multi-container local environment — the app + the DB + the cache — with networking, env vars, and volumes:
version: "3.9"
services:
app:
build: .
ports: ["8080:8080"]
environment:
DATABASE_URL: postgres://db:5432/app
depends_on: [db]
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: secret
docker compose up — the whole stack; compose down — gone. The local-dev environment that mirrors production's topology.
Common Mistakes & Pitfalls
Giving a definition without explaining a practical trade-off.
Check your understanding
Answer these without looking back at the article.
- When would you use Docker Run Go Binary in a real project?
- How would you compare Docker Run Go Binary with an alternative?
- What is a common mistake when using Docker Run Go Binary?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.