getting_started_with_docker

Home

Getting Started with Docker

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).
docker run -it --rm --name my-demo xxradar/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.

docker search ubuntu

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.

  1. 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.)

  2. 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):

  3. 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.)

  4. 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.)

6.1 Fetching Image Layer Blobsums (Example: ubuntu:latest)

  1. Get an authentication token:

    (Removed --header 'GET' as it's default for curl. Added echo for token.)

  2. Get the image manifest and extract layer blobsums:

6.2 Fetching and Extracting an Image Layer (Example: xxradar/hackon)

  1. Get token for xxradar/hackon:

  2. Get manifest and a specific blobsum: (Assuming you pick one blobsum from the output of a similar manifest fetch for xxradar/hackon)

  3. Download the layer blob: The layer is a gzipped tarball.

    (Changed output filename to be more descriptive, e.g., 5667fd...db69ab.tar.gz)

  4. Analyze the layer: Create a separate directory to untar and inspect the layer's contents.

    (Corrected zxfv and used variable for filename.)

6.3 Alternative: Using docker image save

A simpler way to inspect all layers of an image locally is to save it as a tarball and then extract it. This tarball contains all layers and metadata.

  1. Pull the image (if not already local):

  2. Save the image to a tarball:

  3. Extract the main tarball and inspect layers: In a new, separate directory:

    (Changed output to hackon_image.tar and tar -xf. Added example for extracting sub-tarballs.)

Home

Last updated