Getting Started: Stub and Harden a Docker Image with Docker-Compose
Use the RapidFort Command Line Interface (CLI) tools to stub and harden a Docker image with Docker-Compose

Prerequisites

You will need the following:

Stub and Harden the NGINX Docker Image

In this tutorial, we will stub, test, and harden the NGINX Docker image.
Original Image: The NGINX Docker image available directly from Docker Hub. The original image has not yet been optimized and secured by RapidFort and may have known vulnerabilities.
Stub Image: The original image with additional dependencies necessary for RapidFort to trace the runtime behavior.
Hardened Image: The optimized and secured image generated by RapidFort.

Part 1: Get and Deploy the Original Image

Before we start using RapidFort, we will get the original NGINX Docker image and verify that we can run and test this.

Step 1.1: Pull the NGINX Docker Image

Pull the latest NGINX Docker image:
1
docker pull nginx:latest
Copied!
Run docker images and verify that the image is available:
1
docker images | grep nginx
Copied!

Step 1.2: Deploy the Original Image

Download the sample docker-compose.yml file to your client system.
docker-compose.yml
1
web:
2
image: nginx:latest
3
volumes:
4
- ./templates:/etc/nginx/templates
5
ports:
6
- "9999:80"
7
environment:
8
- NGINX_HOST=localhost
9
- NGINX_PORT=80
Copied!
Deploy the original image:
1
docker-compose up -d
Copied!

Step 1.3: Test the Original Image

Open another terminal window and run the following command:
1
curl localhost:9999
Copied!
You should see a welcome message if everything is working as expected.
Undeploy the original image:
1
docker-compose down
Copied!

Part 2: Generate and Deploy the Stub Image

Step 2.1: Log Into RapidFort

Run rflogin to log into RapidFort. Enter your password if prompted.
1
rflogin <your-email-address>
Copied!

Step 2.2: Generate a Stub Image

Run rfstub to generate a stub image:
1
rfstub nginx:latest
Copied!
This creates a new image, nginx:latest-rfstub.
Run docker images to view the stub image:
1
docker images | grep nginx:latest-rfstub
Copied!

Step 2.3: Deploy the Stub Image

Before deploying the stub image, make the following updates to docker-compose.yml:
  • Update the image to nginx:latest-rfstub.
  • Add a new section called cap_add. Add the SYS_PTRACE capability to enable RapidFort to trace the runtime behavior of the stub image.
docker-compose.yml
1
web:
2
image: nginx:latest-rfstub
3
volumes:
4
- ./templates:/etc/nginx/templates
5
ports:
6
- "9999:80"
7
environment:
8
- NGINX_HOST=localhost
9
- NGINX_PORT=80
10
cap_add:
11
- SYS_PTRACE
Copied!
Deploy the stub image:
1
docker-compose up -d
Copied!

Step 2.4: Test the Stub Image

Run some tests. This enables RapidFort to trace the runtime behavior of your container.
For example, run the following command:
1
curl localhost:9999
Copied!

Step 2.5: Verify that Tracing Information is Propagated to RapidFort

Log into the RapidFort dashboard. You should see an image list.
Select the nginx:latest image.
In the left column, select Logs. Verify that the Logs table contains tracing information. You should see file accesses, system calls, and so forth. The Logs table contents are updated in real time.
If the Logs table is empty, please verify the following:
  • The stub image (nginx:latest-rfstub) is currently deployed and running
  • The docker run command specified --cap-add=SYS_PTRACE
  • The Docker host has access to the RapidFort server
When you generate a stub image, RapidFort also scans the original image for packages and known vulnerabilities. You may optionally view the vulnerabilities and packages found in the original image from the RapidFort dashboard.

Step 2.6: Undeploy the Stub Image

When you are finished with running tests, undeploy the stub image:
1
docker-compose down
Copied!

Part 3: Generate and Deploy the Hardened Image

Step 3.1: Harden the Stub Image

Run rfharden to harden the stub image.
1
rfharden nginx:latest-rfstub
Copied!
This creates a new image, nginx:latest-rfhardened.
Run docker images to view the hardened image:
1
docker images | grep nginx:latest-rfhardened
Copied!

Step 3.2: Deploy the Hardened Image

Before deploying the hardened image, make the following updates to docker-compose.yml:
  • Update the image to nginx:latest-rfhardened.
  • Remove the cap_add section. (The SYS_PTRACE capability is not required since we are not profiling the hardened image.)
docker-compose.yml
1
web:
2
image: nginx:latest-rfhardened
3
volumes:
4
- ./templates:/etc/nginx/templates
5
ports:
6
- "9999:80"
7
environment:
8
- NGINX_HOST=localhost
9
- NGINX_PORT=80
Copied!
Deploy the hardened image and test it again:
1
docker-compose up -d
Copied!
1
curl localhost:9999
Copied!
When you are finished with testing the hardened image, undeploy it:
1
docker-compose down
Copied!

Step 3.3: View Image Information

To view information on a RapidFort image, run rfinfo <rapidfort_id>:
1
rfinfo <rapidfort_id>
Copied!
Optionally, to save reports to your local system, run rfinfo with the -s parameter.
1
rfinfo -s <rapidfort_id>
Copied!
You can also visit the RapidFort dashboard for more information.

Review

Stub: Run rfstub to generate a stub image from the original image.
Test: Run and test the stub image instead of the original image.
When running the stub image, add the SYS_PTRACE Linux kernel capability by specifying the following so that RapidFort can trace the runtime behavior:
1
cap_add:
2
- SYS_PTRACE
Copied!
Verify that tracing information is being propagated to the RapidFort server by visiting the RapidFort dashboard and viewing the logs for the stub image.
Harden: Run rfharden to generate a hardened image.
Test Again: Run and test the hardened image again. Verify that the hardened image has the same runtime behavior as the original image.
View the Results: View the attack surface, known vulnerabilities, packages, and files in the original and hardened images. You can also download reports.

Notes

Stub images cannot be used as base images. If you need to make updates, please build a new original image with the updates and then generate a new stub image.