Search code examples
dockerdockerfiledocker-build

Dockerfile - Hide --build-args from showing up in the build time


I have the following Dockerfile:

FROM ubuntu:16.04

RUN apt-get update \
    && apt-get upgrade -y \
    && apt-get install -y \
    git \
    make \
    python-pip \
    python2.7 \
    python2.7-dev \
    ssh \
    && apt-get autoremove \
    && apt-get clean

ARG password
ARG username
ENV password $password
ENV username $username

RUN pip install git+http://$username:[email protected]/scm/do/repo.git

I use the following commands to build the image from this Dockerfile:

docker build -t myimage:v1 --build-arg password="somepassoword" --build-arg username="someuser" .

However, in the build log the username and password that I pass as --build-arg are visible.

Step 8/8 : RUN pip install git+http://$username:[email protected]/scm/do/repo.git
 ---> Running in 650d9423b549
Collecting git+http://someuser:[email protected]/scm/do/repo.git

How to hide them? Or is there a different way of passing the credentials in the Dockerfile?


Solution

  • Update

    You know, I was focusing on the wrong part of your question. You shouldn't be using a username and password at all. You should be using access keys, which permit read-only access to private repositories.

    Once you've created an ssh key and added the public component to your repository, you can then drop the private key into your image:

    RUN mkdir -m 700 -p /root/.ssh
    COPY my_access_key /root/.ssh/id_rsa
    RUN chmod 700 /root/.ssh/id_rsa
    

    And now you can use that key when installing your Python project:

    RUN pip install git+ssh://[email protected]/you/yourproject.repo
    

    (Original answer follows)

    You would generally not bake credentials into an image like this. In addition to the problem you've already discovered, it makes your image less useful because you would need to rebuild it every time your credentials changed, or if more than one person wanted to be able to use it.

    Credentials are more generally provided at runtime via one of various mechanisms:

    • Environment variables: you can place your credentials in a file, e.g.:

      USERNAME=myname
      PASSWORD=secret
      

      And then include that on the docker run command line:

      docker run --env-file myenvfile.env ...
      

      The USERNAME and PASSWORD environment variables will be available to processes in your container.

    • Bind mounts: you can place your credentials in a file, and then expose that file inside your container as a bind mount using the -v option to docker run:

      docker run -v /path/to/myfile:/path/inside/container ...
      

      This would expose the file as /path/inside/container inside your container.

    • Docker secrets: If you're running Docker in swarm mode, you can expose your credentials as docker secrets.