0% found this document useful (0 votes)
22 views12 pages

Docker Lab Exercise

The document outlines a comprehensive Docker lab exercise that covers basic commands, user container creation, Apache and NGINX web server deployment, and Docker volume management. It includes step-by-step tasks for installing Docker, managing containers, creating custom images, and using Docker Compose for service orchestration. The exercises aim to provide hands-on experience with Docker functionalities and automation through scripting.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views12 pages

Docker Lab Exercise

The document outlines a comprehensive Docker lab exercise that covers basic commands, user container creation, Apache and NGINX web server deployment, and Docker volume management. It includes step-by-step tasks for installing Docker, managing containers, creating custom images, and using Docker Compose for service orchestration. The exercises aim to provide hands-on experience with Docker functionalities and automation through scripting.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Docker Lab Exercise

I Docker Basic Commands

Questions:
1. Install Docker on your system and verify the successful installation.
2. Check the Docker service status and ensure it is running.
3. Pull the ubuntu base image from Docker Hub.
4. List all the Docker images available on your local system.
5. Inspect the downloaded ubuntu image in the repositories and display
its low-level information.
6. Create a new Docker container using the ubuntu image.
7. Create another Docker container using the ubuntu image and name it
test.
8. Create an interactive container named user1 using the ubuntu image
and start a bash shell inside it.
9. Start the user1 container.
10. Attach to the running user1 container.
11. Stop the user1 container.
12. Restart the user1 container and attach to it interactively.
13. Write shell scripts for the following tasks:
o Create 100 containers.
o Delete 100 containers.
o Create 10 containers named user1 to user10.
o Start the 10 containers.
14. Monitor computing resource usage for all 10 running containers.
15. Monitor computing resource usage specifically for the user5
container.
16. Check the Docker logs for the user3 container.
17. Check the Docker logs for the user3 container in real-time.
18. Display the detailed runtime details, operational data, and
configurations of the Docker environment.
19. Display the history of the ubuntu Docker image.
20. Search and display the details of the Jenkins Docker image.
21. Remove all existing containers with a single command.
22. Remove the ubuntu Docker image.
23. Create a file named [Link] in the host system and copy it to the
user1 container. Then, create a file named [Link] in the user1
container and copy it back to the host system.
24. Verify that the files ([Link] and [Link]) are successfully
transferred between the host and the user1 container.

1
25. Run a container named user3 in the background using the
ubuntu image, and attach to the container to interact with it.
26. List all running containers by inspecting the
/var/lib/docker/containers directory.
27. Check the log file for a specific running container in the
/var/lib/docker/containers directory.

Q27. Creating a User Container for Isolated Lab Environments


Objective:
This lab helps create a user container on a host system. Users can work in an
isolated container environment with root privileges, minimizing the risk of
impacting the host system. Mentors can monitor user activities and debug
issues effectively, making this ideal for online lab courses.
Steps:
1. Create a User Account: Create a new user named user1 on the host
system: adduser user1
2. Set Password for the User: Assign a password for user1: passwd user1
3. Grant Sudo Privileges: Add user1 to the sudo group: usermod -aG sudo
user1
4. Create a Docker Container for user1: Create and run a container
named user1 using the ubuntu image: docker run -it --name user1 ubuntu
/bin/bash
5. Modify user1’s .profile File: Edit the /home/user1/.profile file to include
the following line: sudo docker start -a -i user1; exit
6. Test Container Access via SSH: SSH into the host system as user1: ssh
<host_ip>. Upon logging in as user1, you will be directly connected to
the user1 container shell.

Benefits:
 Isolation: Mistakes made by users in the container will not affect the
host system.
 Monitoring: Mentors can track user activities and debug container
issues without exposing the host environment.
 Ease of Use: Logging in via SSH seamlessly redirects users to their
container environment.

Q28. Setting Up Apache2 in a Docker Container


Question:
1. Start the user1 Docker container and perform the following tasks inside
the container:
o Update the package list.
o Install the following packages: apache2, apache2-utils, and vim.

2
o Edit the /var/www/html/[Link] file using vim and modify the
content at line number 208 to display a custom message.
o Start the Apache2 service.
o Use elinks with the container's IP address to verify the changes
made to the [Link] file.
Expected Outcome:
 Apache2 is successfully installed and running inside the user1
container.
 The custom message in the /var/www/html/[Link] file is accessible
through the elinks browser.

Q29. Running a Web Server in Docker


Question:
1. Execute the following steps to run a web server inside a Docker
container:
o Pull the Docker image thangaraju/webserver_test from Docker
Hub.
o Run a container using the pulled image, map port 9999 on the
host to port 80 in the container, and start the container
interactively.
o Start the Apache2 service inside the container.
2. Access the web server in a browser using the following URL:
o [Link]
Expected Outcome:
 The web server is successfully running and accessible via the browser
