Detached mode starts a container in the background and returns control of the terminal instead of attaching the session to the container. section 2.4 section 19.1
Use detached mode when a container should keep running as a service:
- Add --detach, or -d, to run a container in the background. section 2.4
- Add --publish when the background container must receive traffic through a host port. section 2.4
- Check that the container is running with docker container ls. section 2.4
- Read output with docker logs, including after stopping the container. section 19.1
Run a web container in detached mode
Run docker container run --detach --publish 8088:80 diamol/ch02-hello-diamol-web:2e. Docker returns a container ID, keeps the web server running, and maps host port 8088 to container port 80. Verify it with docker container ls and look for an Up status. section 2.4
docker container run --detach --publish 8088:80 ` diamol/ch02-hello-diamol-web:2e
Shows the complete detached-mode command with port publishing. section 2.4
# run a detached container:
docker run -d --name timecheck diamol/ch19-timecheck:2e
# check the most recent log entry:
docker logs --tail 1 timecheck
# stop the container and check the logs again:
docker stop timecheck
docker logs --tail 1 timecheck
# check where Docker stores the container log file:
docker container inspect --format='{{.LogPath}}' timecheck
Shows detached execution followed by log inspection, stopping, and log-path inspection. section 19.1
