docker /bin/sh: 1: .: Can't open /path - docker

I have an sbt project projectA under home/demo/projectA my Dockerfile resides in /home/demo/ for some reason i don't want it to be inside projectA
so hierarchy looks like this
home/demo
Dockerfile
projectA
here i am trying to run sbt command in the image build process here is the contents of my Dockerfile
FROM hseeberger/scala-sbt:11.0.2_2.12.8_1.2.8 as stripecommon
MAINTAINER sara <sarawaheed3191#gmail.com>
WORKDIR /aa
RUN \
. /home/demo/projectA sbt
I am getting this error when building the image
:~/home/demo$ docker build -t testapp .
Sending build context to Docker daemon 1.297GB
Step 1/4 : FROM hseeberger/scala-sbt:11.0.2_2.12.8_1.2.8 as stripecommon
---> 349a7e4f4029
Step 2/4 : MAINTAINER sara <sarawaheed3191#gmail.com>
---> Using cache
---> 8603662d3730
Step 3/4 : WORKDIR /aa
---> Using cache
---> f07ec5bb4d34
Step 4/4 : RUN . /home/demo/projectA sbt
---> Running in 7509ee45f622
/bin/sh: 1: .: Can't open /home/demo/projectA
The command '/bin/sh -c . /home/demo/projectA sbt' returned a non-zero code: 2
what is the right way to do this also i am a beginner in docker help will be appreciated

You need to make sure that projectA exists inside the container. so for this either you pick code from github or copy it using COPY or ADD command. After that you can build it using sbt.

Related

building golang using your docker image

