🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOpsSchool!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Docker Lab, Excercise & Assignment – 3

Here’s a practical Lab Guide/Tutorial for each Docker command you listed, with clear explanations and step-by-step examples using httpd (Apache) and ubuntu images.
This is designed so a student can follow on their laptop and actually practice each command.


Docker Command Lab Guide


1. cp — Copy files/folders between a container and the local filesystem

What it does:

Copies files/folders from your computer to a container or from the container to your computer.

Example:

a. Copying from local to container:

# Start an ubuntu container in detached mode
docker run -dit --name test-ubuntu ubuntu

# Create a test file on your host
echo "Hello from your host!" > myfile.txt

# Copy file into the container's /tmp directory
docker cp myfile.txt test-ubuntu:/tmp/
Code language: PHP (php)

b. Copying from container to local:

docker cp test-ubuntu:/tmp/myfile.txt ./copied_from_container.txt
Code language: JavaScript (javascript)

2. diff — Inspect changes to files or directories on a container’s filesystem

What it does:

Shows changes (added/modified/deleted files) made to the container since it started.

Example:

docker run -dit --name diff-ubuntu ubuntu
docker exec diff-ubuntu touch /root/newfile.txt
docker diff diff-ubuntu
# Output will show A /root/newfile.txt (A = Added)
Code language: PHP (php)

3. inspect — Return low-level information on Docker objects

What it does:

Displays detailed JSON info about containers, images, networks, etc.

Example:

docker run -dit --name inspect-httpd httpd
docker inspect inspect-httpd
# Look for "IPAddress", "Mounts", "Config" in the output
Code language: PHP (php)

4. port — List port mappings or a specific mapping for the container

What it does:

Shows how the container’s ports are mapped to your host.

Example:

docker run -d -p 8080:80 --name httpd-port httpd
docker port httpd-port
# Output: 80/tcp -> 0.0.0.0:8080
Code language: PHP (php)

5. update — Update configuration of one or more containers

What it does:

Change resource limits (like CPU, memory) for running containers.

Example:

docker run -dit --name update-ubuntu ubuntu
# Update CPU shares to 512 (default is 1024)
docker update --cpu-shares 512 update-ubuntu
Code language: PHP (php)

6. wait — Block until one or more containers stop, then print their exit codes

What it does:

Waits for a container to stop and prints its exit code.

Example:

docker run --name wait-ubuntu ubuntu sleep 5
docker wait wait-ubuntu
# Output: 0 (means sleep exited successfully)
Code language: PHP (php)

7. logs — Fetch the logs of a container

What it does:

Shows the output (stdout/stderr) of the main process (PID 1) in a container.

Example:

docker run -d --name logs-httpd httpd
docker logs logs-httpd
docker run --name logs-ubuntu ubuntu echo "Hello from Ubuntu"
docker logs logs-ubuntu
# Output: Hello from Ubuntu
Code language: PHP (php)

8. ps — List containers

What it does:

Shows all running containers.

Example:

docker ps                 # Running containers only
docker ps -a              # All containers, including stopped
Code language: PHP (php)

9. stats — Display a live stream of container(s) resource usage statistics

What it does:

Shows CPU, memory, network, and disk stats live.

Example:

docker run -dit --name stats-httpd httpd
docker stats stats-httpd
# Press Ctrl+C to stop viewing stats
Code language: PHP (php)

10. top — Display the running processes of a container

What it does:

Lists the active processes inside a container.

Example:

docker run -dit --name top-ubuntu ubuntu bash
docker top top-ubuntu

11. events — Get real time events from the server

What it does:

Shows real-time Docker events (container start/stop, image pull, etc).

Example:

docker events
# In another terminal, start/stop containers to see events in real time
Code language: PHP (php)

12. Detach from container without stopping it:

When you’re attached to a container (e.g., after docker attach), use:

CTRL + P + Q

(This detaches your terminal from the container, but leaves the container running!)


Lab Flow Example (httpd & ubuntu)

# 1. Start containers
docker run -dit --name myubuntu ubuntu
docker run -d -p 8080:80 --name myhttpd httpd

# 2. Practice each command:
docker cp myfile.txt myubuntu:/tmp/
docker diff myubuntu
docker inspect myhttpd
docker port myhttpd
docker update --cpu-shares 512 myhttpd
docker wait myubuntu
docker logs myhttpd
docker ps
docker ps -a
docker stats myhttpd
docker top myubuntu
docker events
Code language: PHP (php)

Summary Table

CommandWhat it DoesExample
cpCopy files to/from containerdocker cp foo.txt cont:/tmp/
diffShow filesystem changesdocker diff cont
inspectShow low-level object infodocker inspect cont
portShow port mappingsdocker port cont
updateChange container resourcesdocker update --cpu-shares 512
waitWait for container exitdocker wait cont
logsShow container logs (PID 1 output)docker logs cont
psList containersdocker ps -a
statsLive resource statsdocker stats cont
topShow processes in containerdocker top cont
eventsReal-time Docker eventsdocker events

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.