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):
- Run an httpd container in the background:
docker run -d --name my-httpd httpd
- 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.
httpd
) might only have/bin/sh
. Try that if/bin/bash
is missing. - The
- 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
- Find the container’s IP address:
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-httpd
- Output: e.g.,
172.17.0.2
- Output: e.g.,
- Access the HTTP server using
curl
:curl http://172.17.0.2/
- This should return the Apache HTTP Server default page HTML.
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
thenCtrl + q
.)
Tip:
docker attach
works best for containers running foreground processes (likehttpd
orubuntu
with a shell).
Part 3: Useful Docker Commands for Monitoring & Management
Learn to monitor, inspect, and debug running containers:
Command | Purpose | Example |
---|---|---|
logs | Fetch logs from a container | docker logs my-httpd |
ps | List running containers | docker ps |
stats | Show real-time resource usage (CPU, RAM, etc.) | docker stats |
top | Display processes running inside a container | docker top my-httpd |
events | Get real-time events from the Docker daemon | docker 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
Task | Command Example |
---|---|
Start an interactive Ubuntu shell | docker run -it ubuntu |
Run Apache HTTPD in background | docker run -d --name my-httpd -p 8080:80 httpd |
Exec into running container shell | docker exec -it my-httpd /bin/bash |
Find container IP address | docker inspect -f '{{.NetworkSettings.IPAddress}}' my-httpd |
View container logs | docker logs my-httpd |
List running containers | docker ps |
View container stats | docker stats |
See processes in container | docker top my-httpd |
Attach terminal to running container | docker attach my-httpd |
View Docker events | docker 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
Command | What It Does |
---|---|
docker run httpd | Pull + create + start + attach to PID 1 (httpd ) |
docker run -d httpd | Pull + 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
Where | Why is it running? | What is PID 1? |
---|---|---|
Physical Server | PID 1 running | Kernel system (init) |
Virtual Machine | PID 1 running | Kernel system (init) |
Container | PID 1 running | Any 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.

















I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow , and SEO strategies at Wizbrand.
Do you want to learn Quantum Computing?
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at WIZBRAND