at the specified host IP and port.

Q30. Creating a Docker Image for C Program Development and


Pushing to Docker Hub
Objective:
This lab helps you create a custom Docker image tailored for C program
development and push it to Docker Hub. The image will include necessary
tools for compiling and running C programs, making it reusable across
systems.
Steps:
1. Create and Run a Docker Container: Start a Docker container using
the ubuntu image.
2. Install Development Tools: Inside the container, install the required
packages for C program development (e.g., gcc, make, vim, etc.).
3. Configure the Environment: Set up a directory structure and include
sample files for C program compilation.

3
4. Commit the Container: Commit the container to create a custom
Docker image for C program development.
5. Push the Docker Image to Docker Hub: Log in to Docker Hub and
push the custom image to your repository.
6. Verify the Pushed Image: Check your Docker Hub account to confirm
the image is successfully uploaded.
7. Pull and Test the Image: On another system, pull the pushed image
and verify its functionality by compiling and running a sample C program.
Benefits:
 Reusable Development Environment: Simplifies setup for
compiling and running C programs.
 Cross-System Compatibility: The image can be used on any system
with Docker installed.
 Efficient Sharing: Pushing the image to Docker Hub makes it
accessible for collaboration.

Q31. Working with Docker Volumes


Question:
1. Perform the following tasks to understand and use Docker volumes
effectively:
o Create a Docker Volume: Create a volume named devtest.
o List Docker Volumes: Verify the volume creation by listing all
the available Docker volumes.
o Inspect the Docker Volume: Inspect the devtest volume to
check its properties.
o Mount the Volume: Run a container using the ubuntu image
and mount the devtest volume to /usr/src/app in the container.
o Store Files in the Volume: Inside the container, navigate to
/usr/src/app and create 100 files named [Link] to [Link].
Exit the container once done.
o Access the Volume from Another Container: Start another
container and mount the devtest volume to /app. Verify that all
the 100 files are accessible in the /app directory.
2. Delete the Volume:
o Remove the devtest volume after completing the above steps.
Expected Outcome:
 The devtest volume is successfully created, mounted, and shared
between containers.
 The files stored in the volume are accessible across containers.

Q32. Mini Project: Automated Docker Container and User Management


System

4
Objective:
Design a Dockerfile (or Bash script) that automates the creation of Docker
containers for different user groups, manages user accounts on the host
system, and facilitates cleanup operations. This mini-project focuses on
building an efficient script for managing containerized environments and
user access.

Requirements:
1. User Menu for Docker Images:
o Create a menu-based script to allow users to select one of the
following Docker images:
 unix1admin
 debugger
 java
 developer
 unix_11_assessment_base
o Based on the user's choice, start a container from the selected
image.
2. Dynamic User Account Creation:
o Prompt the user to input the number of user accounts to create.
o Create user accounts (user1, user2, etc.) on the host system.
o For each user:
 Set up a home directory.
 Add .bash_profile configurations to enable direct login into
their respective Docker containers.
3. Container Management:
o For each user created, start a Docker container with the user's
name, using the image selected in the menu.
o Ensure the container is linked to the respective user account on
the host.
4. Cleanup Operations:
o Implement a script to delete all user accounts and associated
Docker containers in bulk.

Deliverables:
1. A functional Bash script that:
o Creates user accounts and containers dynamically.
o Allows users to select Docker images via a menu interface.
o Links user accounts to containers for seamless integration.
2. A cleanup script that:
o Deletes all user accounts and containers created during the
process.

5
Expected Outcome:
 The script should streamline the creation and management of user
accounts and containers for multiple users.
 The cleanup script should remove all traces of user accounts and
containers effectively.
 The project should demonstrate practical automation of containerized
environments using Docker and Bash scripting.

**********

6
II Dockerfile

Q1. Dockerizing a Python Application


Objective: Write and run a Dockerized Python application.
Task:
1. Write a Python script named [Link] that prints:
Hello, World! Welcome to Docker!
2. Create a Dockerfile to containerize the Python script.
3. Build a Docker image named hello-world and run a container based on
this image.
4. Use the docker ps -a command to verify the container creation and its
status.
Instructions:
 Students must create the [Link] script and Dockerfile themselves.
 Demonstrate the successful execution of the container by showing the
output and the container's details using docker ps -a.

Q2. Deploying an Apache Web Server Using Docker


Objective: Set up an Apache web server using Docker and demonstrate its
functionality.
Task:
1. Write a Dockerfile that:
o Uses the ubuntu base image.
o Installs Apache2 and its utilities.
o Exposes port 80.
o Starts the Apache server in the foreground.
2. Build a Docker image named apache-server using the docker build
command.
3. Run a container from the apache-server image and verify:
o The container is running.
o Apache is serving the default page.
4. Use the docker ps -a command to verify the status and details of the
container.
5. Use a tool like elinks or a browser to access [Link] and
confirm the Apache default page is displayed.
Notes:
 Address any warnings about the server's fully qualified domain name
