I'm new to Docker and want to put my Django web application into containers.

For the last couple days I'm struggling with passing credentials safe to containers.

Online there is always the hint that secrets should not be passed as environment variable because they can be retrieved via inspect or from the images. My hope is that it can be neglected for my setup.

The setup

  • I provide the credentials via .env file of docker compose. This file is not on version control.

  • The containers are running as non-elevanted users (a different special user).

  • Docker command on the host can be used as root only.

  • I'm the only user of the host server (Ubuntu 18.04).

  • I assume breaking into the host is much harder (I'm using SSH public key authentication with non-root user only) than breaking into container of the Django app.

  • I don't want to use kubernetes or swarm.

I'm newbie to Docker and not a security expert. Do I have overlooked something? Why should I care about the docker inspect or images in this setup?

Thank you in advance

2 Answers 2


As long as no one else is logging into the server or able to run docker commands against the host, then passing secrets as an environment variable into the container which is only stored on the host is relatively secure. About the only risk I can think of is that it is a clear text value on the disk, so you have the risk of the secret escaping with a lost harddrive or backup.

Docker's swarm mode secrets have the following advantages over environment variables and other ways of managing secrets:

  • Values are encrypted on the manager hard drives, though typically, the decryption key is also there to make restarting managers easier.
  • Values are not written to disk on workers, they are injected as a file on a tmpfs filesystem mounted into the container.
  • Values are not able to be displayed with an inspect command on the service, container, or the secret itself. You must deploy a service with the secret to get access to that secret.
  • Secrets are automatically sent to workers on a need to know basis from the managers, no need to build an external process to synchronize files across hosts.
  • The secret file may be limited with unix filesystem permissions, e.g. only enabling root read access, and having your container drop access to another user after loading the secret.

That said, no matter what, all secret management solutions are a way to give your application a clear text password or other credential. And with a process, every way of doing that has some level of risk. One of the best ways to reduce that risk is to make sure every service gets its own individual secret, with only the access needed by that specific service, and that it is frequently rotated. It's also useful to look at additional layers of security, like network policies you can configure with service mesh solutions.

  • Thank you. The missing part were the backups. Since I'm responsible for them and my plan is to backup just the database. So will not have the risk with images. Anyway why I should want to backup them? I have the docker files to simply restore them. I skimmed through the documentation on Docker swarm. As I understand correctly Its not usable with Docker CE on own Servers (on premise). Just for passing secrets it has too much features and a price tag. Sep 28, 2018 at 22:18
  • Docker's swarm mode is included with the CE engine. Implementing it is often as easy as docker swarm init. And deploying your containers if you have a compose file is docker stack deploy -c compose.yml stack_name.
    – BMitch
    Sep 28, 2018 at 22:37

ENV variables are a good way of passing secrets to a container. What you don't want to do is bake the secrets directly in the image. As you have rightly done, do not store secrets in your source control repo as well.

Besides ENV variables, you could also consider using an external secrets repository, such as Hashicorp Vault, or possibly even AWS secrets. See also vault's github page. Your container would still need a token/credentials required to Authenticate/Authorize with the key store, but if required, these can be made to expire/rotate.

So you are on the right path. Some additional reading is worthwhile at this point:

  • Thank you. I looked up habitus.io in detail. But I wasn't able pass credentials. So I gave up and came to the question if it is all worth in my case. Sep 28, 2018 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.