Search code examples
dockerdockerfile

docker build purely from command line


Is there a way to build docker containers completely from the command-line? Namely, I need to be able to set things like FROM, RUN and CMD.

I'm a scenario where I have to use docker containers to run everything (git, npm, etc), and I'd like to build containers on the fly that have prep-work done (such as one with npm install already run).

There are lots of different cases, and it'd be overkill to create an actual Dockerfile for each. I'd like to instead be able to just create command-line commands in my script instead.


Solution

  • Update for 2017-05-05: Docker just released 17.05.0-ce with this PR #31236 included. Now the above command creates an image:

    $ docker build -t test-no-df -f - . <<EOF
    FROM busybox:latest
    CMD echo just a test
    EOF
    Sending build context to Docker daemon  23.34MB
    Step 1/2 : FROM busybox:latest
     ---> 00f017a8c2a6
    Step 2/2 : CMD echo just a test
     ---> Running in 45fde3938660
     ---> d6371335f982
    Removing intermediate container 45fde3938660
    Successfully built d6371335f982
    Successfully tagged test-no-df:latest
    

    The same can be achieved in a single line with:

    $ printf 'FROM busybox:latest\nCMD echo just a test' | docker build -t test-no-df -f - .
    

    Original Response

    docker build requires the Dockerfile to be an actual file. You can use a different filename with:

    docker build -f Dockerfile.temp .
    

    They allow the build context (aka the . or current directory) to be passed by standard input, but attempting to pass a Dockerfile with this syntax will fail:

    $ docker build -t test-no-df -f - . <<EOF
    FROM busybox:latest
    CMD echo just a test
    EOF
    
    unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /home/bmitch/data/docker/test/-: no such file or directory