Docker Secrets in/out

Akshit Grover
Oct 23, 2018 · 5 min read

Docker in version 1.13 introduced the concept of secrets. For any application managing configuration variables, ssh keys, passwords, API keys etc. is a crucial part, This data is not be disclosed to anyone outside the authority.

Initially this data was set manually by setting environment variables on the node (which is a part of the swarm), But setting this data on all the nodes turned out to be quite a tedious task, I mean one can also copy a config file but again how many times will you copy the same file on different nodes, But why would anyone copy when you could put all these variables either in your YML stack files or Dockerfiles (NOTE: Anyone can have access to your data when you push your image to docker hub), YML stack files is not a good option as pushing sensitive data on websites like Github (A must to work in a team) is not what is preferred and YML stack files are to be shared for the application to work.

So what we are left with is manual copying, a correction what we were left with! Thanks to this new feature of SECRETS!!!!!!!!!

What are Secrets?

As the name suggests, Secrets are sensitive pieces of data visible having restricted access. Secrets acts as a envelope which contains your data but security lies in only the one who has that envelope will be able to open it.

To Conclude:
are encrypted pieces of sensitive data, Also, Secrets offer restricted access to itself, ie. Only some people will have access to the envelope (Analogy).

How does Secrets Work?

Before dive deep, Keep this in mind that secrets are available only in docker swarms ie. one cannot use secrets by using a single container and docker compose.

Docker swarms work on a consensus algorithm known as Raft, Raft consensus maintains a log of all the events and data, This where secrets are stored encrypted in the raft logs, Because these raft logs are replicated on all the managers, Our little secret is also replicated and there is no need of manual copying. All you need is to define a secret (We’ll get our hands on this soon, Keep reading :) ) But as we read, Secrets do not allow full access it itself, Only certain services which are given access can have access to the secrets otherwise our secret is kept encrypted in the raft logs.

Before we dive into the practical part, There is one gotcha I want you to think.
Maybe you got it, When secrets are added to the raft logs and replicated on other managers, It is still present on the docker host but we want the data decrypted into the container running our application, We can also have multiple applications grouped into different services in need of the same secret,
Do not worry docker does all this for you!

All you need to do is write a command to provide access of a particular secret to a service and manager docker daemon will take care of transferring the secret to the service over a secure channel and storing it decrypted in the container at the path:
Linux Containers: Windows Containers: So, All you need to do is read this sensitive piece of data from this path and use it in your application.

Creating our First Docker Secret Using CLI

To create secrets make sure you have to have a Docker daemon and Docker CLI installed.

There are two ways you can create a docker secret using Docker CLI:

  • Taking input from STDIN —
  • Using a File to Create a Secret —

Next, Step after creating a secret a is to give it’s access to a service:

Allowing a Service to Access a Secret Using CLI

Revoking a Services Access to a Secret Using CLI

If you noticed in the last 3 headings I have mentioned using CLI, So, What will be another way to handle secrets?

You got it right! Compose or Stack files!

Creating a Secret within Compose/Stack files

version: "3"services:
image: akshitgrover/secrets_demo
- "3000:3000"
external: true
file: ./secret_file.txt

In the above example you can see under secrets > SECRET_1 there is an external key, This is used when you have already set a secret using docker CLI and you want to use it while deploying your stack, Another case as seen under SECRET_2 we have used file key, This key is used when you have a sensitive data file but secret is not created yet, In this case, while deploying docker will create a secret itself.
If you have noticed we have already given access to both of these secrets to our application service by defining a secrets array in the service definition.

Note: There is also a more verbose way of giving access to a secret within the compose/stack file, Visit for details.

Getting our hands on, What we have read so far:

Note: Watch the video to get an overall view of how to use secrets with scalable swarm services.
I have used for creating swarms multiple nodes.
Also, Visit to check the node application I have been using in the video to play with secrets.

If you have watched the video you might have noticed that I just deployed the stack and a secret was created, As soon as I scaled the service and curled the localhost on node 2 it returned the correct secret data, Thus docker saves us from manual copying and secure storage and transfer of sensitive data with the help of secrets.


Secrets are used to store sensitive data related to the application, securely and transfer these to services which require them, over a secure and trusted channel between the master and the service containers.

Useful Links:


A big thanks to Luc Juggery for these articles to help me understand the concept better and use it in my application :)


A publication to know about recent technologies that are…

Akshit Grover

Written by

Learn to implement! Craving to give back to the community!



A publication to know about recent technologies that are being implemented and get your hands on exclusive tech articles/tutorials.

Akshit Grover

Written by

Learn to implement! Craving to give back to the community!



A publication to know about recent technologies that are being implemented and get your hands on exclusive tech articles/tutorials.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store