Don’t leak your Docker image’s build secrets
Building a Docker image often involves installing packages or downloading code, and if you’re installing private code you often need to gain access with a secret: a password, a private key, a token. You don’t want those secrets to end up in the final image, though; if it’s in the image, anyone with access to the image can extract it. Unlike docker run, which supports environment variables (-e) and volumes, docker build has traditionally never had a good solution for securely using secrets. So how do you use build secrets in Docker without leaking them? In this article you’ll learn: Some seemingly reasonable but actually insecure or problematic solutions. The easy solution, if you can use modern Docker features. The sneaky, backwards-compatible solution: getting secrets in through the network. Other potential approaches. (Preventing leaking other kinds secrets, like runtime secrets, is covered in a different article.)