DEV Community

janak0ff
janak0ff

Posted on

Day 44: Write a Docker Compose File – KodeKloud Engineer Challenge

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 3 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 8088 of docker host.

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


Introduction

Welcome back to my 100 Days of DevOps journey! Today marks Day 44, and we're diving into Docker Compose – a powerful tool for defining and running multi-container Docker applications.


📋 Challenge Overview

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

Task Requirements:

  1. On App Server 3 in Stratos DC
  2. Create a container named httpd using a docker compose file /opt/docker/docker-compose.yml
  3. Use httpd:latest image for the container
  4. Map port 80 of container with port 8088 of docker host
  5. Map container's /usr/local/apache2/htdocs with host's /opt/dba volume

🔧 Step-by-Step Solution

Step 1: SSH to App Server 3

ssh banner@stapp03
# Password: BigGr33n
Enter fullscreen mode Exit fullscreen mode

Step 2: Switch to Root User

sudo su -
# Password: BigGr33n
Enter fullscreen mode Exit fullscreen mode

Step 3: Create the Directory Structure

# Create the /opt/docker directory
mkdir -p /opt/docker

# Navigate to the directory
cd /opt/docker
Enter fullscreen mode Exit fullscreen mode

Step 4: Install Docker Compose (if not already installed)

Docker Compose was not installed on the server, so I installed it:

# Download Docker Compose
curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# Grant execution permissions
chmod +x /usr/local/bin/docker-compose

# Verify installation
docker-compose --version
Enter fullscreen mode Exit fullscreen mode

Output:

Docker Compose version v5.4.0
Enter fullscreen mode Exit fullscreen mode

Step 5: Create the Docker Compose File

vi docker-compose.yml
Enter fullscreen mode Exit fullscreen mode

Content:

version: '3'

services:
  httpd-service:
    image: httpd:latest
    container_name: httpd
    ports:
      - "8088:80"
    volumes:
      - /opt/dba:/usr/local/apache2/htdocs
Enter fullscreen mode Exit fullscreen mode

Step 6: Start the Container

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

Output:

WARN[0000] /opt/docker/docker-compose.yml: the attribute `version` is obsolete
[+] up 9/9
 ✔ Image httpd:latest     Pulled
 ✔ Network docker_default Created
 ✔ Container httpd        Started
Enter fullscreen mode Exit fullscreen mode

Step 7: Verify the Container

# Check container status
docker-compose ps
Enter fullscreen mode Exit fullscreen mode

Output:

NAME      IMAGE          COMMAND              SERVICE         CREATED        STATUS                  PORTS
httpd     httpd:latest   "httpd-foreground"   httpd-service   1 second ago   Up Less than a second   0.0.0.0:8088->80/tcp
Enter fullscreen mode Exit fullscreen mode
# Check with docker commands
docker ps | grep httpd
Enter fullscreen mode Exit fullscreen mode

Output:

8f97bad02398   httpd:latest   "httpd-foreground"   1 second ago   Up Less than a second   0.0.0.0:8088->80/tcp   httpd
Enter fullscreen mode Exit fullscreen mode
# Check port mapping
docker port httpd
Enter fullscreen mode Exit fullscreen mode

Output:

80/tcp -> 0.0.0.0:8088
80/tcp -> [::]:8088
Enter fullscreen mode Exit fullscreen mode

Step 8: Test the Web Server

curl http://localhost:8088
Enter fullscreen mode Exit fullscreen mode

Output:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<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>
Enter fullscreen mode Exit fullscreen mode

📋 Complete Commands Summary

# SSH to App Server 3
ssh banner@stapp03
# Password: BigGr33n

# Switch to root
sudo su -
# Password: BigGr33n

# Create directory
mkdir -p /opt/docker
cd /opt/docker

# Create docker-compose.yml
cat > docker-compose.yml << 'EOF'
version: '3'

services:
  httpd-service:
    image: httpd:latest
    container_name: httpd
    ports:
      - "8088:80"
    volumes:
      - /opt/dba:/usr/local/apache2/htdocs
EOF

# Install Docker Compose (if not installed)
curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

# Start the container
docker-compose up -d

# Verify
docker-compose ps
docker ps | grep httpd
docker port httpd
curl http://localhost:8088
Enter fullscreen mode Exit fullscreen mode

Top comments (0)