(FQDN) as described in the slides.

Q3. Deploying an Apache Web Server with FQDN Configuration


Objective: Set up an Apache web server using Docker, ensuring there are
no warnings about the server's fully qualified domain name (FQDN).

7
Task:
1. Write a Dockerfile that:
o Uses the ubuntu base image.
o Installs Apache2 and its utilities.
o Updates the Apache configuration file to set the ServerName
directive to localhost to suppress FQDN warnings.
o Exposes port 80.
o Starts the Apache server in the foreground.
2. Build a Docker image named my-image using the docker build
command with the -f option to specify the Dockerfile.
3. Run a container from the my-image image using the docker run
command, mapping port 80 of the container to port 80 of the host.
4. Verify that:
o The container is running (docker ps -a).
o The Apache default page can be accessed at [Link]
using a browser or a text-based tool like elinks.
Additional Task:
 Demonstrate that no warnings related to FQDN appear in the container
logs by ensuring the ServerName localhost directive is correctly
configured in the Apache configuration file.

Q4. Deploying an NGINX Web Server with External Access


Objective: Set up an NGINX web server using Docker, run it in the
background, and access it from outside the virtual machine.
Expected Result:
 The custom HTML page with "Hello, World!" and a welcome message
should be displayed when accessed via a browser at
[Link]
Notes:
 Ensure the VM's network settings allow external access (e.g., set to
bridged mode or port forwarding is configured).

Q5. Multi-Stage Build for a C Application


Question: Create a multi-stage Docker build for a simple C application.
Write a Dockerfile to:
1. Use a GCC image to compile a hello.c program into a static binary
during the first stage.
2. Use an Alpine image in the second stage to copy the compiled binary
and run it as the container's entry point.
Build the Docker image as hello-c-app and run the container to verify the
output.

8
**********

9
III Docker Compose

Q1. Installing Docker Compose


Question: Install Docker Compose on your system by following these steps:
1. Download the latest Docker Compose binary from the official
repository.
2. Apply execute permissions to the downloaded binary.
3. Create a symbolic link to make docker-compose globally accessible.
4. Verify the installation by checking the Docker Compose version.
Demonstrate the successful installation by showing the output of the
docker-compose --version command.

Q2. NGINX Web Server with Docker Compose


Question:
1. Write a [Link] file to:
o Use the nginx:latest image.
o Map port 8080 on the host to port 80 in the container.
o Bind the ./html directory on the host to /usr/share/nginx/html/ in
the container to serve a custom HTML file.
2. Create a custom HTML file ([Link]) in the ./html directory.
3. Start the service using Docker Compose and verify:
o Access the NGINX web server at [Link]
o Show the running containers using docker ps.
4. Stop the service and remove the containers using docker-compose
down.
Demonstrate the process and the expected results during the lab session.

Q3. Live Update with NGINX and Docker Compose


Question:
1. Write a [Link] file to:
o Use the nginx:latest image.
o Map port 8080 on the host to port 80 in the container.
o Bind the ./html directory on the host to /usr/share/nginx/html/ in
the container to serve custom HTML content.
2. Create a custom HTML file ([Link]) in the ./html directory.
3. Start the service using Docker Compose and verify:
o Access the NGINX web server from outside the virtual machine
using a browser or tool like curl at [Link]
4. While the container is running, update the [Link] file.
5. Refresh your browser to confirm that the changes are reflected
immediately without restarting the service or the container.

10
6. Stop the service and clean up the containers using docker-compose
down.
Expected Outcome:
 Verify that the initial and updated HTML content is accessible from
outside the virtual machine, and live updates are visible without
restarting the container.

Q4. Dynamic Scaling with Docker Compose


Question:
1. Write a [Link] file to:
o Define an nginx service using the nginx:latest image.
o Bind the ./html directory on the host to /usr/share/nginx/html/ in
the container.
o Expose port 80 for internal communication (no direct binding to
the host).
2. Perform the following scaling operations:
o Scale Up: Start the services and scale the NGINX service to 3
instances.
 Use docker ps -a to confirm that 3 NGINX containers are
running.
o Scale Down: Reduce the number of NGINX instances to 1.
 Use docker ps -a to confirm that only 1 NGINX container is
running.
3. Stop all services and clean up the environment.
Expected Outcome:
 During scaling up and down, the correct number of NGINX instances
are reflected in docker ps -a.

Q5. Simple Microservices Implementation with Docker Compose


Question:
1. Write a [Link] file to implement a microservices
architecture with the following services:
o frontend:
 Build the service from the frontend directory.
 Map port 5000 on the host to port 5000 in the container.
 Define dependencies on the addition and multiplication
services.
o addition:
 Build the service from the addition directory.
