At the end of November, we’ll be migrating the Sematext Logs backend from Elasticsearch to OpenSearch

Docker Security: Using Docker Secrets with Swarm

March 27, 2017

Table of contents

This post shows how to use Docker Secrets in Docker Swarm mode. For our example we’ll show how to use Docker Secrets to encrypt and safely store tokens used by Sematext Docker Agent for shipping Docker metrics and Docker logs.

Before we start, check out this useful Docker Cheatsheet.

Containerized applications often require access to sensitive data like SSL keys, login credentials or access tokens. Containers are commonly configured via environment variables, docker-compose or configuration files stored on unencrypted volumes. While nice and simple, this approach has a major security hole – a simple “docker inspect” command would expose passwords or tokens configured in clear text via environment variables.

Docker engine 1.13 introduced the management of secrets for Docker Swarm mode. Docker manages secrets as a blob in the internal Raft store. This means secrets get the same high availability guarantees that the the rest of the Swarm management data gets. Secrets are encrypted using NACL’s Salsa20Poly1305 with a 256-bit key. Volumes with secrets can be mounted at runtime with the new docker service create --secret <secret-name> option.
Containers can access /run/secrets/<secret-name> to access the file.

It turns out this new feature helps not only with encryption and distribution of sensitive files, but can also be used as a switch between different configurations, e.g. to apply different configurations for staging or production, just by changing the --secret <secret-name> parameter.


Docker Secrets Management

Source: https://blog.docker.com/2017/02/docker-secrets-management/

As Docker Ecosystem Partner we are committed to supporting new Docker features in Sematext Docker Agent (SDA) for container monitoring and log management. We introduced support for Docker Secrets in version 1.31.19 released in March 2017, which means that your Sematext Docker Agent configuration could be managed via Docker secrets command.

Let’s see how to deploy Sematext Docker to monitor a Swarm cluster using the new secrets feature in 3 simple steps.

Step 1: Create a configuration file for Sematext Docker Agent

The configuration file content could include all options, normally passed via -e (Environment) parameter to docker run or docker service create commands.
The example includes just the SPM token and the Logsene token to enable monitoring and logging for all containers. Lets save these tokens to a “sematext-agent.conf” file (which you can remove later if you no longer need it):

# sematext-agent.conf - spm-agent-docker configuration
SPM_TOKEN=YOUR_SPM_TOKEN_HERE
LOGSENE_TOKEN=YOUR_LOGSENE_TOKEN_HERE

Note: The Logsene token is the most sensitive bit of information here because it allow writing and reading of logs. Logsene lets you create write-only tokens and we recommend using them for log shipping.

Step 2: Create the secret with the “docker secret create” command

The first argument is the name of the secret, the second argument is the file name with our secrets:
docker secret create sematext-agent sematext-agent.conf

The docker secret commands accepts stdin as well:
cat sematext-agent.conf | docker secret create sematext-agent -

The docker secret create command creates the encrypted blob which will be available in /run/secrets/sematext-agent at container runtime.

Step 3: Create a Swarm service for Sematext Docker Agent using the secret

Here is the full command to deploy the agent to all Swarm nodes as global service using the “sematext-agent” secret we just created:

$ docker service create --mode global --name sematext-agent-docker \
--mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \
--secret sematext-agent \
sematext/sematext-agent-docker

Check the deployment status of the service with the docker service pscommand:

$ docker service ps sematext-agent-docker

After pulling the image Sematext Docker Agent will start, using the configuration from secrets.

Please note the above command works because /run/secrets/sematext-agent is the default path, where Sematext Docker Agent expects the configuration file. To run SDA with different configurations using secret names different from “sematext-agent” you have to specify the source and target properties in –secret parameter in the docker service create command:

$ docker create secret my-sda-config sematext-agent.conf 
$ docker service create --mode global --name sematext-agent-docker \
--mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \
--secret source=my-sda-config,target=sematext-agent \
sematext/sematext-agent-docker


We hope this short post helped explain how to keep secrets with Docker Secret and how to improve the security of your Docker monitoring and logging deployments with Sematext Docker Agent.

Don’t forget to download the Cheat Sheet you need

If have any question get in touch with us via comments, live chat,  @sematext on Twitter or check out Sematext Docker Agent on Github.

Java Logging Basics: Concepts, Tools, and Best Practices

Imagine you're a detective trying to solve a crime, but...

Best Web Transaction Monitoring Tools in 2024

Websites are no longer static pages.  They’re dynamic, transaction-heavy ecosystems...

17 Linux Log Files You Must Be Monitoring

Imagine waking up to a critical system failure that has...