sbt / sbt-native-packager

sbt Native Packager
https://sbt-native-packager.readthedocs.io/en/stable/
BSD 2-Clause "Simplified" License
1.59k stars 439 forks source link

COPY command unable to find files/dir on host machine #1478

Open masonedmison opened 2 years ago

masonedmison commented 2 years ago

Expected behaviour

I want to copy a directory from the host machine to the container using a command like

    dockerCommands := dockerCommands.value.flatMap {
      case Cmd("FROM", args @ _*) if args.contains("stage0") && args.contains("openjdk:11-jre-slim-buster") =>
        Seq(
          Cmd("FROM", args: _*),
          Cmd("COPY", "C:/Users/edmisml/scalaprojects/cici/certs/toKeyStore", "/certs")
        )

within my sbt.

Actual behaviour

when I docker:publishLocal it errors out at

[error]  > [stage0 2/8] COPY C:/Users/edmisml/scalaprojects/cici/certs/toKeyStore /certs:
[error] ------
[error] failed to compute cache key: "/C:/Users/edmisml/scalaprojects/cici/certs/toKeyStore" not found: not found

I thought that by inserting the COPY command after the initial FROM command (stage0) that I would indeed be able to copy files from the host machine but this does not appear to be the case.

Can someone help me out here?

Here's the out of show dockerCommands:

[info] * Cmd(FROM,WrappedArray(openjdk:11-jre-slim-buster, as, stage0))
[info] * Cmd(COPY,WrappedArray(C:/Users/edmisml/scalaprojects/cici/certs/toKeyStore, /certs))
[info] * Cmd(LABEL,WrappedArray(snp-multi-stage="intermediate"))
[info] * Cmd(LABEL,WrappedArray(snp-multi-stage-id="4961b11a-cbaa-4619-9759-2ef8e086bd75"))
[info] * Cmd(WORKDIR,WrappedArray(/opt/docker))
[info] * Cmd(COPY,WrappedArray(2/opt /2/opt))
[info] * Cmd(COPY,WrappedArray(4/opt /4/opt))
[info] * Cmd(USER,WrappedArray(root))
[info] * ExecCmd(RUN,List(chmod, -R, u=rX,g=rX, /2/opt/docker))
[info] * ExecCmd(RUN,List(chmod, -R, u=rX,g=rX, /4/opt/docker))
[info] * ExecCmd(RUN,List(chmod, u+x,g+x, /4/opt/docker/bin/cici-core))
[info] * DockerStageBreak
[info] * Cmd(FROM,WrappedArray(openjdk:11-jre-slim-buster, as, mainstage))
[info] * Cmd(USER,WrappedArray(root))
[info] * Cmd(RUN,List(id, -u, demiourgos728, 1>/dev/null, 2>&1, ||, ((, getent, group, 0, 1>/dev/null, 2>&1, ||, (, type, groupadd, 1>/dev/null, 2>&1, &&, groupadd, -g, 0, root, ||, addgroup, -g, 0, -S, root, )), &&, (, type, useradd, 1>/dev/null, 2>&1, &&, useradd, --system, --create-home, --uid, 1001, --gid, 0, demiourgos728, ||, adduser, -S, -u, 1001, -G, root, demiourgos728, ))))
[info] * Cmd(WORKDIR,WrappedArray(/opt/docker))
[info] * Cmd(COPY,WrappedArray(--from=stage0 --chown=demiourgos728:root /2/opt/docker /opt/docker))
[info] * Cmd(COPY,WrappedArray(--from=stage0 --chown=demiourgos728:root /4/opt/docker /opt/docker))
[info] * Cmd(EXPOSE,WrappedArray(8080))
[info] * ExecCmd(RUN,List(mkdir, -p, ./certs/toKeyStore))
[info] * ExecCmd(RUN,List(chown, -R, demiourgos728:root, ./certs/toKeyStore))
[info] * ExecCmd(VOLUME,List(./certs/toKeyStore))
[info] * ExecCmd(CMD,WrappedArray(mkdir, /certs/))
[info] * Cmd(COPY,WrappedArray(--from=stage0, /certs/, /certs/))
[info] * ExecCmd(RUN,WrappedArray(echo, $JAVA_HOME))
[info] * Cmd(USER,WrappedArray(root))
[info] * ExecCmd(RUN,WrappedArray(pwd))
[info] * ExecCmd(RUN,WrappedArray(ls, /))
[info] * ExecCmd(RUN,WrappedArray(ls, /certs))
[info] * ExecCmd(RUN,WrappedArray(keytool, -importcert, -alias, globalRoot, -trustcacerts, -keystore, $JAVA_HOME/jre/lib/security/cacerts, -storepass, changeit, -noprompt, -file, 
/opt/docker/certs/two.cer))
[info] * ExecCmd(RUN,WrappedArray(keytool, -importcert, -alias, globalRoot, -trustcacerts, -keystore, $JAVA_HOME/jre/lib/security/cacerts, -storepass, changeit, -noprompt, -file, 
/opt/docker/certs/one.cer))
[info] * Cmd(USER,WrappedArray(1001:0))
[info] * ExecCmd(ENTRYPOINT,List(/opt/docker/bin/cici-core))
[info] * ExecCmd(CMD,List())

Information

dwickern commented 2 years ago

Docker can only access files in its build context. So you will have to copy any additional files to Docker / stagingDirectory, otherwise Docker won't see them.

masonedmison commented 2 years ago

Is there a programmatic way to do this? It looks like by default the "stage" directory is <module>/target/docker/stage.

As another approach, I tried to add a mapping tupe using Docker / fileMappings but that did not seem to have any effect on the Dockerfile nor did it add anything to the staging directory.

dwickern commented 2 years ago

Try this:

Docker / dockerPackageMappings += file("/path/to/file.txt") -> "out.txt"