#Can't access files when run inside Docker?

280 messages · Page 1 of 1 (latest)

nimble saffron
#

using this library https://github.com/joho/godotenv

err := godotenv.Load("/app/cmd/app/db.env") // if false, load the envs.
        if err != nil {
            dir, _ := os.Getwd()
            log.Printf("Currently on the directory> %s", dir)
            log.Fatal("Error loading .env file", err)
}

so uh. here is a code snippet.

be ready, this is stupid.

COPY --from=modules /go/pkg /go/pkg
COPY . /app
WORKDIR /app
RUN ls -la /app/cmd/app

when I run this Dockerfile while building the docker-compose,

RUN ls -la /app/cmd/app

prints

drwxr-xr-x    5 root     root          4096 Nov 22 19:20 .
drwxr-xr-x    3 root     root          4096 Nov 22 18:05 ..
-rw-r--r--    1 root     root      12111346 Nov 22 18:05 app
drwxr-xr-x    3 root     root          4096 Nov 22 18:05 data
-rw-r--r--    1 root     root           137 Nov 22 19:17 db.env
drwxr-xr-x    4 root     root          4096 Nov 22 18:05 logs
-rw-r--r--    1 root     root          4374 Nov 22 18:05 main.go
drwxr-xr-x    2 root     root          4096 Nov 22 18:05 textfiles

so db.env is there! so this should work right! right?

err := godotenv.Load("/app/cmd/app/db.env") // if false, load the envs.

no it doesnt. it panics at loading env. with an error of ```go
gmud | 2022/11/22 19:41:37 Currently on the directory> /
gmud | 2022/11/22 19:41:37 Error loading .env fileopen /app/cmd/app/db.env: not a directory

sacred orchid
#

Aren't you inside app? shouldn't it be /cmd/app/db.env ?

#

also confusing with the two app directories

sacred orchid
robust briar
#

yeah I think SHA is right you prob just dont have ur path right

nimble saffron
nimble saffron
#

like i tried it

sacred orchid
#

Also the ls shows everything being in the same dir and not being inside one each other

#

This is so confusing do you have 3 app dirs?

robust briar
#

isnt main.go being ran in /app/cmd/app so the relative directory is /app/cmd/app so the db.env file should just be in the current directory? so Load("db.env") i could be wrong im new tbh

sacred orchid
#

Can you post the whole directory structure?

nimble saffron
#

Let me refactor this a bit

robust briar
#

your structure prob should just be /app/cmd

#

i think

nimble saffron
#

so docker's app folder -> cmd -> app -> db.env

#

shit this is confusing.

sacred orchid
#

lol

robust briar
#

fr

sacred orchid
#

maybe without the leading /?

nimble saffron
#

so there is app executable in app folder of cmd that is in docker's app folder but i am not talking about that

sacred orchid
#

try cmd/app/db.env or app/cmd/app/db.env

#

I think the former will work

nimble saffron
#

shot 2 fail

#

lets see shot 1

robust briar
#

I figured the program would be based off the relative directory where the main.go file is being ran but im prob wrong

nimble saffron
#

shot 1 also fails god dingus

robust briar
#

yeah

#

try it

fast trellis
#

/cmd/cmd/

robust briar
#

i dont think the function is like a package import where u have to declare like the whole structure

#

but who knows im prob wrong ;c

nimble saffron
#

i will do something stupid

#

brb

#

okay I dont know how the

#

fuck

#

code ended up there

#
dir, _ := os.Getwd()
            log.Printf("Currently on the directory> %s", dir)
            files, err := ioutil.ReadDir("./")
            for _, f := range files {
                log.Println(f.Name())
            }
``` prints that.
robust briar
#

well arent u like

#

forgive me since I don't use these techs but
COPY . /app

#

arent u basically saying copy the contents to /app

fast trellis
#

/app/app/db.env

nimble saffron
#

jxsl is deciphering the structure

robust briar
#

so ur file structure is /app/cmd/app

#

idk .-.

nimble saffron
#

app/cmd/app/db.env didnt work

fast trellis
#

if you had a trailing slash COPY . /app/

robust briar
#

did just db.env work?

fast trellis
#

it would put your stuff inside the folder

tardy hatch
#

Shouldnt you create the directory before ?

#

Run mkdir /path/to/directory

nimble saffron
nimble saffron
fast trellis
#

I would just start the docker image with a shell loop that just sleeps

#

image/container

#

and exec into the container

tardy hatch
#

Oh i know why

#

Docker is build upon layer

#

I got sometimes the case when i needed to create the files and use them on the same layer

fast trellis
#

CMD [ "/bin/sh", "-c", "while true; do sleep 5; done;" ]

nimble saffron
nimble saffron
#

or should I delete that line?

fast trellis
#

i just added a slash at the beginning

