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 – 2


Part 1: How to Access the Docker Container

Accessing containers is a fundamental skill. Whether you want to debug, update configuration, or inspect running services, you’ll need to “get inside” the container or reach its running applications.

Scenario:

You are running an Apache HTTP Server (httpd) in a Docker container. How do you access the server or the container itself?


Method 1: Go Inside Using docker exec

The docker exec command lets you start a new interactive shell session inside a running container.

Step-by-Step Example (using httpd):

  1. Run an httpd container in the background: docker run -d --name my-httpd httpd
  2. Access the container shell using exec: docker exec -it my-httpd /bin/bash
    • The -it flags mean interactive terminal.
    • /bin/bash starts a bash shell.
    Note: Some images (like httpd) might only have /bin/sh. Try that if /bin/bash is missing.
  3. You’re now inside the container! Try: ls /usr/local/apache2/htdocs

Method 2: Access via IP Address (Container Networking)

You can access running services inside a container using the container’s IP address (on the Docker bridge network) from your host.

Example: Find the IP address and access httpd homepage

  1. Find the container’s IP address:docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-httpd
    • Output: e.g., 172.17.0.2
  2. Access the HTTP server using curl: curl http://172.17.0.2/
    • This should return the Apache HTTP Server default page HTML.
    Note: By default, Docker containers are on a private bridge network. curl using the container’s IP works from the host machine. To access from outside, you must use port mapping (see below).

Bonus: Expose the Container on a Host Port

To access the container’s web server from your browser or network, map container’s port 80 to host port 8080:

docker run -d --name my-httpd -p 8080:80 httpd
Code language: CSS (css)

Now, open http://localhost:8080 in your browser.


Part 2: Docker Run Commands (With Examples)

The docker run command is the workhorse of Docker. It’s used to pull images, create and start containers, and decide whether to attach to their console or run in the background.


A. docker run – Foreground Mode (Attached)

This command:

  • Pulls the image if not available locally
  • Creates a container from it
  • Starts the container
  • Attaches your terminal to its output

Example with Ubuntu:

docker run -it ubuntu
  • Launches an Ubuntu container and attaches your terminal.
  • You get a shell prompt (root@containerid:/#).
  • Exit by typing exit.

Example with httpd (Not interactive):

docker run httpd
  • This attaches output of Apache server to your terminal (not common; it will show logs).

B. docker run -d – Detached Mode (Background)

-d stands for “detached.” You won’t see the container’s output in your terminal.

Example:

docker run -d --name my-httpd -p 8080:80 httpd
Code language: CSS (css)
  • The container runs in the background.
  • Visit http://localhost:8080 in your browser to see the Apache welcome page.

Manual Attach: docker attach

If you want to attach your terminal to a running container’s main process output:

docker attach my-httpd
  • (To detach, use Ctrl + p then Ctrl + q.)

Tip: docker attach works best for containers running foreground processes (like httpd or ubuntu with a shell).


Part 3: Useful Docker Commands for Monitoring & Management

Learn to monitor, inspect, and debug running containers:

CommandPurposeExample
logsFetch logs from a containerdocker logs my-httpd
psList running containersdocker ps
statsShow real-time resource usage (CPU, RAM, etc.)docker stats
topDisplay processes running inside a containerdocker top my-httpd
eventsGet real-time events from the Docker daemondocker events

A. Fetching Container Logs

To see what’s happening inside your container:

docker logs my-httpd
  • Shows output from the httpd process (access, error logs).

B. Listing Containers

List only running containers:

docker ps

List all containers (including stopped):

docker ps -a

C. Live Resource Usage Stats

See real-time CPU, memory, and network usage for all containers:

docker stats

D. Inspect Processes Inside a Container

See what’s running inside:

docker top my-httpd
  • Displays a process table, similar to ps aux.

E. Real-Time Docker Events

Track what’s happening on your Docker host (container start/stop, images pulled, etc.):

docker events

Summary Table: Key Docker Commands

TaskCommand Example
Start an interactive Ubuntu shelldocker run -it ubuntu
Run Apache HTTPD in backgrounddocker run -d --name my-httpd -p 8080:80 httpd
Exec into running container shelldocker exec -it my-httpd /bin/bash
Find container IP addressdocker inspect -f '{{.NetworkSettings.IPAddress}}' my-httpd
View container logsdocker logs my-httpd
List running containersdocker ps
View container statsdocker stats
See processes in containerdocker top my-httpd
Attach terminal to running containerdocker attach my-httpd
View Docker eventsdocker events

Lab: Try It Yourself

1. Run httpd and access the server via browser:

docker run -d --name lab-httpd -p 8081:80 httpd
Code language: CSS (css)

2. Enter the container shell and check Apache root:

docker exec -it lab-httpd /bin/bash
ls /usr/local/apache2/htdocs

3. Get container IP and access via curl:

docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' lab-httpd
curl http://<container-ip>/
Code language: JavaScript (javascript)

4. Check resource stats and logs:

docker stats
docker logs lab-httpd
docker top lab-httpd


Here’s a short, practical tutorial for understanding and using containers, including PID 1 concepts, basic Docker usage, and how container networking works. This is ideal for quick learning, labs, or sharing with new team members.


Quick Tutorial: Using Containers & Understanding PID 1

1. How to Use a Container

a. Go Inside a Running Container

  • List files: docker exec <container_id> ls
  • Get an interactive shell: docker exec -it <container_id> /bin/bash

b. How to Access Container from Outside

Containers usually run inside a VM or physical server:

[ YOU ] ---> [VM IP:port] ---> Redirected to [Container IP:port]
Code language: CSS (css)
  • Expose container ports to outside: docker run -d -p 80:80 httpd # VM:80 --> Container:80 docker run -d -p 81:80 httpd # VM:81 --> Container:80 docker run -d -p 82:80 httpd # VM:82 --> Container:80
  • Now, access using: http://<VM-IP>:80 http://<VM-IP>:81 http://<VM-IP>:82

2. Docker RUN Basics

CommandWhat It Does
docker run httpdPull + create + start + attach to PID 1 (httpd)
docker run -d httpdPull + create + start, run in background
docker attach <container_id>Attach to running container’s main process (PID 1)

3. The Concept of PID 1

  • Physical/VM:
    • System is running because PID 1 (init/systemd) is running.
    • PID 1 is always the main kernel/system process.
  • Container:
    • Container is running because PID 1 is running.
    • PID 1 can be any executable (bash, nginx, java, etc.)

Examples:

  • If you run: docker run ubuntu bash
    • bash is PID 1 in the container.
  • If you run: docker run mysql
    • mysqld is PID 1 in the container.

If PID 1 exits, the container stops.


4. Sample Lab/Commands

# Create a container (stopped state)
docker create httpd

# List running containers
docker ps

# List all containers (including stopped)
docker ps -a

# Start a container
docker start <container_id>

# Exec into a container and list files
docker exec <container_id> ls

# Exec into a container and see processes
docker exec <container_id> ps

# Attach to a container's terminal
docker attach <container_id>

# Run Ubuntu interactively (PID 1 = bash)
docker run -it ubuntu

# Run a process in the background
docker run -d httpd
Code language: PHP (php)

5. Networking Recap

You access containers through the VM’s public IP and mapped port:

[Your Browser] ---> [VM Public IP:80] ---> [Container:80]
Code language: CSS (css)
  • For port mapping:
    docker run -d -p <host_port>:<container_port> <image>

6. Summary Table

WhereWhy is it running?What is PID 1?
Physical ServerPID 1 runningKernel system (init)
Virtual MachinePID 1 runningKernel system (init)
ContainerPID 1 runningAny executable you run

Remember:

  • In containers, your process is PID 1.
  • If PID 1 stops, container stops.
  • Use docker ps, docker exec, docker run, and port mapping for management and access.


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.