Being new to docker files. I am trying to understand how docker build is working here.
I currently have a folder that has two files in it. The folder is called foo
The structure of the folder is as follows. It has two files in it the first file is main.go
and the other file next to it is Dockerfile
as shown in the diagram below
Foo
|_main.go
|_go.mod
|_go.sum
|_Dockerfile
My Dockerfile currently looks like this and the command docker build -t ..
is called from inside the folder Foo.
FROM golang:1.18-alpine AS builder
# Create and change to the app directory.
WORKDIR /app
COPY go.* ./
RUN go mod download
# Copy local code to the container image.
COPY . ./
RUN go build -v -o my app
....
Now this is a fairly simple Dockerfile. Here is what I understand.
1- golang:1.18-alpine is the base image
2- In the container a folder called app
will be created
3- go.mod
and go.sum
will be copied to ./
path of the container (probably home) of the container.
4- Run go mod download
will be called from inside the /app
folder correct ?
My question is basically for no. 4. go mod download
called from inside /app
folder ? If so how does that work because from my understanding is that the /app
folder is so far empty ? When did the app folder get populated ? In the statement COPY go.* ./
what path is ./
is that the home ?
CodePudding user response:
In line WORKDIR /app
, your current path will be set to /app
. If the directory don't exist then it will be created beforehand.
Next COPY go.* ./
, this matches all of files start with go.
will be copied to /app
directory in the docker container. So your docker /app
should look like this :
/app
| go.mod
| go.sum
Again with COPY . ./
, you are copying all files from current directory to /app
directory of your docker container. It will replace already existing files in the contrainer. /app
will look like this:
/app
| main.go
| go.mod
| go.sum
| Dockerfile
Last with RUN go build -v -o myapp
, you are building the app using go
and saving binary file myapp
.