This guide provides a hands-on introduction to fundamental Docker commands, covering running containers, managing images, the container lifecycle, and some advanced interactions with Docker Hub.
(Assumption: Examples may use custom images like xxradar/hackon. You can substitute with standard images like ubuntu or alpine for many commands, but functionality might differ if specific tools from the custom image are expected.)
1. Running Your First Container (Quick Way)
The docker run command is a quick way to download an image (if not present locally) and start a container from it.
# This command starts a container from the 'xxradar/hackon' image,# names it 'my-demo', runs it interactively (-it), and removes it# automatically when it exits (--rm).dockerrun-it--rm--namemy-demoxxradar/hackon
2. Understanding Docker Images and Containers
2.1 Searching for Images on Docker Hub
Docker Hub is a public registry of Docker images. You can search for images using docker search.
dockersearchubuntu
2.2 Finding Image Tags (Versions)
Images often have different versions, identified by tags. You can query the Docker Hub API to list tags for an image.
Using curl and jq (a JSON processor) to list tags for the official ubuntu image:
Example for a user-specific image (xxradar/fqdnsan_scan):
(Removed -k as it's typically not needed for Docker Hub.)
2.3 Pulling Images
Download images from a registry to your local machine using docker image pull.
List your local images:
3. Managing Container Lifecycle
Let's explore the different states of a container.
3.1 Creating a Container
The docker container create command creates a writable container layer from an image but does not start it.
List all containers (including stopped ones) to see my-ubuntu:
3.2 Starting a Container
Start a previously created container.
Check its status:
3.3 Attaching to a Container
If a container is running interactively (e.g., started with -i and has an interactive process like a shell), you can attach your terminal to its input/output.
After exiting, check the status:
Detaching: To detach from an attached container without stopping it, use the escape sequence Ctrl+P followed by Ctrl+Q.
3.4 Stopping a Container
If a container is running (e.g., in detached mode or attached in another terminal), you can stop it:
3.5 Removing a Container
Once a container is stopped, you can remove it. You cannot remove a running container without forcing it (-f).
Verify removal:
4. Managing Images
4.1 Removing an Image
You can remove an image if no containers (even stopped ones) are using it.
5. Cleaning Up Docker Resources
Here are commands to clean up multiple Docker resources. Use with caution, especially prune commands, as they can remove data.
Stop all running containers:
(Note: docker ps -a -q lists all containers, docker ps -q lists only running ones. For stopping, docker ps -q is correct.)
Remove all stopped containers:
(Clarified: this removes containers that have exited. To remove all containers (running ones must be stopped first or use docker rm -f), you'd use docker rm $(docker ps -a -q) after stopping them.)
Alternatively, to remove all containers (running or stopped, forcing removal for running ones):
Remove all images:(Caution: This will remove all images not currently used by any container. If you want to remove ALL images, including those used by stopped containers, you might need to remove those containers first.)
System Prune (The "hard way"):
This command removes all stopped containers, all networks not used by at least one container, all dangling images, and all build cache.
To also remove all unused images (not just dangling) and all volumes not used by at least one container:
(Added --volumes as an option for even more aggressive cleanup.)
6. Advanced: Interacting with Docker Hub API
This section demonstrates how to use curl and jq to interact with the Docker Hub registry API, for example, to get image manifest details and download layers.
(This is an advanced topic, primarily for understanding Docker's internals.)
# The -k flag might be needed if there are SSL certificate issues with a specific custom domain/registry,
# but generally not required for registry.hub.docker.com.
curl 'https://registry.hub.docker.com/v2/repositories/xxradar/fqdnsan_scan/tags/' | jq '."results"[]["name"]'
# Pull the 'latest' tag (default if no tag is specified)
docker image pull ubuntu
# Pull a specific version, e.g., 18.04
docker image pull ubuntu:18.04
# Replace 'e4f3ea7d1827' with the actual ID of 'my-ubuntu' from 'docker container ls -a', or use the name.
# The -i flag (interactive) is useful if you want to attach to it later.
docker container start my-ubuntu # or docker container start -i my-ubuntu
# docker container start e4f3ea7d1827
docker container ls -a # 'my-ubuntu' should now show as 'Up'
docker container attach my-ubuntu
# If this was a shell, you could type commands.
# To exit the attached shell and stop the container, you might type 'exit'.
# exit
docker container ls -a # The container might be stopped if its main process ended.
docker container stop my-ubuntu
docker container rm my-ubuntu
docker container ls -a
docker image ls
# Attempt to remove the 'ubuntu' image (latest tag)
docker image rm ubuntu
# If it fails, it might be because 'my-ubuntu' (if not removed) or other containers depend on it.
# Or if 'ubuntu:18.04' is still referenced by a container.
docker image ls
# docker container stop $(docker ps -q)
docker container rm $(docker ps -a -f status=exited -q)
# docker container stop $(docker ps -q) && docker container rm $(docker ps -a -q)
# Or, more directly to remove all stopped containers:
docker container prune -f
# docker image rm $(docker image ls -a -q) # Corrected 'list' to 'ls'
# A safer way to remove dangling images (those not tagged and not referenced by any container):
docker image prune -f
# To remove all unused images (not just dangling):
docker image prune -a -f
# First, get all blobsums for xxradar/hackon:latest
# curl --silent --request GET --header "Authorization: Bearer ${TOKEN}" \
# 'https://registry-1.docker.io/v2/xxradar/hackon/manifests/latest' \
# | jq -r '.fsLayers[].blobSum'
# Let's say one of the blobsums obtained is:
export BLOBSUM="sha256:5667fdb72017d1fb364744ca1abf7b6f3bbe9c98c3786f294a461c2866db69ab" # Example from original
mkdir analyse_layer
mv "${BLOBSUM#sha256:}.tar.gz" ./analyse_layer/
cd analyse_layer
tar -zxvf "${BLOBSUM#sha256:}.tar.gz"
# Now explore the extracted files and directories
cd ..
docker image pull xxradar/hackon
docker image save xxradar/hackon -o hackon_image.tar
mkdir analyse_saved_image
mv hackon_image.tar ./analyse_saved_image/
cd analyse_saved_image
tar -xf hackon_image.tar
# You will find a manifest.json file listing layers, and subdirectories for each layer (often named by their ID or as .tar files).
# Example: find . -name '*.tar' | xargs -I {} sh -c 'mkdir -p layer_{}; tar -xf {} -C layer_{}'
cd ..