Docker Compose

Guide for using Garnet with Docker Compose

Below we describe two methods of configuring Docker Compose using Garnet.

For using Garnet in CI and production environments, it is recommended that you use Service Keys. You can read more on configuring service keys on this page.

Method 1: Environment variables in Docker Compose

This method passes the environment variables specified in the docker-compose.ymlfile to the containers. For each service, all required variables would have to be specified explicitly to be picked up by that service.

For example, below is a docker-compose.yaml file that will pass on one required environment variable (GARNET_SERVICE_KEY) along with two custom variables:

version: "3.3"
services:
web:
container_name: garnet-node-test
build: .
image: node:14-alpine
environment:
- GARNET_SERVICE_KEY
- EXAMPLE_VARIABLE_1
- EXAMPLE_VARIABLE_2

Now you can use the Garnet CLI to inject the specified environment variables in Docker Compose:

Production
Local dev/testing
Production

Assuming the GARNET_SERVICE_KEYvariable is provided in your CI/CD or production environment, you can use the following command:

garnet run --service-key=$GARNET_SERVICE_KEY -- docker-compose up
Local dev/testing

Assuming you have authenticated with Garnet using the garnet login , you can use the following command:

garnet run <project-name>/<scope> -- docker-compose up

Using the above method, Docker Compose is run the same way in development environments as it is in production. This allows you to keep the same compose file, but use different configuration contexts depending on the supplied variables.

Method 2: Inside the Dockerfile

This method installs the Garnet CLI in a Dockerfile and requires the GARNET_SERVICE_KEY environment variable to authenticate and define the configuration scope. You can use the following Dockerfile as an example:

FROM node:14-alpine
# Install cURL (if not installed)
RUN apk add -U curl
# Install the Garnet CLI
RUN curl -LO https://garnet.jfrog.io/artifactory/garnet-release/garnet-v0.1.0-linux-amd64
RUN chmod +x ./garnet-v0.1.0-linux-amd64
RUN mv ./garnet-v0.1.0-linux-amd64 /usr/bin/garnet
# Fetched variables can be viewed using the env command, for testing.
# In a real use case, this can be replaced with an actual command e.g. npm start
CMD garnet run --service-key=${GARNET_SERVICE_KEY} -- env

After you have a Dockerfile, you can use the belowdocker-compose.yamlfile as an example:

version: "3.3"
services:
web:
container_name: garnet-node-test
build: .
image: node:14-alpine
environment:
- GARNET_SERVICE_KEY

The following command expects the GARNET_SERVICE_KEY environment variable. This can be provided in any standard way (ie. through exporting the variable in the local shell environment, or through Garnet itself)

# assumes that the GARNET_SERVICE_KEY variable is set
docker-compose up
# OR by explicit definition (for local development)
GARNET_SERVICE_KEY="${GARNET_SERVICE_KEY}" docker-compose up

Using $GARNET_SERVICE_KEY allows Docker Compose to be run and configured in any computing environment that uses environment variables.