Cloud Native 6 min read

Master Docker: Essential Commands for Managing Images and Containers

This tutorial walks you through essential Docker commands, covering how to list images, search and pull images, run containers with various options, inspect logs, stop, remove, restart containers, and export or import container snapshots, all illustrated with clear examples and screenshots.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Docker: Essential Commands for Managing Images and Containers

Exploring More Docker Operations

1. List available images

# docker images

2. Search a public image

# docker search imageName

3. Pull an image

# docker pull imageName

4. Run a Docker container

Basic command: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] Main options:

-d : run container in background

-t : allocate a pseudo‑TTY

-i : keep STDIN open (usually used with -t)

-v : bind‑mount a volume, e.g., -v /data/www:/var/www/html

-p : map container port to host, e.g., -p 8080:80

Examples

Start an interactive container: # docker -it centos:6.6 /bin/bash Start a container in background and map a port: # docker -d -p 4422:22 sshd Bind a local directory and map a port:

# docker run -d -v /data/www:/var/www/html -p 8080:80 httpd

5. List Docker containers

Show running containers: # docker ps Show all containers, including stopped ones:

# docker ps -a

6. View container logs

# docker logs "Container Name or Container ID"

Example for an nginx container:

# docker logs 034b9dc0346c
[log output omitted for brevity]

7. Stop a Docker container

Gracefully stop a running container: # docker stop "Container Name or Container ID" Force‑kill a container:

# docker kill "Container Name or Container ID"

8. Remove a Docker container

Delete a stopped container: # docker rm "Container Name or Container ID" Remove all stopped containers at once:

# docker rm `docker ps -a -q`

9. Restart a Docker container

# docker restart "Container Name or Container ID"

10. Access a container's interactive terminal

Method 1 – attach to a running container:

# docker attach "Container Name or Container ID"

Method 2 – enter the container’s namespace:

docker_ID=$(docker inspect -f {{.State.Pid}} Container_Name_or_ID)
nsenter --target $docker_ID --mount --uts --ipc --net --pid

11. Export and import containers

Export a container to a tar file: # docker export 034b9dc0346c > nginx.tar This creates a snapshot of the container on the local filesystem.

12. One‑page overview of main Docker operations

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Dockercloud-nativeTutorialcommand-line
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.