Skip to main content

Lab 005: Write a Docker Compose File



Requirements

The Nautilus application development team shared static website content that needs to be hosted on the httpd web server using a containerised platform. The team has shared details with the DevOps team, and we need to set up an environment according to those guidelines. Below are the details:

a. On App Server 1 in Stratos DC create a container named httpd using a docker compose file /opt/docker/docker-compose.yml (please use the exact name for file).

b. Use httpd (preferably latest tag) image for container and make sure container is named as httpd; you can use any name for service.

c. Map 80 number port of container with port 3003 of docker host.

d. Map container's /usr/local/apache2/htdocs volume with /opt/itadmin volume of docker host which is already there. (please do not modify any data within these locations).


Note

This document keeps the original task text in Requirements and focuses on one direct implementation path using Docker Compose.

Prerequisites

  • You can SSH to App Server 1 in Stratos DC and switch to a privileged shell.
  • Docker and Docker Compose are already installed on the host.
  • The host directory /opt/itadmin already exists and contains the website data to mount into the container.

Steps

Login to the app server 1 and switch to root. For the server credentials, check out the Project Nautilus documentation.

sshpass -p '******' ssh -o StrictHostKeyChecking=no tony@172.16.238.10
sudo su -
******

Proceed to the specified directory and create the docker-compose.yml based on the requirements.

cd /opt/docker
cat <<'EOF' > docker-compose.yml
version: '3'
services:
httpd-container:
image: httpd:latest
container_name: httpd
ports:
- "3003:80"
volumes:
- /opt/itadmin:/usr/local/apache2/htdocs
EOF

Start the container and verify.

docker-compose up -d

[root@stapp01 docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f21d49883bdc httpd:latest "httpd-foreground" About a minute ago Up About a minute 0.0.0.0:3003->80/tcp httpd

We can try to curl the localhost via port 3003.

[root@stapp01 docker]# curl -I http://localhost:3003
HTTP/1.1 200 OK
Date: Fri, 18 Aug 2023 13:49:07 GMT
Server: Apache/2.4.57 (Unix)
Content-Type: text/html;charset=ISO-8859-1

[root@stapp01 docker]# curl http://localhost:3003
```html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<title>Index of /</title>
</head>
<body>
<h1>Index of /</h1>
<ul><li><a href="index1.html"> index1.html</a></li>
</ul>
</body>
</html>

To verify the mappings, we can inspect the container itself.

[root@stapp01 docker]# docker inspect f21 | grep -A 5 "\"Ports\""
"Ports": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "3003"
}
[root@stapp01 docker]#
[root@stapp01 docker]# docker inspect f21 | grep -A 5 Mounts
"Mounts": [
{
"Type": "bind",
"Source": "/opt/itadmin",
"Destination": "/usr/local/apache2/htdocs",
"Mode": "rw",

Verification

  • Confirm the Compose application is running with docker-compose ps or docker ps and verify that the container name is httpd.
  • Confirm the port mapping is correct with docker inspect httpd | grep -A 5 '"Ports"' and verify that host port 3003 maps to container port 80.
  • Confirm the volume mapping is correct with docker inspect httpd | grep -A 5 Mounts and verify that /opt/itadmin is mounted to /usr/local/apache2/htdocs.
  • Confirm the site responds locally with curl -I http://localhost:3003 and expect an HTTP 200 OK response.

Resources