creating_containers

Home

Creating Docker Images and Containers

This guide walks through the process of creating Docker images using a Dockerfile, tagging images, pushing them to a Docker registry, and running containers from these images.

1. Building a tcpdump Image

This example shows how to create a simple Docker image that includes the tcpdump utility.

1.1 Create Dockerfile

  1. Create a separate directory for your project:

    mkdir my-tcpdump-image
    cd my-tcpdump-image
  2. Create the Dockerfile: Create a file named Dockerfile (no extension) in this directory with the following content:

    FROM ubuntu
    RUN apt-get update && apt-get install -y tcpdump && rm -rf /var/lib/apt/lists/*
    # Default command. Can be overridden during 'docker run'.
    # Using -n to disable name resolution, -l for line-buffered output (good for pipes),
    # and -A to print ASCII. Adjust eth0 if needed.
    CMD ["tcpdump", "-i", "eth0", "-n", "-l", "-A"]

    (Added rm -rf /var/lib/apt/lists/* to reduce image size. Changed CMD to JSON format and added common flags.)

1.2 Build the Image

Now, build the Docker image from your Dockerfile.

(Corrected "Let's built" to "Let's build" implicitly by new heading.)

1.3 Verify Image Creation

List your local Docker images to see the newly created ones:

1.4 Alternative: Building from STDIN (Quick Way)

You can also build an image by piping a Dockerfile content directly to docker image build using a hyphen (-) as the path.

2. Pushing an Image to a Registry

To share your image or use it on other machines, you can push it to a Docker registry like Docker Hub.

  1. Log in to Docker Hub (or your private registry):

  2. Tag the image: Before pushing, you need to tag your image with the registry username and repository name. For example, to push tcpdump:v1 to Docker Hub under username xxradar:

  3. Push the image:

3. Running the tcpdump Container

Test the image you pushed (or the local one if you didn't push).

(Added --rm for auto-cleanup and a note about necessary capabilities for tcpdump.)

4. Building a "Hacking" Container (hackon:v1)

This example creates a more comprehensive image with various networking and utility tools.

4.1 Create Dockerfile

  1. Create a new directory and Dockerfile: (e.g., mkdir my-hackon-image; cd my-hackon-image) Create a Dockerfile with the following content:

    (Corrected "xxadar" to "xxradar". Replaced MAINTAINER with LABEL. Combined RUN apt-get install commands into one layer and added apt cache cleanup. Modified CMD to be more informative.)

4.2 Build and Push the Image

  1. Build the image:

  2. Push the image (Optional): If you tagged it with your username, you can push it:

    (Made build/push steps more explicit.)

4.3 Test Your New Container

  1. Run the container with its default CMD:

    This will output: "My hacking container is ready! Use 'docker run -it bash' for an interactive shell."

  2. Try to run a command (e.g., curl):

    What's happening? This command fails to execute curl as intended because curl 'http://www.brucon.org' is passed as arguments to the image's CMD (which is echo ...). The echo command simply prints these arguments.

  3. Correct way to run a command (override CMD or use bash): To run a specific command like curl, you provide it after the image name, which overrides the Dockerfile's CMD:

    Or, for an interactive shell:

    (Clarified "What's happening?" and the solution.)

4.4 More Ways to Interact with Containers ("Some tricks")

  1. List running containers:

  2. Get an interactive shell in a new container: The container runs, you get a shell, and it's removed when you exit.

  3. Run a container in the background and exec into it:

    (Used a different name hackon-bg to avoid conflict with previous examples if any were not cleaned up. Added -d for detached. Clarified cleanup.)

  4. Pipe input to a container:

    (Added --rm and - for cat to read from stdin.)

Home

Last updated