DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

UK-US Data Bridge: Join TechnologyAdvice and OneTrust as they discuss the UK extension to the EU-US Data Privacy Framework (DPF).

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Kubernetes in the Enterprise: The latest expert insights on scaling, serverless, Kubernetes-powered AI, cluster security, FinOps, and more.

A Guide to Continuous Integration and Deployment: Learn the fundamentals and understand the use of CI/CD in your apps.

Related

  • Understanding and Using Docker Containers in Web Development: A Guide
  • Ways To Reduce JVM Docker Image Size
  • Comparative Analysis of Open Source Cluster Management Systems: Kubernetes vs. Apache Mesos
  • Simplify Docker Container Management at Scale With Amazon ECS

Trending

  • Ultimate Guide to Kubernetes StatefulSets With a MongoDB Example
  • Demystifying Distributed Systems: A Beginner’s Guide
  • A Comprehensive Guide to Cloud Monitoring Tools: Ensuring Optimal Performance and Security
  • Unlocking the Power of Streaming: Effortlessly Upload Gigabytes to AWS S3 With Node.js
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Introduction to Docker Secrets

Introduction to Docker Secrets

We introduce Docker secrets, which offer a secure way to store sensitive info such as username, passwords, and even files.

Denis Bell user avatar by
Denis Bell
·
Nov. 10, 17 · Tutorial
Like (21)
Save
Tweet
Share
56.2K Views

Join the DZone community and get the full member experience.

Join For Free

Overview

In this post, we will be introducing Docker secrets. Docker secrets offer a secure way to store sensitive information such as username, passwords, and even files like self-signed certificates.

Prerequisites

Docker – See how to install Docker here.

Swarm mode enabled - Ensure that swarm mode is enabled before starting this tutorial. If it is not, open your terminal window and type the docker swarm init command.

No Secrets Are No Good

Before we start using secrets, let's look at the disadvantages of not using it. Below is a compose file with the definition of a Postgres  and adminer (a database client) service:

version: '3.1'

services:

  db:
    image: postgres
    environment:
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mysupersecretpassword
      POSTGRES_DB: mydatabase

  adminer:
    image: adminer
    ports:
     - 8080:8080

We have supplied the username, password, and database name for the Postgres service by setting the POSTGRES_USER, POSTGRES_PASSWORD, and POSTGRES_DB environment variables (have a look Postgres Docker image documentation to see other environment variables which can be used).

You may have noticed that the sensitive details for our database are in plain text for all the world to see. This is poor practice and should only be done for local deployments of containers. In the next section, we will be modifying this example to use secrets.

Securing Our Swarm With Secrets

Let's dive right in and see how to create secrets.

Open a terminal window and create a secret for the username by typing the following command:

echo "myuser" | docker secret create pg_user -

We have used the docker secret create command above to create a secret called pg_user. The dash "-" at the end of the command is important, it lets docker know that the data for the secret is being taken from stdin.

To view the secret, type the following command:

docker secret ls

You should see a similar output displayed:

ID                                                        NAME                CREATED       UPDATED
dv82o89ngapgr9lrxm6987uqh     pg_user              5 days ago      5 days ago

Let's create the remaining secrets for password and database name:

echo "mysupersecretpassword" | docker secret create pg_password -
echo "mydatabase" | docker secret create pg_database -

Now we need to modify the compose file to use the secrets we created, see below:

version: '3.1'

services:

    db:
        image: postgres
        restart: always
        environment:
            POSTGRES_USER_FILE: /run/secrets/pg_user
            POSTGRES_PASSWORD_FILE: /run/secrets/pg_password
            POSTGRES_DB_FILE: /run/secrets/pg_database
        secrets:
           - pg_password
           - pg_user
           - pg_database

    adminer: 
        image: adminer 
        ports: 
         - 8080:8080

secrets:
  pg_user:
    external: true
  pg_password:
    external: true
  pg_database:
    external: true

Create a file called postgres-secrets.yml and copy the text above to it. This file will be used later in this post to deploy our services.

We've added a few things to make our secrets work. Firstly, environment variables we used previously have been suffixed with  "_FILE." The path to the secret is also specified, see below:

POSTGRES_USER_FILE: /run/secrets/pg_user 

POSTGRES_PASSWORD_FILE: /run/secrets/pg_password 

POSTGRES_DB_FILE: /run/secrets/pg_database

Docker secrets are stored in files under the /run/secrets folder of the container. This is why we have to specify new environment variables to read the secrets stored in these files.

Important Note: Not all images have environment variables which are compatible with secrets. I many cases you will have to modify the definition of images (Dockerfile) to read secrets.

Secondly, we specified the name of the secrets which are being used by the service:

secrets:
- pg_password
- pg_user
- pg_database

And last but not least we indicated that the secrets are external:

secrets:
 pg_user:
   external: true
 pg_password: 
   external: true
 pg_database: 
   external: true

Deploying the Service

Let's deploy our service to determine if our secrets are working as expected. Type the following command:

docker stack deploy -c postgres-secrets.yml postgres

Give the services about a minute to fully start, and navigate to http://127.0.0.1:8080/ to view the Adminer interface and enter the details as shown below:

adminer1Click the Login button and you should see the admin interface displayed, see below:

adminer2

If you see the screen above then our secrets are being stored and read by our Postgres service. Remember to always use secrets to improve the security of your docker services.

Docker (software)

Published at DZone with permission of Denis Bell. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Understanding and Using Docker Containers in Web Development: A Guide
  • Ways To Reduce JVM Docker Image Size
  • Comparative Analysis of Open Source Cluster Management Systems: Kubernetes vs. Apache Mesos
  • Simplify Docker Container Management at Scale With Amazon ECS

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: