Around Go: containers · 3 MIN READ

Docker daily commands: build, run, inspect and stop

Run a named local container and diagnose it with logs, inspect and stats.

Start with a trusted project containing the Dockerfile from the earlier lesson and a Go service listening on :8080. The following commands use an isolated development container named go-api-dev; an image, a container instance and a published port are different objects.

01

Build once and start a named instance

build tags the image produced from the current context. run creates a container; -d runs it in the background and -p publishes a container port at a host address. Bind the host side to loopback when the exercise should stay local.

bash
docker build -t go-api:dev .
docker run -d --name go-api-dev \
  -p 127.0.0.1:8080:8080 -e APP_ENV=development go-api:dev
docker ps --filter name=go-api-dev
curl --max-time 5 --fail-with-body http://127.0.0.1:8080/healthz
Read the code

Run only after the build succeeds and choose another name or port if already used. The Go listener must accept container-interface traffic, usually :8080. EXPOSE in the Dockerfile alone does not publish anything on the host.

02

Inspect the instance and its actual runtime

logs reads stdout/stderr, inspect can select one property, and stats reports current resource usage. docker exec starts a new command inside the container; a scratch image has no sh, ls or other debugging utility unless you explicitly supplied one.

bash
docker logs --tail 50 go-api-dev
docker inspect --format '{{.State.Status}} {{.State.ExitCode}}' go-api-dev
docker stats --no-stream go-api-dev
Read the code

A stopped container is omitted by ordinary ps; use docker ps -a when investigating an exit. ExitCode is not an HTTP status. Avoid dumping all inspect properties when only state is needed because configuration can include sensitive environment data.

03

Stop and remove only this instance

stop sends a graceful termination signal and allows a finite timeout before forced termination. rm removes the stopped container, not the image or unrelated volumes. Replacing an image tag does not upgrade a previously created container.

bash
docker stop --timeout 15 go-api-dev
docker ps -a --filter name=go-api-dev
docker rm go-api-dev
docker image inspect go-api:dev --format '{{.Id}}'
Read the code

Confirm you no longer need the disposable instance before removing it. Persistent state belongs in deliberate storage; removing the container should not be your only backup strategy. Keep the image if you want to reproduce its startup.

TAKE THIS WITH YOU

Build an artifact, create a named instance and verify it through the published port. Inspect its own logs and exit state, then stop it deliberately; image tags and container instances have separate lifecycles.

Check your understanding

Does EXPOSE 8080 alone make a container port reachable on the host?

Go to the source

Progress stays in this browser. No account needed.