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
*List all containers, including stopped *
$ docker ps --all
List all running containers
$ docker ps
List all container IDs (-q quiet)
$ docker ps --all -q
List latest (-l) container, including stopped
$ docker ps -l
List ID of latest container $ docker ps -l -q
Stop the last container
$ docker stop $(docker ps -l -q)
Start and attach to the stopped container
$ docker start -ia $(docker ps -l -q)
Exit the container, exit or Ctrl-D
List the container and remember its name.
$ docker ps -a
*List the logs of the container by name *
$ docker logs name-of-container
Remove the container
$ docker rm $(docker ps -l -q) # Or use the name
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
Explanation
Save the script to a file, let's say pull_images.sh, and make it executable:
chmod +x pull_images.sh
Run the script:
./pull_images.sh
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
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
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
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
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
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
Docker dockerfile assignment
Multi-Stage Builds:
- Create a Dockerfile using multi-stage builds.
- Optimize the Docker image size using multi-stage builds.
Build a multi-stage Dockerfile for a specific language or framework
.
Dynamic Configuration:Implement dynamic configuration in a Dockerfile.
Use environment variables for configuration.
Demonstrate how to override configuration at runtime
.
Secure Dockerfiles:Follow security best practices in Dockerfile creation.
Scan Docker images for vulnerabilities.
Implement user namespaces for increased container security
.
Custom Base Images:Create a custom base image for your application.
Use a minimal base image for security and efficiency.
Demonstrate the advantages of a custom base image
.
Container Orchestration:Create a Dockerfile for a microservices-based application.
Implement readiness and liveness probes in the Dockerfile.
Showcase how the Dockerfile fits into a larger container orchestration system (e.g., Kubernetes)
.
Dynamic Content and Configuration:Dynamically configure container content during build time.
Use build arguments for conditional content in the Dockerfile.
Show how to manage sensitive information securely
.
Layer Caching Strategies:Optimize Dockerfile layer caching for faster builds.
Utilize multi-line commands strategically to minimize layers.
Demonstrate the impact of each command on layer caching
.
Custom Entry Points:Define custom entry points in a Dockerfile.
Use entry point scripts for initialization tasks.
Explain the benefits of custom entry points
.
Health Checks:Implement health checks in a Dockerfile.
Define a health check command for monitoring container health.
Showcase the integration of health checks with container orchestration
.
Environment Variable Handling:Manage environment variables effectively in a Dockerfile.
Showcase how to use default values for environment variables.
Demonstrate the importance of environment variable handling in different deployment scenarios
.
Advanced Build Context Management:Optimize build contexts for Docker builds.
Use .dockerignore to exclude unnecessary files from the build context.
Explain the impact of build context on build performance
.
Optimizing Dockerfile Layers:Demonstrate techniques for optimizing Dockerfile layers.
Minimize the number of layers for better image efficiency.
Utilize layer squashing where applicable
.
Basic Custom Entry Point
:
- Create a simple custom entry point script (e.g., entrypoint.sh) in the Dockerfile.
Use the ENTRYPOINT instruction in the Dockerfile to set the custom entry point script
.
Passing Arguments to Entry Point:Demonstrate how to pass command-line arguments to the custom entry point script.
Utilize the CMD instruction in combination with the custom entry point script to handle default command behavior
.
Environment Variable Usage:Showcase the use of environment variables within the custom entry point script.
Explain how environment variables can be used to configure the container at runtime
.
Handling Signals and Process Signals:Implement signal handling in the custom entry point script.
Illustrate how the entry point script can gracefully handle signals (e.g., SIGTERM, SIGINT)
.
Graceful Shutdowns:Implement a mechanism in the entry point script for graceful container shutdowns.
Demonstrate how the custom entry point script responds to container termination signals
.
Entrypoint with Default Commands:Combine the CMD instruction with the custom entry point script to provide default behavior.
Explain the interaction between the entry point script and default command
.
Dynamic Configuration:Showcase how the custom entry point script can dynamically configure the container based on input parameters.
Use entry point scripts to fetch external configuration files or data during container startup
.
Error Handling:Implement error handling mechanisms within the custom entry point script.
Handle errors gracefully and provide informative error messages
.
Multiple Entry Points:Create multiple entry point scripts for different container startup scenarios.
Illustrate how to switch between entry points based on specific conditions
.
Logging and Output:Implement logging mechanisms within the custom entry point script.
Demonstrate how to redirect script output and errors to log files or standard streams
.
Integration with Other Tools:Showcase integration with other tools or services from within the custom entry point script.
Illustrate how the entry point script can interact with external services during container startup
.
Documentation:Provide clear and concise documentation on the purpose and usage of the custom entry point script.
Explain any configuration options, environment variables, or arguments supported by the entry point script
.
Top comments (0)