Back to Resources

Blog

Posted December 1, 2020

Leveraging Docker Containers to Manage Sauce Connect Tunnels

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.

quote

What is Sauce Connect Proxy?

Sauce Connect Proxy™ is a built-in HTTP proxy server that allows users to access Sauce Labs infrastructure from their local environment  ("localhost") or behind a corporate firewall. This extra layer of security ensures that no sensitive data is exposed, and allows Sauce users to securely test their web and mobile applications.

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

This white paper contains an in-depth overview of Sauce Connect Proxy. 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.

Once Sauce Connect Proxy connection is established, all requests and responses between the browsers running the Selenium test and the web servers may be configured to 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

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 cannot 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" file to 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.

Published:
Dec 1, 2020
Share this post
Copy Share Link
© 2023 Sauce Labs Inc., all rights reserved. SAUCE and SAUCE LABS are registered trademarks owned by Sauce Labs Inc. in the United States, EU, and may be registered in other jurisdictions.