My goal is to compile some code using maven from my project directory on my docker client machine using docker. (mvn compile
running in a docker container).
Assume my maven image is called mvn-image
and my project directory is project-dir
.
If I had the docker host running on the same machine as the docker client - then I could mount a volume with something similar to:
mvn -v /projects/project-dir:/workdir -i mvn-image mvn compile
But here is the tricky bit. My docker client is on a different machine to the host machine. I'm trying not to build and and push and run a image built from my project directory - I want the convenience of the docker one-liner.
I also know I can run the container - and do a cp
to get the files in there. But with that I still don't get the one-liner that I would with the docker-copy mount volume.
My question is: **How to get docker run to take the directory from the client machine to the host container? **
Sending local data is not really a feature of docker run
. Other commands like docker build
, docker import
and docker cp
are able to send local data across from client to host. docker run
can send stdin to the server though.
A docker build
will actually run on the remote host your client points at. The "build context" is sent across and then everything is run remotely. You can even run the maven build as part of the build:
FROM mvn-image
COPY . /workdir
RUN mvn compile
CMD ls /workdir
Then run
docker build -t my-mvn-build .
You end up with an image on the remote host with your build in it. There's no local-build/push/remote-build steps.
docker run
can do standard Unix IO, so piping something into a command running in a container works like so:
tar -cf - . | docker run -i busybox sh -c 'tar -xvf -; ls -l /'
I'm not sure how that's going to work with the mvn
command that you supplied to run a container, under the normal Docker client it would be something like:
tar -cf - -C /projects/project-dir . | \
docker run mvn-image mvn compile sh -c 'tar -xvf - -C /workdir; mvn compile'
Otherwise you could mount the data on the host from the client somehow. There are NFS and sshfs storage plugins.
Using rsync
or something similar to send your project to the Docker host would be a lot more efficient over time.
The docker cp
is a fairly simple solution though. If you put it in a script, yet get a "one liner" to run.
#!/bin/sh
set -uex
cid=$(docker create mvn-image mvn compile)
docker cp /projects/project-dir $cid/workdir
docker start $cid
docker logs -f $cid