Debug School

rakesh kumar
rakesh kumar

Posted on

Docker lab assignment

Docker basics assignment
Docker image assignment
Docker dockerfile assignment
Docker compose assignment
Docker volume assignment
Docker search and filter assignment
Docker swarm assignment

Docker basics assignment

Pull commonly used images

The following images will be used during the workshop and may be pulled in advance to limit network usage.

debian – Official Debian Image
ubuntu – Official Ubuntu Image
scratch – Base image for other images, 0 bytes, no need to pull this
busybox – Minimal Linux distro, 2.5MB
node – Official Node.js image
redis – Official Redis image
mongo – Official Mongo image
postgres – Official Postgres image
jwilder/nginx-proxy – Nginx image with automatic proxy configuration of other containers.
andersjanmyr/counter – Counter web-service with support for multiple databases.

2.Perform the following commands

List all containers, including stopped
List all running containers

List all container IDs (-q quiet)
List latest (-l) container, including stopped
List ID of latest container $ docker ps -l -q
Stop the last container

Start and attach to the stopped container
Exit the container, exit or Ctrl-D
List the container and remember its name.
List the logs of the container by name

Remove the container

Write a script for downloading multiple Docker images

2.Perform the following commands

Run some commands, create a couple of files, then exit exit-

command or Ctrl-D
Enter fullscreen mode Exit fullscreen mode

*List all containers, including stopped *

$ docker ps --all   
Enter fullscreen mode Exit fullscreen mode

List all running containers

$ docker ps 
Enter fullscreen mode Exit fullscreen mode

List all container IDs (-q quiet)

$ docker ps --all -q  
Enter fullscreen mode Exit fullscreen mode

List latest (-l) container, including stopped

$ docker ps -l   
Enter fullscreen mode Exit fullscreen mode

List ID of latest container $ docker ps -l -q

Stop the last container

$ docker stop $(docker ps -l -q)  
Enter fullscreen mode Exit fullscreen mode

Start and attach to the stopped container

$ docker start -ia $(docker ps -l -q) 
Enter fullscreen mode Exit fullscreen mode

Exit the container, exit or Ctrl-D

List the container and remember its name.

$ docker ps -a  
Enter fullscreen mode Exit fullscreen mode

*List the logs of the container by name *

$ docker logs name-of-container  
Enter fullscreen mode Exit fullscreen mode

Remove the container

$ docker rm $(docker ps -l -q) # Or use the name
Enter fullscreen mode Exit fullscreen mode

3.## Write a script for downloading multiple Docker images

for i in debian ubuntu busybox node redis mongo postgres jwilder/nginx-proxy andersjanmyr/counter; do
  docker pull $i
done
Enter fullscreen mode Exit fullscreen mode

Explanation
Save the script to a file, let's say pull_images.sh, and make it executable:

chmod +x pull_images.sh
Enter fullscreen mode Exit fullscreen mode

Run the script:

./pull_images.sh
Enter fullscreen mode Exit fullscreen mode

This script will sequentially pull the specified Docker images from Docker Hub. Adjust the list of images in the loop according to your requirements. The docker pull command downloads the latest version of each specified image by default.

Refrence
Refrence
Script for Displaying Container Information:
Display Container Information Based on a Condition:

#!/bin/bash
for container_id in $(docker ps -aq); do
    container_name=$(docker inspect --format '{{.Name}}' $container_id)
    container_status=$(docker inspect --format '{{.State.Status}}' $container_id)

    if [ $container_status == "running" ]; then
        echo "Container $container_name is running."
    else
        echo "Container $container_name is stopped."
    fi
done
Enter fullscreen mode Exit fullscreen mode

Script for Health Check:
Perform Health Check on Containers:

#!/bin/bash
for container_id in $(docker ps -aq); do
    health_status=$(docker inspect --format '{{.State.Health.Status}}' $container_id)

    if [ $health_status == "healthy" ]; then
        echo "Container $container_id is healthy."
    else
        echo "Container $container_id is not healthy."
    fi
done
Enter fullscreen mode Exit fullscreen mode

Script for Dynamic Container Commands:
Run Dynamic Commands on Containers:

#!/bin/bash
commands=("command1" "command2" "command3")

