Docker lab/version note: use supported Docker and Node.js tools and patched images. The JavaScript examples have local tests; Docker, Nginx and hosted CI steps were not tested live.
What this guide covers
A Docker image is the template used to create a container. A container is one instance of that image, with its own process and writable files. Stopping a container does not remove it.
Before you start
Use Docker with Linux containers. These commands create only named test containers and use an Alpine image. Check docker context show so you know which Docker host receives the commands.
Step 1: Create and start a container
Create a container that prints a message and exits:
docker pull alpine:3.23
docker image inspect alpine:3.23 --format '{{.Id}} {{.Os}}/{{.Architecture}}'
docker create --name demo-task \
--label example=tutorial \
alpine:3.23 sh -ec 'printf "Hello from Docker\n"; date -u'
docker ps -a --filter name=demo-task
docker start --attach demo-task
docker inspect demo-task --format 'status={{.State.Status}} exit={{.State.ExitCode}}'
docker create prepares it without running it. docker start --attach runs it and shows its output. An exited container still appears in docker ps -a.
Step 2: Try another command
Inspect the image’s default command and run a replacement command in a new container:
docker image inspect alpine:3.23 --format 'entrypoint={{json .Config.Entrypoint}} cmd={{json .Config.Cmd}}'
docker run --rm alpine:3.23 printf 'new container, new command\n'
docker logs --timestamps demo-task
docker start --attach demo-task
Restarting the old container uses its original command. It does not create a new container.
Step 3: Check what happens to files
This creates a file, restarts the same container, then removes it:
docker create --name demo-files alpine:3.23 \
sh -ec 'printf "local marker\n" >> /tmp/example-file; cat /tmp/example-file'
docker start --attach demo-files
docker start --attach demo-files
docker diff demo-files
docker rm demo-files
docker run --rm alpine:3.23 sh -ec 'test ! -e /tmp/example-file'
The file survives a restart of that container. After removal, a new container from the image does not have it. Use a volume for data that must survive container replacement.
Step 4: Stop a running process
Run a process that responds to a stop request:
docker run -d --name demo-stop alpine:3.23 \
sh -c 'trap "echo shutdown-requested; exit 0" TERM; while :; do sleep 1 & wait $!; done'
docker stop --time 5 demo-stop
docker logs demo-stop
docker inspect demo-stop --format '{{.State.ExitCode}}'
Docker first asks the main process to stop. If it does not finish within the timeout, Docker forces it to stop. In your application, handle shutdown and finish important work within the allowed time.
Check the result
Remove the remaining test containers with docker rm demo-task demo-stop. Use docker ps -a to confirm they are gone. Do not use a system-wide prune command to clean up a small exercise.
Conversation
0 commentsNo comments yet. Start the conversation below.