🎯 Objective
Use docker exec to enter a running Docker container, install Apache2, change its default HTTP port from 80 to 8087, and verify that the web server is running correctly.
The main Docker command is:
docker exec -it <container_name> bash🧠 Concept
docker exec allows us to execute commands inside an already running container.
The basic syntax is:
docker exec [OPTIONS] <container> <command>For an interactive Bash shell:
docker exec -it <container_name> bashThe flow is:
Docker Host
│
│ docker exec -it
▼
Running Container
│
├── Install Apache
├── Configure Port 8087
├── Start Apache
└── Test Web ServerUnlike docker run, docker exec does not create a new container. It executes a command inside an existing running container.
🔍 Step 1: Check Running Containers
List currently running containers:
docker psExample:
CONTAINER ID IMAGE COMMAND STATUS NAMES
abc123def456 ubuntu "bash" Up 5 mins kkloudIdentify the container name you want to access.
🚀 Step 2: Enter the Container
Open an interactive Bash shell:
docker exec -it <container_name> bashExample:
docker exec -it kkloud bashCommand Breakdown
docker execExecute a command inside a running container.
-iKeeps standard input open for interactive use.
-tAllocates a pseudo-terminal.
Together:
-it → Interactive terminalFinally:
bashstarts a Bash shell inside the container.
📦 Step 3: Update Package Repository
Inside the container, update the APT package index:
apt updateThis downloads the latest package information from the configured Ubuntu/Debian repositories.
🌐 Step 4: Install Apache2
Install Apache:
apt install -y apache2The -y option automatically confirms the installation.
Verify the installation:
apache2 -vYou should see Apache version information similar to:
Server version: Apache/2.4.x
Server built: ...⚙️ Step 5: Change Apache Port
Apache listens on port 80 by default.
The requirement is:
Default Port
80
↓
Change
↓
8087Apache's listening port is configured in:
/etc/apache2/ports.confReplace:
Listen 80with:
Listen 8087using:
sed -i 's/Listen 80/Listen 8087/' /etc/apache2/ports.confWhat sed -i Does
sed
↓
Search and replace text
-i
↓
Modify the file directlySo:
sed -i 's/Listen 80/Listen 8087/' /etc/apache2/ports.confmeans:
Find: Listen 80
Replace: Listen 8087🌍 Step 6: Update the Virtual Host Port
Apache's default site also expects port 80.
The configuration is located at:
/etc/apache2/sites-available/000-default.confChange:
<VirtualHost *:80>to:
<VirtualHost *:8087>using:
sed -i 's/<VirtualHost \*:80>/<VirtualHost \*:8087>/' /etc/apache2/sites-available/000-default.confNow both configurations use:
8087The relationship becomes:
/etc/apache2/ports.conf
│
└── Listen 8087
/etc/apache2/sites-available/000-default.conf
│
└── <VirtualHost *:8087>🧠 Listen vs VirtualHost
These two settings have different purposes.
Listen
Listen 8087tells Apache:
> Accept incoming connections on TCP port 8087.
VirtualHost
<VirtualHost *:8087>tells Apache:
> Use this virtual-host configuration for requests arriving on port 8087.
For this setup, both should reference the new port.
🧪 Step 7: Validate Apache Configuration
Before starting Apache, check the configuration:
apache2ctl configtestExpected output:
Syntax OKThis is important because it catches configuration errors before the service is started.
The flow is:
Modify Configuration
↓
apache2ctl configtest
↓
Syntax OK
↓
Start Apache▶️ Step 8: Start Apache
Start the Apache service:
service apache2 startInside many containers, service is more appropriate than:
systemctl start apache2because containers often do not run systemd as PID 1.
🔍 Step 9: Check Apache Status
Check whether Apache is running:
service apache2 statusYou should see output indicating that Apache is running.
🌐 Step 10: Test Apache
Test the web server from inside the container:
curl http://localhost:8087If everything is configured correctly, Apache should return the default HTML page.
The request flow is:
curl
│
│ http://localhost:8087
▼
Port 8087
│
▼
Apache
│
▼
Default Website
│
▼
HTML Response🔍 Optional: Verify Listening Port
You can also verify that Apache is listening on 8087.
If ss is available:
ss -lntpOr specifically:
ss -lntp | grep 8087You should see Apache listening on something equivalent to:
0.0.0.0:8087or:
*:8087This is important if Apache must accept connections through localhost, the container IP, and other available interfaces rather than being bound to one specific IP address.
🚪 Step 11: Exit the Container
Once verification is complete:
exitThis exits the interactive shell.
It does not stop the container.
exit
↓
Leave container shell
↓
Container remains running⚠️ Important: Container Port vs Published Port
Configuring Apache to listen on:
8087means Apache is listening on port 8087 inside the container.
That does not automatically make port 8087 accessible from the Docker host or external network.
For example, a container created with:
docker run -p 8087:8087 ...maps:
Host :8087
│
│ Docker port mapping
▼
Container :8087
│
▼
ApacheIf the task only requires testing from inside the container:
curl http://localhost:8087then publishing the port may not be necessary.
🐳 docker exec vs docker run
docker run
Creates and starts a new container:
Image
│
│ docker run
▼
New Containerdocker exec
Runs a command inside an existing running container:
Existing Running Container
│
│ docker exec
▼
Execute CommandExample:
docker exec -it kkloud bashdoes not create another container.
⚙️ Complete Workflow
Check the running container:
docker psEnter it:
docker exec -it <container_name> bashUpdate packages:
apt updateInstall Apache:
apt install -y apache2Verify installation:
apache2 -vChange Apache's listening port:
sed -i 's/Listen 80/Listen 8087/' /etc/apache2/ports.confUpdate the default virtual host:
sed -i 's/<VirtualHost \*:80>/<VirtualHost \*:8087>/' /etc/apache2/sites-available/000-default.confValidate:
apache2ctl configtestStart Apache:
service apache2 startCheck status:
service apache2 statusTest:
curl http://localhost:8087🧪 Validation Checklist
📌 Summary
The key Docker command is:
docker exec -it <container_name> bashOnce inside the container:
apt update
apt install -y apache2
apache2 -v
sed -i 's/Listen 80/Listen 8087/' /etc/apache2/ports.conf
sed -i 's/<VirtualHost \*:80>/<VirtualHost \*:8087>/' /etc/apache2/sites-available/000-default.conf
apache2ctl configtest
service apache2 start
service apache2 status
curl http://localhost:8087The complete flow is:
Docker Host
│
│ docker exec -it
▼
Running Container
│
├── apt install apache2
│
├── Listen 80 → 8087
│
├── VirtualHost 80 → 8087
│
├── configtest
│
└── Start Apache
│
▼
Apache :8087
│
│ curl localhost:8087
▼
Website Response ✅The key takeaway is:
> docker exec lets us execute commands inside an existing running container. In this task, we use it to install and configure Apache to listen on port 8087 without creating a new container.