o multiplication:
 Build the service from the multiplication directory.

11
2. Define a custom network named app-network using the bridge driver
to allow all services to communicate internally.
3. Start all the services using Docker Compose and verify:
o Use docker ps to confirm that all three services (frontend,
addition, and multiplication) are running.
o Access the frontend service at [Link] and confirm
that it interacts with the addition and multiplication services.
4. Stop all services and clean up the environment.
Expected Outcome:
 All three services (frontend, addition, and multiplication) are running
and can communicate internally using the app-network.
 The frontend service is accessible at [Link]

**********

12

Common questions

Powered by AI

Shell scripting can create, start, stop, and remove multiple Docker containers automatically. Scripts can loop to create several containers simultaneously (e.g., creating 100 containers), manage their life cycle, and monitor resource usage . Scripts need to handle dynamic aspects, such as container IDs and states, which poses a challenge . Automation scripts must also ensure error handling and logging to track operations. The challenge lies in scalability and ensuring scripts are adaptable to varied environments without causing unexpected issues .

Automating user account and Docker container management involves scripting for account creation, linking containers, and handling cleanup operations. Begin by creating and configuring user accounts with a Bash script that adds users and links their home directories to corresponding Docker containers . The script must include a menu for selecting different Docker images, dynamically create user accounts, and ensure containers are started with appropriate configurations. Cleanup operations must efficiently remove all user accounts and containers . This is useful as it simplifies management, ensures isolated access, and prevents operations on the host system from affecting users' learning environments, enhancing security and usability in training contexts .

To create and manage a Docker container with the Ubuntu image, first, you pull the Ubuntu base image from Docker Hub using the command `docker pull ubuntu` . Then, create a container using this image with `docker run -it --name <container_name> ubuntu /bin/bash`, which starts the container interactively with a bash shell . To manage it, use commands like `docker start`, `docker stop`, and `docker attach`. The implications for isolation include running the container in a separate environment, preventing interference with the host system. Monitoring is facilitated by tracking user activities within the container without affecting the host environment .

Docker Compose automates the deployment and management of a microservices architecture by allowing multiple containerized services to be defined and controlled as a single application in a `docker-compose.yml` file . Internal networking with a custom bridge network like `app-network` allows these services to communicate securely and efficiently, without exposing traffic unnecessarily to the outside . This enhances service interaction fidelity and reduces the complexity associated with managing individual service deployments, leading to improved scalability and maintenance .

Live updates for an NGINX web server hosted in Docker can be managed through a `docker-compose.yml` file that binds a host directory to the container directory serving HTML content . By modifying files within the bound directory, changes are instantaneously reflected in the running container without needing a restart . This is advantageous during development as it allows for rapid iteration and testing, reduces downtime, and improves the feedback cycle, facilitating a seamless development workflow .

Deploying an Apache web server using Docker involves writing a Dockerfile that uses the Ubuntu base image and installs Apache2 . It should update the Apache configuration to set the `ServerName` directive to localhost to avoid FQDN warnings, expose port 80, and start the server in the foreground . Build and run the container with `docker build` and `docker run`, respectively, mapping the host's port to the container's port. The benefits of this setup include a smooth operation without unnecessary warnings cluttering logs, leading to easier troubleshooting and a more professional server setup .

Multi-stage builds in Docker optimize the image size by isolating the build environment from the final application runtime environment. For C applications, the first stage uses a GCC image to compile the program, resulting in a static binary . The subsequent stage uses a minimalistic Alpine image, copying only the necessary binary, thus reducing the image size and improving deployment efficiency . This approach is significant as it enhances security (smaller attack surface) and performance (smaller, faster containers), crucial for production environments .

Implementing a microservices architecture with Docker Compose involves writing a `docker-compose.yml` file that defines multiple services . Each service, like frontend, addition, and multiplication, is set up with its respective build contexts or images and port mappings . To enable communication, define a custom network, such as `app-network`, using the bridge driver for internal service interaction without needing external exposure . This setup benefits by isolating network traffic within the Docker ecosystem, enhancing security, and improving the efficiency and management of inter-service communications .

To transfer files between a Docker container and the host system, you can use the `docker cp` command. First, copy a file from the host to the container using `docker cp <source_path> <container_name>:<destination_path>` . Then, within the container, create or modify the file, and reverse the process to copy it back to the host. Verification involves checking both locations to ensure the presence and integrity of the files, confirming the operation was successful .

Monitoring resource usage in Docker can be done using the `docker stats` command, which provides real-time metrics like CPU and memory usage for all containers . For individual container monitoring, such as the `user5` container, specify the container name or ID with the `docker stats user5` command . Important considerations include setting up thresholds for resource anomalies and ensuring logging and alerting mechanisms are in place to quickly identify and resolve potential issues, thus maintaining container performance and stability .

You might also like