#

add it into your dockerfile at the end

#

start the container

#

and exec into the container

robust briar
#

what does the trailing slash do? I thought its already taking the contents of his local root directory . and putting it into a folder called /app

fast trellis
#

I think in this case the trailing slash does not matter D:. Just a thought...

nimble saffron
tardy hatch
#

Oh wait

#

It's a permission issue ?

#

No

nimble saffron
#

its ran with sudo

robust briar
#

did just regular 'db.env' not work damacansu?

nimble saffron
robust briar
#

ah

fast trellis
#

build and start

#

docker exec -it containername /bin/sh

nimble saffron
#

kk

#

what should I set this mf?

#

app/cmd/app/db.env?

robust briar
nimble saffron
#

^^^^^^^^ also this.

robust briar
#

doesnt docker compose already handle this stuff

tardy hatch
#

Btw you shouldn't load env variables from .env in prod

fast trellis
#

oh

nimble saffron
fast trellis
#

docker compose up -d --build --force-recreate .

robust briar
fast trellis
#

something like this

tardy hatch
#

He can still exec through a docker compose ?

#

I don't see the problem

#

lol

fast trellis
#

he can exec in any running container

robust briar
#

isnt his issue just not knowing the path to his .env?

fast trellis
#

docker compose just allows to orchestrate multiple containerd and the network between those containers

#

containers

#

can you post the output of docker ps

nimble saffron
#

running dis

#

sec

robust briar
fast trellis
#

well, an exec into the running container would suffice in case it actually runs

tardy hatch
#

its docker-compose

#

docker-compose is a seperate program

#

not part of the standard docker cli

fast trellis
#

new versions of docker support

#

the subcommand

#

compose

#

so docker compose works

tardy hatch
#

Oh okay

fast trellis
#

depending on the version

tardy hatch
#

But he ddidnt specify a folder ?

fast trellis
#

current dir by default I'd guess

tardy hatch
#

Well better be sure lol

nimble saffron
#

as other one panics, only DB container is alive

#

i will try to remove panic parts

fast trellis
#

docker ps -a

nimble saffron
#

well i just realized i have a container named cunt

#

let me clear these shit

tardy hatch
#

average linux user

#

lol

robust briar
#

well ima take a backseat to this then cuz I thought his issue was just the path to his .env

#

so im lost why we getting into his container stuff

nimble saffron
fast trellis
#

gotte get the container running with an infinite shell loop in order to exec into it and look at the actual path

tardy hatch
#

docker logs

fast trellis
#

can you post the content of your docker-compose.yaml

robust briar
#

yeah but hes been getting the ls of things via docker compose

robust briar
#

code

nimble saffron
fast trellis
#

can you replace command: "./app"

robust briar
#

wait

#

this was his working directory

#

for his container

tardy hatch
#

Yep

#

./app

robust briar
tardy hatch
#

not /app

robust briar
#

??

nimble saffron
#

wait

#

HOLY FUCK

robust briar
#

isnt code where his stuff is?

#

or am i wrong

#

how tf does he have app and code in the same directory ._.

#

i think his docker compose and dockerfile are just confusing the shit out of me rn

fast trellis
#

command: [ "/bin/sh", "-c", "while true; do sleep 5; done;" ]

#

to start the container with an infinite loop

tardy hatch
#

and code is the directory

#

of the volume

nimble saffron
#

jesus what the fuck is this

robust briar
#

mounting one with the docker compose and one with the dockerfile

nimble saffron
tardy hatch
#

One doesn't hinder the other

fast trellis
#

ah

#

well, scratch does not have a shell

robust briar
fast trellis
#

I'd suggest using alpine instead of scratch containers

#

cuz it has a shell ._.

#

the app volume does not seem to define any locations. I guess it's mounting an empty folder at /code

robust briar
nimble saffron
#

dude wait

fast trellis
#

should be in the same folder

#

otherwise build: . would not work

robust briar
#

¯_(ツ)_/¯

tardy hatch
nimble saffron
#

okay so wait.

robust briar
#

why is mounting two folders good practice

nimble saffron
fast trellis
#

no

#

in your Dockerfile

nimble saffron
#

fekl

fast trellis
#

you use FROM scratch

#

use FROM alpine:latest

#

then docker compose up -d --build --force-recreate

#

and lastly

#

docker ps
and
docker exec -it containername /bin/sh

#

where did I even see the from scratch ._.

nimble saffron
#

okay this mf is alive

fast trellis
#

where is the Dockerfile content 😂

nimble saffron
#

okay you are alive

fast trellis
#

it's just an infinite loop, not your app running

nimble saffron
#

yeah ikr

fast trellis
#

ls

#

and look inside your folders

fast trellis
nimble saffron
#

yeah i am just

#

looking.

