creating_containers
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
tcpdump ImageThis example shows how to create a simple Docker image that includes the tcpdump utility.
1.1 Create Dockerfile
Create a separate directory for your project:
mkdir my-tcpdump-image cd my-tcpdump-imageCreate the
Dockerfile: Create a file namedDockerfile(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.
Log in to Docker Hub (or your private registry):
Tag the image: Before pushing, you need to tag your image with the registry username and repository name. For example, to push
tcpdump:v1to Docker Hub under usernamexxradar:Push the image:
3. Running the tcpdump Container
tcpdump ContainerTest 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)
hackon:v1)This example creates a more comprehensive image with various networking and utility tools.
4.1 Create Dockerfile
Create a new directory and
Dockerfile: (e.g.,mkdir my-hackon-image; cd my-hackon-image) Create aDockerfilewith the following content:(Corrected "xxadar" to "xxradar". Replaced
MAINTAINERwithLABEL. CombinedRUN apt-get installcommands into one layer and added apt cache cleanup. Modified CMD to be more informative.)
4.2 Build and Push the Image
Build the image:
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
Run the container with its default CMD:
This will output: "My hacking container is ready! Use 'docker run -it bash' for an interactive shell."
Try to run a command (e.g.,
curl):What's happening? This command fails to execute
curlas intended becausecurl 'http://www.brucon.org'is passed as arguments to the image'sCMD(which isecho ...). Theechocommand simply prints these arguments.Correct way to run a command (override CMD or use
bash): To run a specific command likecurl, you provide it after the image name, which overrides theDockerfile'sCMD:Or, for an interactive shell:
(Clarified "What's happening?" and the solution.)
4.4 More Ways to Interact with Containers ("Some tricks")
List running containers:
Get an interactive shell in a new container: The container runs, you get a shell, and it's removed when you
exit.Run a container in the background and
execinto it:(Used a different name
hackon-bgto avoid conflict with previous examples if any were not cleaned up. Added-dfor detached. Clarified cleanup.)Pipe input to a container:
(Added
--rmand-forcatto read from stdin.)
Last updated