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

Pendulum: Predictive Supply Chain Insights

Supply Chain Disruptions and the Rise of Pendulum The Turbulent...

The Role of Customizable Booths in Advancing Robotics and AI Exhibitions

Artificial intelligence (AI) holds a special place in...

Lawmakers Take Aim at DeepSeek

The Justice Department Agrees to Restrict Access to Treasury...

Super Mario’s Dark Side Revealed

Creepy Mario Myths that are Actually True! A Darker Side...

Dive into Her Heart

saizoai With over two decades of experience in crafting art...

AI Agents to Significantly Improve Employee Productivity

ARK's Big Ideas 2025 ARK Invest's Big Ideas 2025 highlights...

Avelios Nabs $31M Led by Sequoia to Fix the Ailing World of Healthcare IT

The Race to Build a New Generation of Healthcare...

PHILIPS 27E1N1600AE Affordable Business Monitor

Work-Play Balance: Philips 27E1N1600AE Monitor Review Like many creatives, my...

LEAVE A REPLY

Please enter your comment!
Please enter your name here