dockerdocker-compose

"Remove" a VOLUME in a Dockerfile


I have a Dockerfile extending FROM an image that declares a VOLUME. Is there a way to overwrite the VOLUME directive in such a way, that it "removes" the VOLUME?


Solution

  • Updated answer from 2024:

    As @thorfi suggested in his answer:

    You may use COPY --from now to copy the files from the postgres image to the Debian image it's based on (while leaving out the VOLUME directive (along with everything else too).

    Your final Dockerfile might look like this:

    FROM postgres:15-bookworm as postgres-docker
    
    FROM debian:bookworm-slim as postgres-build
    
    # We do this because the postgres-docker container has a VOLUME on /var/lib/postgresql/data
    # This copies the filesystem without bringing the VOLUME with it.
    COPY --from=postgres-docker / /
    
    # DO THE REST OF YOUR STUFF, e.g.:
    VOLUME /var/lib/postgresql
    
    # https://hub.docker.com/_/postgres - see 15-bookworm for the stuff we are setting here
    # Note: We need to set these because we are using COPY --from=postgres-docker - see above
    ENTRYPOINT ["docker-entrypoint.sh"]
    ENV LANG en_US.utf8
    ENV PG_MAJOR 15
    ENV PATH $PATH:/usr/lib/postgresql/${PG_MAJOR}/bin
    ENV PGDATA /var/lib/postgresql/data
    ENV SLEEP_ON_EXIT=0
    STOPSIGNAL SIGINT
    CMD ["postgres"]
    
    EXPOSE 5432
    

    GitHub issues on the matter:


    Original answer from 2017:

    No.

    The only way to do so, is if you clone Dockerfile of the image you use as base one (the one in FROM) and remove the VOLUME directive manually. Then build it and use in your FROM as base one.