Skip to content
Laviyx Learn

Try

Docker Practice

Practise Docker: running containers, writing Dockerfiles, volumes, networks and Compose. Every solution has been run with Docker on Ubuntu and shows its real output.

Intermediate12 problemsUpdated

Review first: Running Your First Container, Managing Containers, Writing a Dockerfile, Layers, Cache and .dockerignore, Multi-Stage Builds, Volumes, Container Networking, Environment Variables and Secrets, Getting Started with Compose

  1. 1.Run a one-off command

    Beginner

    Use alpine:3.24 to print the Alpine version from /etc/alpine-release, showing only the first two parts, and leave no container behind.

    Show hint

    --rm removes the container, and cut -d. -f1,2 keeps two parts.

    Show solution
    Terminal
    docker run --rm alpine:3.24 cat /etc/alpine-release | cut -d. -f1,2
    docker ps -aq | wc -l
    Output
    3.24
    0
  2. 2.Publish a web server

    Beginner

    Run nginx:1.30-alpine in the background, as web, on host port 8081. Print the HTTP status code from curl, then remove it.

    Show hint

    -d --name web -p 8081:80.

    Show solution
    Terminal
    docker run -d --name web -p 8081:80 nginx:1.30-alpine > /dev/null
    sleep 2
    curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8081
    docker rm -f web > /dev/null
    Output
    200
  3. 3.Pass configuration

    Beginner

    Run Alpine with CITY=Hyderabad set, and print Welcome to Hyderabad using that variable.

    Show hint

    -e, and sh -c so that the variable is expanded inside the container.

    Show solution
    Terminal
    docker run --rm -e CITY=Hyderabad alpine:3.24 sh -c 'echo "Welcome to $CITY"'
    Output
    Welcome to Hyderabad
  4. 4.Build a custom image

    Beginner

    Write a Dockerfile that serves index.html with Nginx. Build it as page:1.0, run it on port 8082, and print the page.

    Starter code (Terminal)
    echo "<h1>My page</h1>" > index.html
    Show hint

    FROM, then COPY to /usr/share/nginx/html/.

    Show solution
    Terminal
    cat > Dockerfile <<'EOF'
    FROM nginx:1.30-alpine
    COPY index.html /usr/share/nginx/html/index.html
    EOF
    docker build -q -t page:1.0 . > /dev/null
    docker run -d --name site -p 8082:80 page:1.0 > /dev/null
    sleep 2
    curl -s http://localhost:8082
    docker rm -f site > /dev/null
    Output
    <h1>My page</h1>
  5. 5.Run as a non-root user

    Intermediate

    Build an image from alpine:3.24 that creates a user called worker, runs as that user, and prints the user's name when it starts.

    Show hint

    adduser -D worker, then USER worker.

    Show solution
    Terminal
    cat > Dockerfile <<'EOF'
    FROM alpine:3.24
    RUN adduser -D worker
    USER worker
    CMD ["whoami"]
    EOF
    docker build -q -t worker:1.0 . > /dev/null
    docker run --rm worker:1.0
    Output
    worker
  6. 6.CMD and arguments

    Intermediate

    Build an image with ENTRYPOINT ["echo", "Result:"] and CMD ["none"]. Run it with no arguments, then with the argument 42.

    Show hint

    Arguments replace CMD, and are added after ENTRYPOINT.

    Show solution
    Terminal
    cat > Dockerfile <<'EOF'
    FROM alpine:3.24
    ENTRYPOINT ["echo", "Result:"]
    CMD ["none"]
    EOF
    docker build -q -t res:1.0 . > /dev/null
    docker run --rm res:1.0
    docker run --rm res:1.0 42
    Output
    Result: none
    Result: 42
  7. 7.A two-stage build

    Intermediate

    Write a multi-stage Dockerfile whose first stage creates /out/report.txt containing Built, and whose final stage, based on alpine:3.24, copies it in and prints it.

    Show hint

    FROM alpine:3.24 AS build, then COPY --from=build.

    Show solution
    Terminal
    cat > Dockerfile <<'EOF'
    FROM alpine:3.24 AS build
    RUN mkdir /out && echo Built > /out/report.txt
    
    FROM alpine:3.24
    COPY --from=build /out/report.txt /report.txt
    CMD ["cat", "/report.txt"]
    EOF
    docker build -q -t twostage:1.0 . > /dev/null
    docker run --rm twostage:1.0
    Output
    Built
  8. 8.Keep data in a volume

    Intermediate

    Use a named volume store. Write hello to /data/msg in one container, and read it from another.

    Show hint

    -v store:/data on both commands.

    Show solution
    Terminal
    docker run --rm -v store:/data alpine:3.24 sh -c 'echo hello > /data/msg'
    docker run --rm -v store:/data alpine:3.24 cat /data/msg
    Output
    hello
  9. 9.A read-only bind mount

    Intermediate

    Mount the current folder read-only at /work in Alpine, print notes.txt, and show that writing fails.

    Starter code (Terminal)
    echo "Host file" > notes.txt
    Show hint

    Add :ro to the -v option.

    Show solution
    Terminal
    docker run --rm -v "$PWD":/work:ro alpine:3.24 sh -c 'cat /work/notes.txt; touch /work/new'
    Output
    Host file
    touch: /work/new: Read-only file system
  10. 10.Connect containers by name

    Intermediate

    Create a network shop, start Nginx on it as store, and fetch the page title from an Alpine container using the name store. Then clean up.

    Show hint

    --network shop on both containers.

    Show solution
    Terminal
    docker network create shop > /dev/null
    docker run -d --name store --network shop nginx:1.30-alpine > /dev/null
    sleep 2
    docker run --rm --network shop alpine:3.24 wget -qO- http://store | grep -o '<title>.*</title>'
    docker rm -f store > /dev/null
    docker network rm shop > /dev/null
    Output
    <title>Welcome to nginx!</title>
  11. 11.A Compose file with two services

    Intermediate

    Write a compose.yaml with web (Nginx on port 8083) and helper (Alpine running sleep 30). Start it, print the service names, check that helper can reach web, and take it down.

    Show hint

    Services reach each other by service name.

    Show solution
    Terminal
    cat > compose.yaml <<'EOF'
    services:
      web:
        image: nginx:1.30-alpine
        ports:
          - "8083:80"
      helper:
        image: alpine:3.24
        command: sleep 30
    EOF
    docker compose up -d > /dev/null 2>&1
    sleep 2
    docker compose ps --format '{{.Service}}' | sort
    docker compose exec helper wget -qO- http://web | grep -c nginx
    docker compose down -t 1 > /dev/null 2>&1
    Output
    helper
    web
    7
  12. 12.Read an exit code

    Beginner

    Run Alpine with the command sh -c 'echo Failed >&2; exit 3', named job. Print its logs and its exit code, then remove it.

    Show hint

    docker logs, and .State.ExitCode with docker inspect.

    Show solution
    Terminal
    docker run --name job alpine:3.24 sh -c 'echo Failed >&2; exit 3'
    docker inspect --format '{{.State.ExitCode}}' job
    docker rm job > /dev/null
    Output
    Failed
    3