Hi,
using TestContainers 1.4.3 on Windows 10. Trying to start a GenericContainer from a Dockerfile with:
```
@ClassRule
public static GenericContainer jee = new GenericContainer(
new ImageFromDockerfile("foo").withFileFromPath("Dockerfile", Paths.get("Dockerfile")))
The Dockerfile is in the same Maven module from which the test is run, and it is very simple:
FROM jboss/wildfly:10.1.0.Final
ADD target/my-cantina-frontend-0.0.1-SNAPSHOT.war /opt/jboss/wildfly/standalone/deployments/
However, when I try to run such test, I get:
Caused by: com.github.dockerjava.api.exception.DockerClientException: Could not build image: ADD failed: stat /var/lib/docker/tmp/docker-builder682250974/target/my-cantina-frontend-0.0.1-SNAPSHOT.war: no such file or directory
at com.github.dockerjava.core.command.BuildImageResultCallback.getImageId(BuildImageResultCallback.java:79)
at com.github.dockerjava.core.command.BuildImageResultCallback.awaitImageId(BuildImageResultCallback.java:51)
at org.testcontainers.images.builder.ImageFromDockerfile.resolve(ImageFromDockerfile.java:141)
at org.testcontainers.images.builder.ImageFromDockerfile.resolve(ImageFromDockerfile.java:30)
at org.testcontainers.utility.LazyFuture.getResolvedValue(LazyFuture.java:20)
at org.testcontainers.utility.LazyFuture.get(LazyFuture.java:27)
at org.testcontainers.containers.GenericContainer.getDockerImageName(GenericContainer.java:770)
```
I have no clue of where that /var/lib/docker/tmp/docker-builder682250974/ is coming from. Anyway, it looks like the Docker image is not built against the current directory the tests are run from. Having absolute paths in the Dockerfile is not really possible. Is there any way to make it work? Or is it a bug? The documentation at https://www.testcontainers.org/usage/dockerfile.html is not particularly clear on this regard.
many thanks
Hi @arcuri82,
Add .withFileFromPath("target/my-cantina-frontend-0.0.1-SNAPSHOT.war", Paths.get("target/my-cantina-frontend-0.0.1-SNAPSHOT.war") if you want to refer it from your Dockerfile.
Also, you don't need ImageFromDockerfile here at all. Just use:
@ClassRule
public static GenericContainer jee = new GenericContainer("jboss/wildfly:10.1.0.Final")
.withFileSystemBind("target/my-cantina-frontend-0.0.1-SNAPSHOT.war", "/opt/jboss/wildfly/standalone/deployments/app.war", BindMode.READ_ONLY);
You also have to keep in mind, that Docker for Windows is still running in it's own VM (moby linux on Hyper-V). So the Docker daemon will only be able to access the files from your Windows filesystem if you've configured volume sharing in your Docker settings.
OK, thanks, seems to work now. So, if I understood it well, the Docker image is built in a different folder (or even in a different VM), and one has to use methods like .withFileFromPath to copy files (needed by the Dockerfile) over that directory. If that is correct, a couple of sentences in the documentation to clarify it would help :) thx
@arcuri82 yes :)
https://www.testcontainers.org/usage/dockerfile.html
ImageFromDockerfile accepts arbitrary files, strings or classpath resources to be used as files in the build context. At least one of these needs to be a Dockerfile
The following methods may be used to provide the Dockerfile and any other required files:
We already have a few sentences about it, do you think we need to be more explicit about the files?
Also, AFAIR @rnorth added an ability to copy folders, not just files, not sure tho
I would add something like: "When generating a Docker image from an existing Dockerfile on disk, the image is not built in the same folder as the working directory of the running tests. As such, all relative paths for the files inside the Dockefile would not resolve correctly. You need to copy over all the needed files by using methods like withFileFromPath"
Another related thing: I have been using TestContainers to run configurations from existing docker-compose.yml files. And those work just fine, even with relative paths inside those files. It is bit confusing that building images from Dockerfile instead of Compose behave differently (ie, having to manually specify all the needed extra files)
If I have in Dockerfile:
ADD some.tar.gz /some/path
how can I do this with testcontainer, assuming that I expect tar.gz to be extracted into this dir?
withFileFromPath() doesn't seem to work
AFAIK you use withFileFromPath()to configure your build context, not for emulating ADD some.tar.gz /some/path. So withFileFromPath("some.tar.gz", "some.tar.gz") should work in this case, since the ADD command will do the extraction.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe this is a mistake, please reply to this comment to keep it open. If there isn't one already, a PR to fix or at least reproduce the problem in a test case will always help us get back on track to tackle this.
This issue has been automatically closed due to inactivity. We apologise if this is still an active problem for you, and would ask you to re-open the issue if this is the case.
Most helpful comment
I would add something like: "When generating a Docker image from an existing Dockerfile on disk, the image is not built in the same folder as the working directory of the running tests. As such, all relative paths for the files inside the Dockefile would not resolve correctly. You need to copy over all the needed files by using methods like
withFileFromPath"