Docker Container Operations: Creating, Starting, Stopping, and Managing Containers
This guide explains Docker container fundamentals and provides step‑by‑step instructions for creating, launching (both new and stopped containers), running containers in the background, attaching or executing commands inside them, viewing logs, exporting/importing snapshots, and cleaning up containers using Docker CLI commands.
Docker containers are lightweight, isolated environments that run applications and their runtime dependencies, distinct from full virtual machines. Managing containers involves creating, starting, stopping, and removing them using Docker commands.
Starting Containers
To launch a new container, use docker run with the desired image, for example:
$ docker run ubuntu:14.04 /bin/echo 'Hello world'To start an existing stopped container, run docker container start <container_name>.
Background Execution
Running a container in the background (detached mode) is achieved with the -d flag:
$ docker run -d ubuntu:17.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"Output from a detached container is not shown on the host terminal; it can be retrieved later with docker container logs <id_or_name>.
Interacting with Running Containers
The docker attach command re‑attaches to a container’s primary process, but exiting the attached shell will stop the container. Instead, the recommended approach is docker exec, which runs a new command inside a running container without affecting its lifecycle. Example:
$ docker exec -it 1f1b0 bash
root@1f1b0:/# ps
root@1f1b0:/# exitViewing Logs
Container output can be inspected with:
$ docker container logs <container_id_or_name>Exporting and Importing Containers
Export a container’s filesystem to a tar archive:
$ docker export <container_id> > ubuntu.tarImport the tar file as a new image:
$ cat ubuntu.tar | docker import - test/ubuntu:latestRemoving Containers
Delete a stopped container with docker container rm <name>. To force‑remove a running container, add -f. All stopped containers can be cleaned up using: $ docker container prune The guide concludes with references and author contact information.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
