Leveraging Docker Containers to Manage Sauce Connect Tunnels

Posted Dec 1, 2020

Sauce Labs Rubiks Cube

In this article we will leverage Docker containers. This will be useful if you’re an existing customer, managing your Sauce Connect tunnels manually, and you want to know how to automate and scale this solution with Docker. If you’re looking to become a Sauce Labs customer, this will give you a good idea of how to Dockerize one of the most important components of your test infrastructure.

What is Sauce Connect Proxy?

Sauce Connect Proxy™ is a built-in HTTP proxy server that opens a secure "tunnel" connection for testing between a Sauce Labs virtual machine or real device and a website or mobile app hosted on your local computer ("localhost") or behind a corporate firewall. It provides a means for Sauce Labs to access your application or website.

Sauce Connect Proxy Benefits

Sauce Connect Proxy has some other uses in your testing network architecture:

  • Provides ability to monitor upstream traffic through a proxy like BrowserMob

  • Stabilizes network connections (e.g., detecting and re-sending dropped packets)

  • You can use WonderProxy to change the Geolocation of your test traffic

How Sauce Connect Proxy Works

Sauce Connect Proxy is based on a proprietary protocol that runs over TLS and is available for use by any Sauce Labs account. For information on downloading, setting up, and configuring Sauce Connect Proxy in your network architecture, please see the topics under Getting Started with Sauce Connect Proxy and Sauce Connect Proxy Setup and Configuration.

All requests and responses between the browsers running the Selenium test and the web servers pass through it. Since Sauce Connect Proxy is just being used internally in your Sauce Labs testing, it doesn't matter which port it uses. A free port is allocated dynamically when Sauce Connect Proxy is started.

Why Docker?

If you’re not sure where Docker fits, consider these use cases:

  1. If you’re creating automation (or manual testing) for multiple applications that reside in different environments, say for example: Desktop Web and Headless. They need to create two different Sauce Connect tunnels in both environments. Keep in mind this is flexible so you could create a Sauce Connect tunnel in all the sauce environments using this approach as well.

  2. If you’re creating multiple High Availability Sauce Connect tunnels on the same system, without having to manage or use different ports.

  3. If you no longer want to manage Sauce Connect versions

If any of these apply to you, or are even in the neighborhood of your situation, keep reading.

Sauce Connect Docker Image

Supported Tags

  • 4.6.2, 4.6.2-apline-glibc, latest

  • 4.5.4

How to Use Sauce Connect Docker Image

Before we can run the container you need to pull it from Docker Hub:

$ docker pull saucelabs/sauce-connect

This will pull the latest version of Sauce Connect which we recommend to use. You can always specify a specific tag. To run the image, execute:

$ export SAUCE_USERNAME="my-user"
$ export SAUCE_ACCESS_KEY="my-access-key"
docker run \
    -e SAUCE_USERNAME=${SAUCE_USERNAME} \
    -e SAUCE_ACCESS_KEY=${SAUCE_ACCESS_KEY} \
    --network="host" \
    -it saucelabs/sauce-connect

Additional arguments may be specified as you would normally do with Sauce Connect. Ensure you have --network="host" set as argument otherwise Sauce Connect within the Docker container can not access your local services in the host machine.

CI Example

If you want to run this Docker image as part of your CI/CD pipeline, you can run the following steps:

Create "wait-for-sc.sh" fileTo ensure we only continue our pipeline once Sauce Connect is fully connected, we need a simple shell script that waits for Sauce Connect to be ready:# wait-for-sc.sh

until [ -f /tmp/sc.ready ]
do
    sleep 5
done
echo "SC ready"
  1. exit

  2. Pull docker image

$ docker pull saucelabs/sauce-connect

Start Sauce Connect. It is important that you mount a temp folder so that wait-for-sc.sh can detect when Sauce Connect has launched. Also make sure that you set --network="host" to allow Sauce Connect to access your application in the host machine.

$ docker run \
    -e SAUCE_USERNAME=${SAUCE_USERNAME} \
    -e SAUCE_ACCESS_KEY=${SAUCE_ACCESS_KEY} \
    -v /tmp:/tmp \
    --network="host" \
    -t saucelabs/sauce-connect:latest \
    -f /tmp/sc.ready \
    -i some-identifier &

3. $ ./wait-for-sc.sh

Have a look into the GitHub Actions pipeline for this repository. If you use GitHub Actions you can just make use of our GitHub Actions Integration.

Written by

Zuzana Bodíková

Topics

Container Testing

Categories