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
- Exercise 1: Install
curl
and Run a Simple Command - Exercise 2: Serve a Static HTML Page
- Exercise 3: Write a Simple Python Web Server
- Exercise 4: Scan for Vulnerabilities
- Exercise 5: Read a File in a Container
- Exercise 6: Modify
data.txt
and Rebuild - Exercise 7: Modify
data.txt
Without Rebuilding - Exercise 8: Use a Volume
Questions
- Create a Dockerfile that installs
curl
and runscurl google.com
. - Create a Dockerfile that starts a web server on port 8080 and serves a static HTML file with the message
Hello, Docker!
. - 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 thepython:latest
image. - 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? - Write a Python script that reads a file called
data.txt
and prints its content. Create a Dockerfile that copies thedata.txt
file to the image and runs the Python script. For this exercise, you can use thepython:latest
image. - Change the
data.txt
file from the previous exercise and rebuild the image. Does the Python script print the new content? - 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? - Run the Docker image from the previous exercise with a volume mounted to the
/data
directory. Change thedata.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"]
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
Create index.html
:
<!DOCTYPE html>
<html>
<head>
<title>Hello Docker</title>
</head>
<body>
<h1>Hello, Docker!</h1>
</body>
</html>
Build and run:
docker build -t static-site .
docker run -p 8080:80 static-site
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)
Create a Dockerfile
:
FROM python:latest
WORKDIR /app
COPY app.py /app/
RUN pip install flask
EXPOSE 8080
CMD ["python", "app.py"]
Build and run:
docker build -t python-server .
docker run -p 8080:8080 python-server
Access via http://localhost:8080?name=Docker
.
Exercise 4: Scan for Vulnerabilities
Use Docker Scout to check for CVEs:
docker scout cves python-server
You will likely see a list of vulnerabilities. To fix them, use a smaller base image like python:alpine
:
FROM python:alpine
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())
Create a Dockerfile
:
FROM python:latest
WORKDIR /data
COPY data.txt /data/
COPY script.py /data/
CMD ["python", "script.py"]
Build and run:
docker build -t file-reader .
docker run file-reader
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.
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)