COPY failed while using docker - docker

Im building a express app but when i use the command sudo docker build - < Dockerfile i get the error COPY failed: file not found in build context or excluded by .dockerignore: stat package.json: file does not exist.
This is how my proyect structure looks like:
.
├── build
│   ├── server.js
│   └── server.js.map
├── Dockerfile
├── esbuild.js
├── package.json
├── package-lock.json
├── Readme.md
└── src
├── index.ts
├── navigate.ts
├── pages.ts
├── responses
│   ├── Errors.ts
│   └── index.ts
└── server.ts
And this is my Dockerfile content
FROM node:14.0.0
WORKDIR /usr/src/app
RUN ls -all
COPY [ "package.json", \
"./"]
COPY src/ ./src
RUN npm install
RUN node esbuild.js
RUN npx nodemon build/server.js
EXPOSE 3001
CMD ["npm", "run", "serve", ]
At the moment of run the command, im located in the root of the project.

Related

Building a Docker container for Golang code: package PACKAGE_NAME is not in GOROOT

I built a small Golang application and I want to run it on a Docker container.
I wrote the following Dockerfile:
# syntax=docker/dockerfile:1
FROM golang:1.16-alpine
WORKDIR /app
COPY go.mod ./
COPY go.sum ./
RUN go mod download
COPY ./* .
RUN go env -w GO111MODULE=on
RUN go build -o /docker-gs-ping
EXPOSE 8080
CMD [ "/docker-gs-ping" ]
However, when I run the command:
docker build --tag docker-gs-ping .
I get the errors:
#16 0.560 found packages controllers (controller.go) and repositories (csv_file_repository.go) in /app
#16 0.560 main.go:4:2: package MyExercise/controllers is not in GOROOT (/usr/local/go/src/MyExercise/controllers)
I want to mention that the package controllers exists in my working directory and all files associated with this directory are placed in MyExercise/controllers folder.
Do you know how to resolve this error?
Edit:
This is the directory tree:
.
├── Dockerfile
├── REDAME
├── controllers
│   └── controller.go
├── go.mod
├── go.sum
├── logging
│   └── logger.go
├── main.go
├── models
│   └── location.go
├── output.log
├── repositories
│   ├── csv_file_repository.go
│   ├── csv_file_repository_builder.go
│   ├── csv_file_repository_builder_test.go
│   ├── csv_file_repository_test.go
│   ├── repository_builder_interface.go
│   ├── repository_interface.go
│   └── resources
│   └── ip_address_list.txt
└── services
├── ip_location_service.go
├── ip_location_service_test.go
├── rate_limiter_service.go
├── rate_limiter_service_interface.go
├── rate_limiter_service_test.go
└── time_service.go
import section in main.go:
import (
"MyExercise/controllers"
"MyExercise/logging"
"MyExercise/repositories"
"MyExercise/services"
"errors"
"github.com/gin-gonic/gin"
"os"
"strconv"
"sync"
)
Do go mod vendor in your app directory. Documentaion.
For build the container docker build -t app:v1 .
Dockerfile
FROM golang:1.16-alpine
WORKDIR /app/
ADD . .
RUN go build -o /app/main
EXPOSE 5055
CMD [ "/app/main" ]
There is actually an issue with your Dockerfile.
COPY ./* .
does not actually do what you think. It will copy all files recursively in a flat structure to the /app directory.
Modify your Dockerfile to something like:
# syntax=docker/dockerfile:1
FROM golang:1.16-alpine
WORKDIR /app
ADD . /app
RUN go mod download
RUN go env -w GO111MODULE=on
RUN go build -o /docker-gs-ping
EXPOSE 8080
CMD [ "/docker-gs-ping" ]
Basically, remove all of the COPY directives and replace with a single ADD directive

How to dockerzise beego application

I tried to Dockerize a Beego application, but the HTML rendering is not finding HTML files stored inside the view/templates directory.
FROM golang:1.13
WORKDIR /go/src/fileUpload
COPY . .
RUN go get -d -v ./...
RUN go install -v ./...
EXPOSE 8080
# Install server application
CMD ["go", "run", "./main/main.go"]
You could try to set the directory containing the templates inside the Docker image.
beego.BConfig.WebConfig.ViewsPath = "myviewpath"
https://beego.me/docs/mvc/view/view.md#template-directory
Edit: directory structure
It is difficult to answer the question, as the directory layout is not clear. However, I can give an example based on quickstart:
export GOPATH="$HOME/go/src"
bee new quickstart
In $GOPATH/src/quickstart/Dockerfile:
FROM golang:1.13
WORKDIR /go/src/quickstart
COPY . .
RUN go get -d -v ./...
RUN go install -v ./...
EXPOSE 8080
# Install server application
CMD ["go", "run", "main.go"]
Note that I do not have a directory (./main) in front of main.go. This is what the structure of the app looks like:
tim#sky:~/go/src/quickstart$ tree
.
├── conf
│ └── app.conf
├── controllers
│ └── default.go
├── Dockerfile
├── main.go
├── models
├── routers
│ └── router.go
├── static
│ ├── css
│ ├── img
│ └── js
│ └── reload.min.js
├── tests
│ └── default_test.go
└── views
└── index.tpl
If the views directory in you app is in a different place, you need to add the correct path to main.go as described in my initial answer.

How to copy a folder from a dockerfile's parent into workdir

So I have a tree that looks like this:
.
├── README.md
├── dataloader
│   ├── Dockerfile
...
│   ├── log.py
│   ├── logo.py
│   ├── processors
...
│   └── tests
├── datastore
│   ├── datastore.py
and the Dockerfile inside the dataloader application looks like this:
FROM python:3.7
WORKDIR /var/dsys-2uid-dataloader
COPY assertions/ ./assertions/
COPY events/ ./events/
COPY processors/ ./processors/
COPY requirements.txt ./
<*>COPY datastore/ ./datastore/
COPY *.py ./
RUN pip3 install -r requirements.txt
ENTRYPOINT ["python", "dataloader.py"]
the line with the asterisk doesn't work since the datastore folder is in the parent of the Dockerfile. What can be done? I need this Dockerfile to be correct because it's going to be used as the image in the kubernetes deployment.
You can't access a file outside of your build context, but you can "trick" docker to be in a different build context.
Just run docker build -t foo -f dataloader/Dockerfile . from the root directory (where you have the README and the dirs)
$ tree
.
├── bar
│   └── wii
└── foo
└── Dockerfile
2 directories, 2 files
$ cat foo/Dockerfile
FROM ubuntu
COPY bar/wii .
$ docker build -t test -f foo/Dockerfile .
Sending build context to Docker daemon 3.584kB
Step 1/2 : FROM ubuntu
---> cf0f3ca922e0
Step 2/2 : COPY bar/wii .
---> c3ff3f652b4d
Successfully built c3ff3f652b4d
Successfully tagged test:latest

Docker COPY no such file or directory

Building docker image fails on copy task. No such file or directory. I am using the hello world example from spring
Building from openjdk:8-jdk-alpine
Run echo ${PWD} prints /
Run ls prints a set of normal directories (/usr /var etc) but no project files are present
Why is docker not using the WORKING directory?
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG DEPENDENCY=target/dependency
COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","hello.Application"]
Files to copy are prepared by gradle and i can confirm that they are present:
task unpack(type: Copy) {
dependsOn bootJar
from(zipTree(tasks.bootJar.outputs.files.singleFile))
into("build/dependency")
}
I am running
docker build .
docker gradle task
docker {
name "${project.group}/${bootJar.baseName}"
copySpec.from(tasks.unpack.outputs).into("dependency")
buildArgs(['DEPENDENCY': "dependency"])
}
Problem
docker build -t springio/gs-spring-boot-docker .
Sending build context to Docker daemon 16.78MB
Step 1/8 : FROM openjdk:8-jdk-alpine
---> a3562aa0b991
Step 2/8 : RUN addgroup -S spring && adduser -S spring -G spring
---> Using cache
---> 5b122a1db135
Step 3/8 : USER spring:spring
---> Using cache
---> 3bdba2028e42
Step 4/8 : ARG DEPENDENCY=target/dependency
---> Using cache
---> 616945cc41ed
Step 5/8 : COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY failed: file not found in build context or excluded by .dockerignore: stat target/dependency/BOOT-INF/lib: file does not exist
Fix
mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar)
The below folder layout will be created.
This is done to explain the layering options for your docker image. It is all explained in Example 3. Dockerfile
$pwd
/java-training/workspace/gs-spring-boot-docker/complete
$tree -L 3 target/dependency
target/dependency
├── BOOT-INF
│   ├── classes
│   │   ├── application.yml
│   │   └── hello
│   ├── classpath.idx
│   └── lib
│   ├── jackson-annotations-2.11.0.jar
│   ├── jackson-core-2.11.0.jar
│   ├── jackson-databind-2.11.0.jar
│   ├── jackson-datatype-jdk8-2.11.0.jar
│   ├── jackson-datatype-jsr310-2.11.0.jar
│   ├── jackson-module-parameter-names-2.11.0.jar
│   ├── jakarta.annotation-api-1.3.5.jar
│   ├── jakarta.el-3.0.3.jar
│   ├── jul-to-slf4j-1.7.30.jar
│   ├── log4j-api-2.13.2.jar
│   ├── log4j-to-slf4j-2.13.2.jar
│   ├── logback-classic-1.2.3.jar
│   ├── logback-core-1.2.3.jar
│   ├── slf4j-api-1.7.30.jar
│   ├── snakeyaml-1.26.jar
│   ├── spring-aop-5.2.6.RELEASE.jar
│   ├── spring-beans-5.2.6.RELEASE.jar
│   ├── spring-boot-2.3.0.RELEASE.jar
│   ├── spring-boot-autoconfigure-2.3.0.RELEASE.jar
│   ├── spring-boot-starter-2.3.0.RELEASE.jar
│   ├── spring-boot-starter-json-2.3.0.RELEASE.jar
│   ├── spring-boot-starter-logging-2.3.0.RELEASE.jar
│   ├── spring-boot-starter-tomcat-2.3.0.RELEASE.jar
│   ├── spring-boot-starter-web-2.3.0.RELEASE.jar
│   ├── spring-context-5.2.6.RELEASE.jar
│   ├── spring-core-5.2.6.RELEASE.jar
│   ├── spring-expression-5.2.6.RELEASE.jar
│   ├── spring-jcl-5.2.6.RELEASE.jar
│   ├── spring-web-5.2.6.RELEASE.jar
│   ├── spring-webmvc-5.2.6.RELEASE.jar
│   ├── tomcat-embed-core-9.0.35.jar
│   └── tomcat-embed-websocket-9.0.35.jar
├── META-INF
│   ├── MANIFEST.MF
│   └── maven
│   └── org.springframework
└── org
└── springframework
└── boot
Now you can build it!
 docker build -t springio/gs-spring-boot-docker .
Sending build context to Docker daemon 33.44MB
Step 1/8 : FROM openjdk:8-jdk-alpine
---> a3562aa0b991
Step 2/8 : RUN addgroup -S spring && adduser -S spring -G spring
---> Using cache
---> 5b122a1db135
Step 3/8 : USER spring:spring
---> Using cache
---> 3bdba2028e42
Step 4/8 : ARG DEPENDENCY=target/dependency
---> Using cache
---> 616945cc41ed
Step 5/8 : COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
---> Using cache
---> 1d1404d8b0e7
Step 6/8 : COPY ${DEPENDENCY}/META-INF /app/META-INF
---> Using cache
---> ad8c03fa8c4e
Step 7/8 : COPY ${DEPENDENCY}/BOOT-INF/classes /app
---> Using cache
---> 2aaadf41ccd2
Step 8/8 : ENTRYPOINT ["java","-cp","app:app/lib/*","hello.Application"]
---> Using cache
---> a21e7f0c88fc
Successfully built a21e7f0c88fc
Successfully tagged springio/gs-spring-boot-docker:latest
Best of luck!
You're getting a "no such file or directory" error, and it looks like that's the truth.
The Dockerfile sets:
ARG DEPENDENCY=target/dependency
And then attempts a COPY operation:
COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
If you resolve ${DEPENDENCY}, that COPY command look like:
COPY target/dependency/BOOT-INF/lib /app/lib
And there is no target directory in the repository. Maybe this is something you're supposed to create by following the tutorial? From that document:
This Dockerfile has a DEPENDENCY parameter pointing to a directory where we have unpacked the fat jar. If we get that right, it already contains a BOOT-INF/lib directory with the dependency jars in it, and a BOOT-INF/classes directory with the application classes in it. Notice that we are using the application’s own main class hello.Application (this is faster than using the indirection provided by the fat jar launcher).
Run this inside your project
mkdir target/dependency
(cd target/dependency; jar -xf ../*.jar)
I had the same problem. for solving this:
before execute your
docker build
command, just run this to commands in root of your project (where Dockerfile and target exist):
mkdir target/dependency
(cd target/dependency; jar -xf ../*.jar)
or,
2. just add those two command in your .gitlab-ci.yml file (if using gitlab CI/CD pipeline):
docker-build:
stage: package
tags:
- vasci2_shell_runner
script:
- mkdir target/dependency
- (cd target/dependency; jar -xf ../*.jar)
- docker build -t nexus.css.ir:30005/vas/harim/apc:lastest .

golang docker build for linux

I need to compile a Golang application for Linux and I can't cross-compile under Mac, because of another library. So I decided to compile within a Docker container. This is my first time to use Docker.
This is my current directory structure:
.
├── Dockerfile
├── Gopkg.lock
├── Gopkg.toml
├── Vagrantfile
├── bootstrap.sh
├── src
│   ├── cmd
│   │   ├── build.bat
│   │   ├── build.sh
│   │   ├── config.json
│   │   ├── readme.md
│   │   └── server.go
│   ├── consumers.go
│   ├── endpoints
│   │   ├── json.go
│   │   ├── rate.go
│   │   ├── test_payment.go
│   │   └── wallet.go
│   ├── middleware
│   │   └── acl.go
│   ├── models.go
│   ├── network
│   │   └── network.go
│   ├── qr
│   │   └── qr.go
│   ├── router
│   │   └── router.go
│   ├── service
│   │   └── walletService.go
│   ├── services.go
│   ├── setup.sql
│   ├── store
│   │   └── wallet.go
│   ├── stores.go
│   └── wallet
│   ├── coin.go
│   └── ethereum.go
Dockerfile:
FROM golang:latest
WORKDIR /src/cmd
RUN ls
RUN go get github.com/go-sql-driver/mysql
RUN go build -o main ./src/cmd/server.go
CMD ["./main"]
I try to build the Docker image with:
docker build -t outyet .
This is the error it returns:
Sending build context to Docker daemon 5.505MB
Step 1/6 : FROM golang:latest
---> d0e7a411e3da
Step 2/6 : WORKDIR /src/cmd
---> Using cache
---> 0c4c2b99e294
Step 3/6 : run ls
---> Using cache
---> 23d3e491a2e1
Step 4/6 : RUN go get github.com/go-sql-driver/mysql
---> Running in f34447e51f6c
Removing intermediate container f34447e51f6c
---> 5731ab22ee43
Step 5/6 : RUN go build -o main server.go
---> Running in ecc48fcf5488
stat server.go: no such file or directory
The command '/bin/sh -c go build -o main server.go' returned a non-zero code: 1
How i can build my Golang application with docker?
The error you're seeing is:
stat server.go: no such file or directory
The command '/bin/sh -c go build -o main server.go' returned a non-zero code: 1
... and in other words, it's telling you that the Go compiler can't find server.go which you're trying to build. Update your Dockerfile to copy your local files into the Docker image:
FROM golang:latest
COPY . /go/src/workdir
WORKDIR /go/src/workdir
RUN go build -o main ./src/cmd/server.go
CMD ["/go/src/workdir/main"]
In your directory I spotted Gopkg.toml which is a dependency manifest used by dep. dep will use a directory called vendor to include all dependencies for your Go project. Before you build the Docker image, you need to ensure all dependencies are present with dep ensure:
$ dep ensure
$ docker build -t outyet .
You need to add your sources in your build container. Add this line in your Dockerfile (after FROM golang:latest for example) :
ADD src/ /src/cmd
Then, you could access files inside the container (/src/cmd) : RUN ls should now return something.

Resources