fast trellis
#

at that point I didn't know you used docker compose

tardy hatch
robust briar
fast trellis
#

volumes inside a dockerfile don't mount anything if I'm not mistaken

#

is that what you mean?

#

they indicate that you can mount stuff at those locations

nimble saffron
#

i am still looking for my files

#

holy hell

fast trellis
#

lol

#

cd /app

#

ab

#

wait

#

😂

nimble saffron
#

BRO HAHAHH

fast trellis
#

isn't your app called /app

nimble saffron
#

wait i am inside the app

fast trellis
#

everyrhing is called app, lol

nimble saffron
#

hahahahah

#

shit is

#

ridicilous

#

okay so where is my copied files

fast trellis
#

did you escape the container through some volume mount and now you are on your host filesystem?

nimble saffron
#

no i am not that drunk yet

robust briar
#

/app/app/app/app/app

fast trellis
#

it's either there or not

#

there is not really much to look for

nimble saffron
#

its really hard when you see your monitor twice

#

wait a second

fast trellis
#

rip

nimble saffron
#

okay this will avoid confusions

fast trellis
#

perfect condition for programming

#

rip

robust briar
#

/app/app2/app3/app4/app5

fast trellis
#
# Step 1: Modules caching
FROM golang:1.19-alpine3.16 as modules
COPY go.mod go.sum /modules/
WORKDIR /modules
RUN go mod download

# Step 2: Builder
FROM golang:1.19-alpine3.16 as builder
COPY --from=modules /go/pkg /go/pkg
COPY . /app
WORKDIR /app
RUN ls -la /app/cmd/app
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
    go build -tags migrate -o /bin/app ./cmd/app
FROM scratch
COPY --from=builder /bin/app /app
CMD ["/app"]
#

cooy from builder

nimble saffron
#

okay this is where we started from

fast trellis
#

actually puts your applicarion at the position /app

#

/app becomes the executable

#

so mounting stuff at /app

#

rips your executable

#

that's what I meant with trailing slash

#

in the beginning

#

the /app has no trailing slash, so you put the executable at that position.

#

don't mount stuff at /app

#

mount it at /data

#

or so

#

my second suggestion would be: do not bake your config files inside of your docker image but mount them through sich a /data or /config folder

#

so what you do is, you create a folder /app

#

put stuff inside

#

and then overwrite the folder with your executable you actually omit everything from the previous build stage and only put the executable in your final container

#

and then mount stuff over your executable

nimble saffron
#

wait i have a solution

fast trellis
#
version: '3'
services:
  db:
    image: 'mysql/mysql-server:latest'
    container_name: mysql
    build:
      context: .
      dockerfile: ./db.Dockerfile
    ports:
      - '127.0.0.1:3306:3306'
    volumes:
      - 'db_data:/var/lib/mysql'
    restart: on-failure
    command:
      - mysqld
      - '--default-authentication-plugin=mysql_native_password'
    environment:
      # Change this later.
      - MYSQL_ROOT_PASSWORD=caner31313131
    networks:
      - dev
    healthcheck:
      test:
        - CMD
        - mysqladmin
        - ping
        - '-h'
        - localhost
      timeout: 60s
      retries: 10
  mud:
    container_name: gmud
    build: .
    ports:
      - "2222:2222"
      - "8081:8081"
    volumes:
    - app:/code
    command: "./app"
    environment:
      - WAIT_HOSTS=mysql:3306
      - WAIT_HOSTS_TIMEOUT=300
      - WAIT_SLEEP_INTERVAL=30
      - WAIT_HOST_CONNECT_TIMEOUT=30
    depends_on:
      - db
    networks:
      - dev
volumes:
  db_data:
  app:
networks:
    dev:
      driver: bridge
nimble saffron
#

dude you are way too helpful, can I offer you a beer?

fast trellis
#

thanks 😄 but not on weekdays

nimble saffron
#

oh god

#

oh finally.

fast trellis
#

don't bake configs info your docker image

#

the Dockerfile was pretty good

#

instead of scratch I'd suggest using alpine, tho

#

and then in your docker-compose.yaml

#

you would define a proper volume mount

#

that mounts your db.env at your expected location

#

your current volume does not define a source location

#

it just defines a volume name which is called "app"

nimble saffron
#

I will consider all of these

nimble saffron
#

and thanks to everyone contributed in this emotional rollercoaster thread

#

i launched the code and... my computer is not running anymore

#

at least we solved the problem bois!

fast trellis
#

since docker compise 3 volumes have gotten weird but yeah

nimble saffron
#

I will get used to it eventually

fast trellis
#

I guess you can use stuff like
volumes:
- ./cmd/app/db.env:/config/db.env

#

on the level of your container, not the global volumes sectio

nimble saffron
#

and seems more understandable

#

*readable