for container_id in $(docker ps -aq); do
    random_command=${commands[$((RANDOM % ${#commands[@]}))]}
    docker exec $container_id $random_command
done
Enter fullscreen mode Exit fullscreen mode

Script for Updating Containers:
Update Containers Based on Conditions:

#!/bin/bash
for container_id in $(docker ps -aq); do
    update_needed=false

    if [ $update_needed == true ]; then
        docker exec $container_id apt-get update
        docker exec $container_id apt-get upgrade -y
    fi
done
Enter fullscreen mode Exit fullscreen mode

Script for Dynamic Image Pull:
Pull Images Based on Conditions:

#!/bin/bash
images=("image1" "image2" "image3")

for image_name in "${images[@]}"; do
    docker pull $image_name
done
Enter fullscreen mode Exit fullscreen mode

Script for Container Restart:
Restart Containers Based on Uptime:

#!/bin/bash
for container_id in $(docker ps -aq); do
    uptime=$(docker inspect --format '{{.State.StartedAt}}' $container_id)
    restart_threshold=86400  # Restart containers if uptime is more than 24 hours (in seconds)

    current_time=$(date +%s)
    container_start_time=$(date -d "$uptime" +%s)
    uptime_seconds=$((current_time - container_start_time))

    if [ $uptime_seconds -gt $restart_threshold ]; then
        docker restart $container_id
    fi
done
Enter fullscreen mode Exit fullscreen mode

Docker dockerfile assignment

Multi-Stage Builds:

  1. Create a Dockerfile using multi-stage builds.
  2. Optimize the Docker image size using multi-stage builds.
  3. Build a multi-stage Dockerfile for a specific language or framework
    .
    Dynamic Configuration:

  4. Implement dynamic configuration in a Dockerfile.

  5. Use environment variables for configuration.

  6. Demonstrate how to override configuration at runtime
    .
    Secure Dockerfiles:

  7. Follow security best practices in Dockerfile creation.

  8. Scan Docker images for vulnerabilities.

  9. Implement user namespaces for increased container security
    .
    Custom Base Images:

  10. Create a custom base image for your application.

  11. Use a minimal base image for security and efficiency.

  12. Demonstrate the advantages of a custom base image
    .
    Container Orchestration:

  13. Create a Dockerfile for a microservices-based application.

  14. Implement readiness and liveness probes in the Dockerfile.

  15. Showcase how the Dockerfile fits into a larger container orchestration system (e.g., Kubernetes)
    .
    Dynamic Content and Configuration:

  16. Dynamically configure container content during build time.

  17. Use build arguments for conditional content in the Dockerfile.

  18. Show how to manage sensitive information securely
    .
    Layer Caching Strategies:

  19. Optimize Dockerfile layer caching for faster builds.

  20. Utilize multi-line commands strategically to minimize layers.

  21. Demonstrate the impact of each command on layer caching
    .
    Custom Entry Points:

  22. Define custom entry points in a Dockerfile.

  23. Use entry point scripts for initialization tasks.

  24. Explain the benefits of custom entry points
    .
    Health Checks:

  25. Implement health checks in a Dockerfile.

  26. Define a health check command for monitoring container health.

  27. Showcase the integration of health checks with container orchestration
    .
    Environment Variable Handling:

  28. Manage environment variables effectively in a Dockerfile.

  29. Showcase how to use default values for environment variables.

  30. Demonstrate the importance of environment variable handling in different deployment scenarios
    .
    Advanced Build Context Management:

  31. Optimize build contexts for Docker builds.

  32. Use .dockerignore to exclude unnecessary files from the build context.

  33. Explain the impact of build context on build performance
    .
    Optimizing Dockerfile Layers:

  34. Demonstrate techniques for optimizing Dockerfile layers.

  35. Minimize the number of layers for better image efficiency.

  36. Utilize layer squashing where applicable
    .

Basic Custom Entry Point

:

  1. Create a simple custom entry point script (e.g., entrypoint.sh) in the Dockerfile.
  2. Use the ENTRYPOINT instruction in the Dockerfile to set the custom entry point script
    .
    Passing Arguments to Entry Point:

  3. Demonstrate how to pass command-line arguments to the custom entry point script.

  4. Utilize the CMD instruction in combination with the custom entry point script to handle default command behavior
    .
    Environment Variable Usage:

  5. Showcase the use of environment variables within the custom entry point script.

  6. Explain how environment variables can be used to configure the container at runtime
    .
    Handling Signals and Process Signals:

  7. Implement signal handling in the custom entry point script.

  8. Illustrate how the entry point script can gracefully handle signals (e.g., SIGTERM, SIGINT)
    .
    Graceful Shutdowns:

  9. Implement a mechanism in the entry point script for graceful container shutdowns.

  10. Demonstrate how the custom entry point script responds to container termination signals
    .
    Entrypoint with Default Commands:

  11. Combine the CMD instruction with the custom entry point script to provide default behavior.

  12. Explain the interaction between the entry point script and default command
    .
    Dynamic Configuration:

  13. Showcase how the custom entry point script can dynamically configure the container based on input parameters.

  14. Use entry point scripts to fetch external configuration files or data during container startup
    .
    Error Handling:

  15. Implement error handling mechanisms within the custom entry point script.

  16. Handle errors gracefully and provide informative error messages
    .
    Multiple Entry Points:

  17. Create multiple entry point scripts for different container startup scenarios.

  18. Illustrate how to switch between entry points based on specific conditions
    .
    Logging and Output:

  19. Implement logging mechanisms within the custom entry point script.

  20. Demonstrate how to redirect script output and errors to log files or standard streams
    .
    Integration with Other Tools:

  21. Showcase integration with other tools or services from within the custom entry point script.

  22. Illustrate how the entry point script can interact with external services during container startup
    .
    Documentation:

  23. Provide clear and concise documentation on the purpose and usage of the custom entry point script.

  24. Explain any configuration options, environment variables, or arguments supported by the entry point script
    .

Top comments (0)