I am trying to build docker image and I don't want...
# general
s
I am trying to build docker image and I don't want to use cache is there any way I can do that? • Also I wanna see the logs of each instruction running in docker_image target, how to do that? • How to add .env file while creating docker_image or how to add environment variables from .env file.
c
Hi Kelvin, If you refer to the CACHE lines in there.. that's from Docker.. and it's using a previously built layer when available. If you don't want that, one way around it is to first clear all your layers before building:
docker rmi -a
something like that.. been a long time since I used docker now.
Pants doesn't stream process outputs unfortunately, so the docker build logs will only be presented after the fact, and only on failure unless you run pants with
-ldebug
.
For
.env
you can add that to your image using a
file
target:
Copy code
# BUILD
file(name="env", source=".env")
docker_image(..., dependencies=[":env"], instructions=[..., "COPY .env ..."])
There are a number of ways to set environment variables, I suggest reading the docs for more details: https://www.pantsbuild.org/docs/docker
s
@curved-television-6568 Thanks for reply, I have tried above way
@curved-television-6568 This is what I am getting when I try above method. It says it cannot find that .env file. Only that statement is not working other statement is working fine.
c
There are a couple of things to check. Run with
--keep-sandboxes=on_failure
that way you can inspect the Dockerfile, .dockerignore and the build context provided for the docker build command. By looking in the
__run.sh
see if all files and arguments are as expected, to find the root of the issue.
s
I am not using
docker build
I am using
pants package docker_image
command and where can I see build context or __run.sh file ?
@curved-television-6568 I am able to successfully run the command and working fine just wanted to know how can I give relative path of parent directory from BUILD file for file?
file(name='env', source='../../.env')
This gives error below: InvalidTargetException: apps/auth/BUILD34 The 'source' field in target apps/auth:env should not include
../
patterns because targets can only have sources in the current directory or subdirectories. It was set to ../../.env. Instead, use a normalized literal file path (relative to the BUILD file).
What does normalized liletarl file path means?
šŸ‘€ 1
How can I give path for .env in BUILD file in side auth app. Without using absolute path
I think I got it.
@curved-television-6568 Thanks for support.
šŸ‘ 1
c
What does normalized liletarl file path means?
It's just another way of saying this (reiterating to make a point, potentially more confusing):
targets can only have sources in the current directory or subdirectories.
šŸ‘ 1
let us know if you run into more trouble šŸ™‚
s
Yup I created BUILD file at root level for env and from that I am using .env file as it is shared between multiple apps.
b
Also I wanna see the logs of each instruction running in docker_image target, how to do that?
The
build_verbose
option allows this (although, as Andreas notes, the output isn't streamed, just shown in a batch at the end): https://www.pantsbuild.org/docs/reference-docker#build_verbose
I am trying to build docker image and I don't want to use cache is there any way I can do that?
With Pants 2.18, the
build_no_cache
option allows this: https://www.pantsbuild.org/docs/reference-docker#build_no_cache
šŸ‘ 1