
The graphic design trends disclose the marvelous phenomena of how art embrace the life essence. These are not just trends of […]
By AayushNonetheless, today’s typical developer is expected to know the fundamentals of containerizing their apps. Docker is the most widely used containerization tool.
There are specific Docker ideas that most developers overlook despite the growing need for containers. Among these ideas is the handling of private information in containers. This can be seen in the fact that most developers hardcode private information into their Dockerfiles or
environment variables, such as API keys, database credentials, and other secrets.
This is not recommended as it may result in security flaws. The best practices for handling sensitive data in Docker containers will be walked through in this post. It will teach you how to secure your applications using Docker’s built-in secrets management capability. By the end of this tutorial, you will understand how to safely transfer sensitive data to your containers.
There will be numerous real-world examples and helpful demos in this text.
You will need the following to follow along:
A feature of Docker containers called “Docker secrets” enables the safe and secure use and storage of sensitive data. SSH keys, API keys, database credentials, or any other information that shouldn’t be disclosed to anyone with container access could be considered sensitive data.
Data sent between Docker containers is not encrypted or protected by default, making it possible for someone with network access to intercept and read the data. Docker secrets are added to the mix in order to give your data the security it needs.
Suppose one of your secrets is your TLS certificates, which are kept in plaintext in your Dockerfile or Docker image. This implies that this secret is easily accessible to anyone who can access your Dockerfile or Docker image. You require Docker secrets for this reason.
In addition to its primary security purpose, Docker secrets are necessary for additional purposes as well.
Among these explanations are the following:
Let’s now examine a real-world application of Docker secrets. First, we will create a secret and then demonstrate how to use it inside a service.
Let’s start by making a new secret with a password:
$ echo “mypassword” | docker secret create my_secret –
The use of the — at the end of the command instructs Docker to read the secret’s value from standard input rather than a file. This command will create a new secret called my_secret, containing the password “password.” Let’s now develop an essential service that utilizes this secret. We’ll use the official NGINX image, and the password for basic authentication will be our secret:
$ docker service create –name nginx –secret my_secret nginx:latest
Using the standard NGINX image, we create a new service called nginx with this command. Additionally, we affix our previously generated secret, my_secret, to the function. The secret can now be accessed by the NGINX container through a file at /run/secrets/my_secret. To use the secret password for simple authentication, we can make a reference to this file in our nginx settings. Remember that the secret is exposed as a file rather than as an environment variable. This method adds another degree of protection because logs or process listings may reveal environment variables.
To see a list of your Docker secrets, run the ls command (docker secret). This command will show a table including the names, creation dates, and secret IDs. This is an illustration of the command:
$ docker secret ls
The output will look something like this:
ID NAME CREATED UPDATED
vqnz6btp7gru0z6gk8zvtey6k my_secret 2minutes ago 2minutes ago
But the list of secrets and their information are the only things displayed by the Docker secret ls command. The actual contents of the secrets are not displayed. Use the docker secret inspect command, followed by the secret’s name or ID, to examine a secret and see its metadata in more detail:
$ Docker secret inspect my_secret
The secret’s accurate content will remain hidden even after this command returns the metadata in a JSON format. Since Docker secrets are meant to be secure, it would be counterproductive to reveal their content via CLI instructions. Instead, a secret’s content can be accessed by attaching it to a service in your Docker Swarm; the secret will then be accessible as a file within that service’s containers.
When utilizing Docker secrets, bear in mind the following recommended practices:
You can manage secrets with a collection of subcommands provided by the docker secret command. Some of the most popular subcommands are listed below:
As can be observed, the command “docker secret ls” provides a list of all the secrets kept on the Docker host.
Docker secret inspects: This command offers comprehensive details on a particular secret. It allows you to access a secret’s metadata, including its ID, name, and creation date.
Docker private examine my_secret
docker secret inspect my_secret
[
{
“ID”: “kotjiwncv3kp8uyyj8d7kzesn”,
“Version”: {
“Index”: 11
},
“CreatedAt”: “2024-03-19T12:07:44.93980171Z”,
“UpdatedAt”: “2024-03-19T12:07:44.93980171Z”,
“Spec”: {
“Name”: “my_secret”,
“Labels”: {}
}
}
]
To remove a secret from the Docker host, execute the following command: docker secret rm. By providing the secret ID or name as an argument, you can utilize it to erase a secret.
docker secret rm my_secret
my_secret
Services operating on the same Docker Swarm are the only ones with access to Docker secrets. This implies that only services included in a Docker Swarm are eligible to use Docker secrets.
For those unfamiliar, Docker Swarm is a platform for container orchestration that lets you oversee a group of Docker servers.
You must first establish a secret and provide service access to it in order to use Docker secrets with Docker Swarm. To accomplish this, modify the docker service creation command to include the –secret parameter.
The example that follows will build a redis service on Docker Swarm that has a secret.
docker swarm init
echo “mysecretpassword” | docker secret create redis_password –
qg8cpym1q5v96jg67qkj4q5rc
docker service create –name redis –secret redis_password redis:alpine
p3rmff7x5ta4gi4804cp9suj3
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged
You are establishing a redis service and giving it access to the redis_password secret with this command. The service is created using the redis: alpine image. This command creates the redis service and grants it access to the redis_password secret. The application will access the secret once installed within the service’s containers.
docker service ps redis
docker container exec $(docker ps –filter name=redis -q) ls -l /run/secrets
total 4
-r–r–r– 1 root root 16 Mar 19 12:51 redis_password
docker container exec $(docker ps –filter name=redis -q) ls -l /run/secrets
ls: /run/secrets: No such file or directory
The redis service won’t be able to access the redis_password secret after executing this command.
docker container exec $(docker ps –filter name=redis -q) ls -l /run/secrets
ls: /run/secrets: No such file or directory
There is no such file or directory ls: /run/secrets
docker service rm redis
redis
As was previously noted, Docker compose enables you to run several containers simultaneously. Still, Docker secrets can only be used on multiple containers. This implies that Docker Compose and Docker secrets can be used together.
When using Docker secrets with Docker Compose, there are two syntaxes available. The long and short syntaxes are these. Whereas the short syntax just allows you to specify the secret’s name, the extended syntax will enable you to define many attributes of the secret.
In the following demo, Docker composes will be used to apply a secret to a Redis service using the short syntax.
echo “mysecretpassword” > my_secret.txt
version: “3.9”
services:
redis:
image: redis:alpine
secrets:
– my_secret
secrets:
my_secret:
file: ./my_secret.txt
You are establishing a service called redis in this file and giving it access to my private secret. The service is created using the Redis: alpine image. The value is read from the my_secret.txt file and used to generate the my_secret secret using the file attribute.
Docker-compose up -d
This command will build the Redis service and allow access to the my_secret secret. The application will have access to the secret once installed within the service’s containers.
docker-compose ps
Name Command State Ports
—————————————————————
ubuntu_redis_1 docker-entrypoint.sh redis … Up
Name Command State Ports
—————————————————
redis docker-entrypoint.sh ubuntu_redis_1… Raising
docker container exec $(docker ps –filter name=redis -q) ls -l /run/secrets
total 4
-rw-rw-r– 1 1000 redis 17 Mar 19 18:12 my_secret
Now that the redis secret is mounted into the redis container, the application can access it.
Note: SemaphoreCI also leverages secrets in your CI/CD pipelines to manage sensitive data much more quickly. You can specify your secret using a YAML file, the CLI, or the UI.
Here on SemaphoreCI, you can see how to manage your secrets.
The primary lesson to be learned from this paper is the importance of secrets management in container security. Using Docker secrets, you may safely handle and store sensitive data in your containers. The inner workings of Docker secrets, their management, and how to incorporate them into Docker Swarm and Docker Compose are all covered in detail in this article. You can safely store and utilize your secrets in your applications if you follow the examples.
Emblem
Get free consultation for your digital product idea to turn it into reality!