Date:

Docker Exercises: Part 1

Table of Contents

Questions

  1. Create a Dockerfile that installs curl and runs curl google.com.
  2. Create a Dockerfile that starts a web server on port 8080 and serves a static HTML file with the message Hello, Docker!
  3. Write a simple web server in Python that gets a name from the query string and returns Hello, !. Create a Dockerfile that runs this web server on port 8080. For this exercise, you can use the python:latest image.
  4. After creating the Python web server, check the image for vulnerabilities using the docker scout cves command. How many vulnerabilities are there? How can you fix them?
  5. Write a Python script that reads a file called data.txt and prints its content. Create a Dockerfile that copies the data.txt file to the image and runs the Python script. For this exercise, you can use the python:latest image.
  6. Change the data.txt file from the previous exercise and rebuild the image. Does the Python script print the new content?
  7. Change the data.txt file from the previous exercise and run the image again without rebuilding it. Does the Python script print the new content?
  8. Run the Docker image from the previous exercise with a volume mounted to the /data directory. Change the data.txt file on the host machine and see if the Python script prints the new content.

Exercise 1: Install Raw curl and Run a Simple Command

Create a Dockerfile that installs curl and runs curl google.com.


    # Use an official lightweight image
    FROM alpine:latest

    # Install curl
    RUN apk add --no-cache curl

    # Run curl google.com
    CMD ["curl", "google.com"]
  

Build and run the container:

docker build -t curl-test .
docker run --rm curl-test

You should see the HTML response from Google.

Exercise 2: Serve a Static HTML Page

Create a Dockerfile that starts a web server on port 8080 and serves a static HTML file with the message Hello, Docker!


    # Use an official Nginx image
    FROM nginx:latest

    # Copy custom index.html
    COPY index.html /usr/share/nginx/html/index.html

    # Expose port 8080
    EXPOSE 8080
  

Build and run:

docker build -t file-reader .
docker run file-reader

The new content will be printed.

Exercise 6: Modify data.txt and Rebuild

Modify data.txt and rebuild:

docker build -t file-reader .
docker run file-reader

The new content will be printed.

Exercise 7: Modify data.txt Without Rebuilding

Change data.txt but don’t rebuild. Run the container:

docker run file-reader

The output will still be the old content, as the file was copied during build.

Exercise 8: Use a Volume

Run the container with a mounted volume:

docker run -v $(pwd)/data.txt:/data/data.txt file-reader

Now, modifying data.txt will immediately affect the output.

Conclusion

By completing these exercises, you’ve explored key Docker concepts, including building images, running containers, scanning for vulnerabilities, and managing file changes efficiently. Happy Dockerizing!

FAQs

Q: What is Docker?
A: Docker is a containerization platform that allows you to run applications in containers.

Q: What is a container?
A: A container is a lightweight and portable way to package an application and its dependencies.

Q: What is a Dockerfile?
A: A Dockerfile is a text file that contains instructions for building a Docker image.

Q: How do I run a Docker container?
A: You can run a Docker container using the docker run command.

Q: How do I stop a Docker container?
A: You can stop a Docker container using the docker stop command.

Q: How do I remove a Docker container?
A: You can remove a Docker container using the docker rm command.

Latest stories

Read More

Shinoda Tayu to Kuzunoha

The Beautiful Oiran of Japan: Shinoda Tayu Introduction In the world...

The Zenfone 12 Ultra is another big phone the US won’t get

The Zenfone 12 Ultra: A New Chapter in the...

Chipmakers Qualcomm and Arm post sales rise on smartphone strength

Qualcomm and Arm Post Strong Quarterly Sales Growth Qualcomm and...

DeepSeek A.I. Is a Danger to Party Control

China's Ambitious AI Plan In 2017, China watched in awe...

AI Discovers Hidden Cancer Markers

AI Tool Finds Cancer Signs Missed by Doctors Pioneering Research...

Amazon Nova: Cost-Effective and Performant Cloud Computing Options

Security Teams Leverage Amazon Nova Micro to Automate Threat...

LEAVE A REPLY

Please enter your comment!
Please enter your name here