DEV Community

Cover image for Docker Exercises: Part 1
Mohammad-Ali A'RÂBI for Docker

Posted on

Docker Exercises: Part 1

Docker is the go-to solution for building, running, and managing containers. As a Docker Captain, I was asked to conduct a workshop on Docker basics. The following exercises are designed to help beginners understand key Docker concepts. Let's dive in!

Topics:

  • Building Docker images
  • Running Docker containers
  • Scanning for vulnerabilities
  • Using ports and volumes

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, <name>!. 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 <image> 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 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"]
Enter fullscreen mode Exit fullscreen mode

Build and run the container:

docker build -t curl-test .
docker run --rm curl-test
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Create index.html:

<!DOCTYPE html>
<html>
<head>
    <title>Hello Docker</title>
</head>
<body>
    <h1>Hello, Docker!</h1>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Build and run:

docker build -t static-site .
docker run -p 8080:80 static-site
Enter fullscreen mode Exit fullscreen mode

Visit http://localhost:8080 in your browser.


Exercise 3: Write a Simple Python Web Server

Create a Python web server that reads a name from the query string and returns Hello, <name>!.

from flask import Flask, request

app = Flask(__name__)

@app.route('/')
def hello():
    name = request.args.get('name', 'World')
    return f'Hello, {name}!'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)
Enter fullscreen mode Exit fullscreen mode

Create a Dockerfile:

FROM python:latest

WORKDIR /app
COPY app.py /app/

RUN pip install flask

EXPOSE 8080
CMD ["python", "app.py"]
Enter fullscreen mode Exit fullscreen mode

Build and run:

docker build -t python-server .
docker run -p 8080:8080 python-server
Enter fullscreen mode Exit fullscreen mode

Access via http://localhost:8080?name=Docker.


Exercise 4: Scan for Vulnerabilities

Use Docker Scout to check for CVEs:

docker scout cves python-server
Enter fullscreen mode Exit fullscreen mode

You will likely see a list of vulnerabilities. To fix them, use a smaller base image like python:alpine:

FROM python:alpine
Enter fullscreen mode Exit fullscreen mode

Rebuild and scan again.


Exercise 5: Read a File in a Container

Create a Python script that reads data.txt:

with open("/data/data.txt", "r") as file:
    print(file.read())
Enter fullscreen mode Exit fullscreen mode

Create a Dockerfile:

FROM python:latest
WORKDIR /data
COPY data.txt /data/
COPY script.py /data/
CMD ["python", "script.py"]
Enter fullscreen mode Exit fullscreen mode

Build and run:

docker build -t file-reader .
docker run file-reader
Enter fullscreen mode Exit fullscreen mode

Exercise 6: Modify data.txt and Rebuild

Modify data.txt and rebuild:

docker build -t file-reader .
docker run file-reader
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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


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!

Top comments (0)