Reputation: 26527
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.
Upvotes: 8
Views: 9514
Reputation: 5254
On Windows using PowerShell the following works using -f-
(see Build context: Local context with Dockerfile from stdin):
@'
FROM mcr.microsoft.com/dotnet/sdk:8.0
RUN apt-get update && apt-get install -y clang zlib1g-dev
'@ | docker build -f- -t 'mcr.microsoft.com/dotnet/sdk:8.0-aot' .
Upvotes: 0
Reputation: 1297
Build an image from STDIN with no context:
docker build -t myimage:latest -<<EOF
FROM busybox
RUN echo "hello world"
EOF
https://docs.docker.com/build/building/context/#text-files
Upvotes: 1
Reputation: 263469
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
Upvotes: 13