mbieren
mbieren

Reputation: 1118

Docker Volume point to host Directory in Dockerfile

I have the following Dockerfile :

FROM jboss/wildfly
USER jboss
RUN mkdir -p /opt/jboss/wildfly/standalone/log
VOLUME /opt/jboss/wildfly/standalone/log 
CMD /bin/bash
# CMD true

This resulting image is started with docker run -ti --name=data_volume data/volume. The next Dockerfile

FROM jboss/wildfly
RUN sed -i 's|<file relative-to="jboss.server.log.dir" 
    path="server.log"/>|\<file relative-to="jboss.server.log.dir" 
    path="\${jboss.host.name}-server.log"/\>|' 
    /opt/jboss/wildfly/standalone/configuration/standalone.xml

overrides the logging of the resulting jboss to log to "servername"-server.log in the logging dir. When I start the resulting image with docker run -ti --name=wild-01 --volumes-from=data_volume my/wildfly and docker run -ti --name=wild-02 --volumes-from=data_volume my/wildfly I have two log files in my data_colume container. So fine so good.

I would like to point my volume to a directory on the host eg. /var/log/wildfly.

How can I achieve this in Dockerfiles and not with the -v parameter when running data/volume

Thanks a lot in advance

Upvotes: 1

Views: 1539

Answers (2)

Scott Stensland
Scott Stensland

Reputation: 28285

I would like to point my volume to a directory on the host eg. /var/log/wildfly.

remove all mention of volumes from your Dockerfile ... launch your container using

docker run -d -v /var/log/wildfly:/var/log/wildfly  your-image-name

then in your code just reference the normal path

/var/log/wildfly

Your syntax to launch the container using docker run -ti makes the container shell interactive whereas -d is the normal mode to spin it up as a daemon running in the background

Upvotes: 0

samprog
samprog

Reputation: 2634

Inside dockerfiles you can only define volumes in /var/lib/docker/volumes. This is because every host can be different from the other.

Docker uses /var/lib/docker as "docker area" where it stores all docker-related data. It's the directory that's guaranteed on every host because it gets created on installation.

If you were to point out a volume in the dockerfile, let's say to /home/mbieren/docker_vol, the image would result in multiple errors when executed on a different host, as that directory does not exist and the user probably has insufficient permissions to create it.

Docker goes around that problem by not allowing custom mount-paths to be set in the dockerfile.

Upvotes: 1

Related Questions