Good day everyone. I'm in the middle of a task using our docker image to build go microservice. To reduce build time, keep the images under control.
I expect that after downloading the golang image from the docker hub, upload it to our registry and use it in all the dockerfiles of our microservices.
As soon as I started building go mircoservice, during the installation phase go gave me an error:
11:04:16 [91m / bin / sh: go: not found
11:04:16 [0mThe command '/ bin / sh -c go install -mod vendor' returned a non-zero code: 127
I tried to install go in a container, write $ PATH for go, but nothing helped. Then I was told that this image already contains go.
Here are the complete build steps:
11:04:15 Step 1/7 : FROM 192.168.0.8:5000/golang
11:04:15 ---> 49f356fa4513
11:04:15 Step 2/7 : EXPOSE 80
11:04:15 ---> Using cache
11:04:15 ---> 6e0c49e600e6
11:04:15 Step 3/7 : WORKDIR /go/src/autocomplete-trains
11:04:15 ---> Using cache
11:04:15 ---> 41cccfb1ed82
11:04:15 Step 4/7 : COPY . .
11:04:15 ---> 1a44653382a8
11:04:15 Step 5/7 : RUN chmod +x ./start.sh
11:04:15 ---> Running in c13b5cfbfd0b
11:04:16 Removing intermediate container c13b5cfbfd0b
11:04:16 ---> ab03d13e5984
11:04:16 Step 6/7 : RUN go install -mod vendor
11:04:16 ---> Running in b3727493f938
11:04:16 [91m/bin/sh: go: not found
11:04:16 [0mThe command '/bin/sh -c go install -mod vendor' returned a non-zero code: 127
11:04:17 Build step 'Execute shell' marked build as failure
Dockerfile:
FROM 192.168.0.8:5000/golang
EXPOSE 80
WORKDIR /go/src/autocomplete-trains
COPY . .
RUN chmod +x ./start.sh
RUN go install -mod vendor
ENTRYPOINT ./script.sh
Note: If FROM 192.168.0.8:5000/golang:1.15.5-alpine is changed to FROM golang then everything works (but we don't wish to rely upon dockerhub so this is not a viable solution).
The process used to push the image into our repository was:
sudo docker pull golang
sudo docker tag golang:latest 192.168.0.8:5000/golang:1.15.5-alpine
sudo docker push 192.168.0.8:5000/golang:1.15.5-alpine

In Docker, can't cd into directory installed with "go get"?

I'm trying to build a Docker container which downloads the source of https://github.com/micromdm/scep and using the go get command and cds into the resulting directory in order to perform some follow-up commands (namely, make deps, make build, and COPYing the relevant resulting binary into the container's PATH).
So far I've tried this Dockerfile:
FROM golang:latest
ENV GO111MODULE on
RUN ["go", "get", "github.com/micromdm/scep"]
RUN cd $GOPATH/src/github.com/micromdm/scep
However, if I try to run this I get the following error:
> docker build . --tag loadtest
Sending build context to Docker daemon 4.608kB
Step 1/4 : FROM golang:latest
---> 52b59e9ead8e
Step 2/4 : ENV GO111MODULE on
---> Using cache
---> 28335bf0142b
Step 3/4 : RUN ["go", "get", "github.com/micromdm/scep"]
---> Using cache
---> 86760bf0c490
Step 4/4 : RUN cd $GOPATH/src/github.com/micromdm/scep
---> Running in b86fc3ab0ab8
/bin/sh: 1: cd: can't cd to /go/src/github.com/micromdm/scep
The command '/bin/sh -c cd $GOPATH/src/github.com/micromdm/scep' returned a non-zero code: 2
If I try the command without setting the GO111MODULE environment variable to on, I get his error:
> docker build . --tag loadtest
Sending build context to Docker daemon 4.608kB
Step 1/3 : FROM golang:latest
---> 52b59e9ead8e
Step 2/3 : RUN ["go", "get", "github.com/micromdm/scep"]
---> Running in 8cb54311a416
package github.com/micromdm/scep: no Go files in /go/src/github.com/micromdm/scep
The command 'go get github.com/micromdm/scep' returned a non-zero code: 1
Following https://github.com/ponzu-cms/ponzu/issues/204, I also tried running it with a ... at the end,
FROM golang:latest
RUN ["go", "get", "github.com/micromdm/scep/..."]
RUN cd $GOPATH/src/github.com/micromdm/scep \
but this results in
> docker build . --tag loadtest
Sending build context to Docker daemon 4.608kB
Step 1/3 : FROM golang:latest
---> 52b59e9ead8e
Step 2/3 : RUN ["go", "get", "github.com/micromdm/scep/..."]
---> Running in 961bae3bb455
# github.com/micromdm/scep/scep
src/github.com/micromdm/scep/scep/scep.go:318:17: p7.EncryptionAlgorithm undefined (type *pkcs7.PKCS7 has no field or method EncryptionAlgorithm)
src/github.com/micromdm/scep/scep/scep.go:449:26: too many arguments in call to pkcs7.Encrypt
src/github.com/micromdm/scep/scep/scep.go:449:53: undefined: pkcs7.WithEncryptionAlgorithm
src/github.com/micromdm/scep/scep/scep.go:542:26: too many arguments in call to pkcs7.Encrypt
src/github.com/micromdm/scep/scep/scep.go:542:54: undefined: pkcs7.WithEncryptionAlgorithm
The command 'go get github.com/micromdm/scep/...' returned a non-zero code: 2
Any ideas about how I could achieve downloading the package and cding into the resulting directory?
First of all, in docker you are building a container, so trying to cd into a directory in your pc its not possible. Use the WORKDIR command, also id recomend to git clone into a repository and there create the Dockerfile, and then COPY everything in there to a directory inside the docker for example:
COPY . /app
WORKDIR /app/where_you_want_to_cd
RUN whatever_you_want_to_run
You can set this path to be your WORKDIR, and the follow up command would be easy.
From the docker builder reference, each RUN command is run independently. So doing RUN cd does not have any effect on the next RUN command.
The following Dockerfile works:
FROM golang:latest
ENV GO111MODULE on
RUN go get github.com/micromdm/scep
WORKDIR /go/src/github.com/micromdm/scep
You need here the GO111MODULE since in the go.mod of that module isn't defined the go version, so you need to activate it yourself.
It turns out that the resulting code is located not in /go/src, but in /go/pkg.

Go 1.11 unknown import path for own package in Docker build

I am migrating some code to work with Go 1.11 modules, and I am able to build it from the shell but not in Docker.
Relevant Dockerfile sections:
WORKDIR /goscout
COPY ["go.mod", "go.sum", "./"]
RUN GO111MODULE=on go get -u=patch
COPY *.go ./
RUN GO111MODULE=on go build -v -ldflags "-linkmode external -extldflags -static" -o GoScout -a .
When Docker is running the last command in the above excerpt, I get this error:
can't load package: package github.com/triplestrange/StrangeScout/goscout: unknown import path "github.com/triplestrange/StrangeScout/goscout": ambiguous import: found github.com/triplestrange/StrangeScout/goscout in multiple modules:
github.com/triplestrange/StrangeScout/goscout (/goscout)
github.com/triplestrange/StrangeScout v0.3.0 (/go/pkg/mod/github.com/triplestrange/!strange!scout#v0.3.0/goscout)
I don't get this in the shell, so I'm guessing I am not copying some files correctly. But before this command runs I have copied go.mod, go.sum, and *.go, so I don't know what could be missing.
Make sure that you initialized modules properly for your project
go mod init github.com/triplestrange/StrangeScout/goscout
so that the content of your go.mod is
module github.com/triplestrange/StrangeScout/goscout
And then you can use your current Dockerfile without any changes.
There is no need to set GO111MODULE=on since you're running go commands outside of the $GOPATH
➜ docker build -t goscout .
Sending build context to Docker daemon 47.1kB
Step 1/11 : FROM golang:latest AS builder
---> fb7a47d8605b
Step 2/11 : WORKDIR /goscout
---> Running in e9786fe5ab53
Removing intermediate container e9786fe5ab53
---> 6d101e346175
Step 3/11 : COPY ./ ./
---> 7081c0b47dc9
Step 4/11 : RUN go get -d -v ./...
---> Running in 3ce69359ae88
go: finding github.com/go-sql-driver/mysql v1.4.0
go: finding github.com/gorilla/mux v1.6.2
go: downloading github.com/gorilla/mux v1.6.2
go: downloading github.com/go-sql-driver/mysql v1.4.0
Removing intermediate container 3ce69359ae88
...
---> 3df0dbca80e5
Successfully built 3df0dbca80e5
Successfully tagged goscout:latest

Docker Copy Command fails

I am new to Docker , while running the Docker file
My Docker file
FROM microsoft/dotnet:2.2.0-preview1-aspnetcore-runtime AS base
WORKDIR /app
FROM microsoft/dotnet:2.2.100-preview1-sdk AS build
WORKDIR /DineshVisualStudio/Autofac-interceptor/AutofacModule/Autofac.interface.ConcactFactory
COPY Autofac.Interface.ConcatFactory.csproj project/
WORKDIR /Autofac-interceptor/project
RUN dotnet restore
COPY /Autofac.interface.Concactfactory .
RUN dotnet build -c Release -o /app
FROM build AS publish
RUN dotnet publish -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["Autofac.Interface.ConcatFactory.exe"]
When running docker build -t myappfactory . it's failing. I tried many combinations for the path in COPY command but without any luck.
I am using Visual Studio 2017 and had installed Docker Tools too
This is my folder structure
My folder structure with the code having docker file
when running docker build -t autofacinterface . I'm getting an Error saying:
failed to create file . System cannot find the file specified.
I am using Cmd to build the docker from my current working .csproj folder.
D:\DineshVisualStudio\Autofac-Interceptor\AutofacModule\Autofac.Interface.ConcatFactory>docker build -t autofacinterfaceconcatfactory .
Sending build context to Docker daemon 4.608kB
Step 1/15 : FROM microsoft/dotnet:2.2.0-preview1-aspnetcore-runtime AS base
---> 2df5940c47f7
Step 2/15 : WORKDIR /app
---> Using cache
---> f4d2190d9b44
Step 3/15 : FROM microsoft/dotnet:2.2.100-preview1-sdk AS build
---> af242cb10bf0
Step 4/15 : WORKDIR /DineshVisualStudio/Autofac-
interceptor/AutofacModule/Autofac.interface.ConcactFactory
---> Using cache
---> dbf15787395b
Step 5/15 : COPY /Autofac.Interface.ConcatFactory.csproj project/
COPY failed: CreateFile \\?\C:\ProgramData\Docker\tmp\docker-
builder138146052\COPY: The system cannot find the file specified.
after #Mike suggestion I am getting this issue:
D:\DineshVisualStudio\Autofac-Interceptor\AutofacModule\Autofac.Interface.ConcatFactory>docker build -t myappfact .
Step 6/15 : WORKDIR /Autofac-interceptor/project
Step 7/15 : RUN dotnet restore
---> Running in 9e91df3e68a3
MSBUILD : error MSB1003: Specify a project or solution file. The current
working directory does not contain a project or solution file.
The command 'cmd /S /C dotnet restore' returned a non-zero code: 1
I don't think you need the leading / in COPY /Autofac.Interface.ConcatFactory.csproj. The source file(s) are relative to current working directory, and you've already called WORKDIR previously.
https://docs.docker.com/engine/reference/builder/#copy
Step 5/15 : COPY COPY
Do you have the word COPY twice in a row in your Dockerfile?
The command "COPY /Autofac.Interface.ConcatFactory.csproj project/" failed. I recommend you read more about the command COPY here. Use the command "COPY . ." if you run docker build from the folder "D:\DineshVisualStudio\Autofac- Interceptor\AutofacModule\Autofac.Interface.ConcatFactory".

How to flatten a Docker image?

I made a Docker container which is fairly large. When I commit the container to create an image, the image is about 7.8 GB big. But when I export the container (not save the image!) to a tarball and re-import it, the image is only 3 GB big. Of course the history is lost, but this OK for me, since the image is "done" in my opinion and ready for deployment.
How can I flatten an image/container without exporting it to the disk and importing it again? And: Is it a wise idea to do that or am I missing some important point?
Now that Docker has released the multi-stage builds in 17.05, you can reformat your build to look like this:
FROM buildimage as build
# your existing build steps here
FROM scratch
COPY --from=build / /
CMD ["/your/start/script"]
The result will be your build environment layers are cached on the build server, but only a flattened copy will exist in the resulting image that you tag and push.
Note, you would typically reformulate this to have a complex build environment and only copy over a few directories. Here's an example with Go to make a single binary image from source code and a single build command without installing Go on the host and compiling outside of docker:
$ cat Dockerfile
ARG GOLANG_VER=1.8
FROM golang:${GOLANG_VER} as builder
WORKDIR /go/src/app
COPY . .
RUN go-wrapper download
RUN go-wrapper install
FROM scratch
COPY --from=builder /go/bin/app /app
CMD ["/app"]
The go file is a simple hello world:
$ cat hello.go
package main
import "fmt"
func main() {
fmt.Printf("Hello, world.\n")
}
The build creates both environments, the build environment and the scratch one, and then tags the scratch one:
$ docker build -t test-multi-hello .
Sending build context to Docker daemon 4.096kB
Step 1/9 : ARG GOLANG_VER=1.8
--->
Step 2/9 : FROM golang:${GOLANG_VER} as builder
---> a0c61f0b0796
Step 3/9 : WORKDIR /go/src/app
---> Using cache
---> af5177aae437
Step 4/9 : COPY . .
---> Using cache
---> 976490d44468
Step 5/9 : RUN go-wrapper download
---> Using cache
---> e31ac3ce83c3
Step 6/9 : RUN go-wrapper install
---> Using cache
---> 2630f482fe78
Step 7/9 : FROM scratch
--->
Step 8/9 : COPY --from=builder /go/bin/app /app
---> Using cache
---> 5645db256412
Step 9/9 : CMD /app
---> Using cache
---> 8d428d6f7113
Successfully built 8d428d6f7113
Successfully tagged test-multi-hello:latest
Looking at the images, only the single binary is in the image being shipped, while the build environment is over 700MB:
$ docker images | grep 2630f482fe78
<none> <none> 2630f482fe78 6 days ago 700MB
$ docker images | grep 8d428d6f7113
test-multi-hello latest 8d428d6f7113 6 days ago 1.56MB
And yes, it runs:
$ docker run --rm test-multi-hello
Hello, world.
Up from Docker 1.13, you can use the --squash flag.
Before version 1.13:
To my knowledge, you cannot using the Docker api. docker export and docker import are designed for this scenario, as you yourself already mention.
If you don't want to save to disk, you could probably pipe the outputstream of export into the input stream of import. I have not tested this, but try
docker export red_panda | docker import - exampleimagelocal:new
Take a look at docker-squash
Install with:
pip install docker-squash
Then, if you have a image, you can squash all layers into 1 with
docker-squash -f <nr_layers_to_squash> -t new_image:tag existing_image:tag
A quick 1-liner that is useful for me to squash all layers:
docker-squash -f $(($(docker history $IMAGE_NAME | wc -l | xargs)-1)) -t ${IMAGE_NAME}:squashed $IMAGE_NAME
Build the image with the --squash flag:
https://docs.docker.com/engine/reference/commandline/build/#squash-an-images-layers---squash-experimental
Also consider mopping up unneeded files, such as the apt cache:
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

Resources