Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to navigate up one folder in a dockerfile

I'm having some trouble building a docker image, because the way the code has been structured. The code is written in C#, and in a solution there is a lot of projects that "support" the application i want to build.

My problem is if i put the dockerfile into the root i can build it, without any problem, and it's okay but i don't think it's the optimal way, because we have some other dockerfiles we also need to build and if i put them all into the root folder i think it will end up messy.

So if i put the dockerfile into the folder with the application, how do i navigate into the root folder to grab the folders i need?

I tried with "../" but from my point of view it didn't seem to work. Is there any way to do it, or what is best practice in this scenario?

like image 872
Ginger Avatar asked Dec 17 '22 16:12

Ginger


1 Answers

TL;DR

run it from the root directory:

docker build . -f ./path/to/dockerfile


the long answer:

in dockerfile you cant really go up.

why

when the docker daemon is building you image, it uses 2 parameters:

  • your Dockerfile
  • the context

the context is what you refer to as . in the dockerfile. (for example as COPY . /app) both of them affect the final image - the dockerfile determines what is going to happen. the context tells docker on which files it should perform the operations you've specified in that dockerfile.

thats how the docs put it:

A build’s context is the set of files located in the specified PATH or URL. The build process can refer to any of the files in the context. For example, your build can use a COPY instruction to reference a file in the context.

so, usually the context is the directory where the Dockerfile is placed. my suggestion is to leave it where it belongs. name your dockerfiles after their role (Dockerfile.dev,Dockerfile.prod, etc) thats ok to have a few of them in the same dir.

the context can still be changed:

after all, you are the one that specify the context. since the docker build command accepts the context and the dockerfile path. when i run:

docker build .

i am actually giving it the context of my current directory, (ive omitted the dockerfile path so it defaults to PATH/Dockerfile)

so if you have a dockerfile in dockerfiles/Dockerfile.dev, you shoul place youself in the directory you want as context, and you run:

docker build . -f dockerfiles/Dockerfile.dev

same applies to docker-compose build section (you specify there a context and the dockerfile path)

hope that made sense.

like image 132
Efrat Levitan Avatar answered Jan 06 '23 18:01

Efrat Levitan