Integration: Docker

Test your Docker applications using Ghost Inspector

Search our documentation:

Toggle Documentation Menu

Integration: Docker

Docker is a popular and powerful method for packaging and deploying your modern web application. In this tutorial we're going to look at a couple different ways we can test our containerized applications using Ghost Inspector.

Getting started

Before we get started we are going to need the following:

We offer two different Docker images to aide in testing your containerized app:

Base Image (node.js)

The base image test runner is designed for you to extend with your source files for the purpose of testing your application. This can be useful if you have a fairly simple Docker setup or are working in a CI environment that does not support multi-container Docker (such as Bitbucket Pipelines). The image includes all the utilities and scripts needed to execute your Ghost Inspector test suite against your Node.js application.

Dockerfile

Let's have a look at an example Dockerfile:

FROM ghostinspector/test-runner-node

# Copy your source code
COPY . .

# Install your app
RUN npm install .

# Configure the test runner
ENV APP_PORT 3000
ENV GI_API_KEY <ghostinspector-api-key>
ENV GI_SUITE <ghostinspector-suite-id> 
ENV GI_PARAM_myVar some-custom-value
ENV NGROK_TOKEN <your-ngrok-token> 

# Pass your application entrypoint into our test runner script
CMD ["index.js"]

This Dockerfile simple extends our ghostinspector/test-runner-node image, copies our application files, and installs the application. Next we will need to adjust the environment variables for the test runner to execute properly, and then adjust our CMD accordingly.

Environment variables

In order for this image to successfully interact with your application we will need to provide some configuration at runtime, either for the test running script, or for your Ghost Inspector test suite.

Some of the environment variables are required:

Note that all environment variables will also be available to your application in the process.env object.

CMD

The last line of our Dockerfile is going to be the CMD. This should be the Node.js entrypoint of your application, in our case it looks like:

CMD ["index.js"]

Note that it could be server.js or myapp/start.js or whatever Node.js script fires up your app, as long as it is a Node.js file.

Also, if you require additional parameters on startup, you are not restricted by the number of parameters that you pass into CMD:

CMD ["index.js", "--sky=blue"]

Any additional parameters past the index.js position will be passed to your application when it is started up.

Firing up

Now, assuming everything is configured correctly, we should be able to build and run our container and watch it's progress:

$ docker build -t my-app .
$ docker run my-app

The container will now start your application, open a tunnel using ngrok, and execute your Ghost Inspector suite via the API using the API key and suite ID you've provided, along with any custom parameters defined in the Environment Variables.

Continue to the next section to learn about executing your test suite within a more complex Docker environment.

Standalone image

This Docker image will allow you to execute your Ghost Inspector test suite against another running Docker container within your cluster by using Docker container networking.

First, pull down the ghostinspector/test-runner-standalone image to your local environment:

$ docker pull ghostinspector/test-runner-standalone

Setting up the network

Next we will create a new network in Docker-land so we can link the applications at runtime:

$ docker network create my-test-network

This will create a network that we can attach to our Docker containers at runtime to give them the ability to communicate privately.

Start the application

Then we can start your application in the background (-d), noting that <my-image> will need to be changed to the name of your application Docker image:

$ docker run -d --name my-app --network my-test-network <my-image>

Here we have named the new container my-app and connected it to the network my-test-network.

Execute the test suite

Once that's up and running, we can fire up the Ghost Inspector test-runner-standalone image which will run our suite against our running app:

$ docker run \
  -e NGROK_TOKEN=<my-ngrok-token> \
  -e GI_API_KEY=<my-api-key> \
  -e GI_SUITE=<my-suite-id> \
  -e GI_PARAM_myVar=foo \
  -e APP_PORT=my-app:8000 \
  --network my-test-network \
  ghostinspector/test-runner-standalone

Make sure you change out all the environment variables to your own custom values for NGROK_TOKEN, GI_API_KEY, and GI_SUITE. Notice that for APP_PORT we've passed in my-app:8000, my-app will resolve to our running docker container that we named my-app and 8000 assumes that is the port that our application is running on. Finally we also connect this new container to the same my-test-network so all the DNS and networking magic can happen. Also note that you can send custom variables to the API call when we execute your test suite by using a custom environment variable named GI_PARAM_varName where varName is the name of your variable. This will be available in your Ghost Inspector tests under at runtime.

Once this container fires up, it will perform the exact same set of actions as before:

Wrapping up

That's it! If everything went well you should be able to visit your dashboard and check on the status of your test suite.

If you run into issues setting up this tutorial, or any other problems with our product, feel free to drop us a line in support and we'll be happy to answer any questions for you.