Cannot find module DOCKER RUN - docker

I am trying to run a simple docker image with a index.js file in my /build folder with a simple console.log('Hello world')
My docker file is:
FROM node:alpine
WORKDIR /test/build
COPY ./ ./
CMD node index.js
I create the image in my directory with:
docker build -t hello-docker .
Then I run:
docker run hello-docker
But I keep on getting the error:
cannot find module /test/build/index.js
How can I solve this?

Capturing my comment as an answer for posterity - since the docker build command will be run from the application's root, the index file will end up under the build directory. I.e., the application should be run using:
CMD node ./build/index.js

Related

Docker does not build image

I just got started with docker and was following fireship's tutorial, however I encountered a problem when I ran the docker build command. I was expecting a similar output as the video (timestamp). Instead, I got the following:
Dockerfile
FROM node:12
WORKDIR /app #maybe its this? There is no /app directory
COPY package*.json ./
RUN npm install
COPY . .
ENV PORT=8080
EXPOSE 8080
CMD ["npm","start"]
docker build command
docker build -t <my docker id>/firstapp:1.1 .
File tree
D:/
Code/
testing/
Docker/
test1/
Notes
Yes, I have a docker ID.
The docker build command stopped at COPY . ., rather than finishing at CMD ["npm","start"]
CWD (Windows): D:\Code\testing\Docker\test1
I ran the docker build command twice
Questions
Why is this happening?
How can it be fixed?
The reason this occurs is because fireship is running Linux (like Hans Kilian noted) and I am running Windows. To get your image ID, run the docker images command, which will list your images w/ their image ID.
However, I am still not sure why it does not complete all 8 steps.

write dockerfile for multiple go.mod project

There is a library writing in Go. Its folder structure is:
lib/
golang/
go.mod
lib.go
example/
golang/
proto/
protofile
go.mod
main.go
Dockerfile
example is folder to show how to use this lib, so it has a main.go which can build and run. In order to use the lib, the example/golang/go.mod is:
module golang
go 1.15
require (
lib/golang v0.0.0
other stuff...
)
replace lib/golang => ../../golang
Now I want to pack the runnable example into a docker image, the Dockerfile is:
FROM golang:1.15
WORKDIR /go/src/app
COPY . .
RUN go env -w GO111MODULE=auto
RUN go env -w GOPROXY=https://goproxy.io,direct
RUN go get -d -v ./...
RUN go install -v ./...
CMD ["app"]
Then I cd into example/golang run docker build -t example ., the error log is
open /go/golang/go.mod: no such file or directory
It seems can not access lib/go.mod file, then I cd into lib folder and run docker build -t server -f examples/golang/Dockerfile ., however this will affect the import of main.go:
import "golang/proto"
error log is:
golang/proto: unrecognized import path "golang/proto"
How should I fix this to make the docker image?
==========================================
After I spend some time to read docker doc, here is the summary about this problem:
docker build command follow a PATH argument, that is the dot . at the end. That control the content of building, it decides what files docker build can access. So the reason of first error is that the pwd is lib/example/golang, and docker build command path is ., then docker build can not access parent files of lib/example/golang and they are required in main.go as a lib.
the command should be: docker build -t example ../../ However, docker build seek Dockerfile only in root of content path, so use -f to tell it where the Dockerfile located: docker build -t example -f ./Dockerfile ../../
if pwd is lib/, command is docker build -t server -f examples/golang/Dockerfile .
Be short:
If the dockerfile is not located at project root path, use -f and PATH of docker build command to give it enough access of files. If using go module, make sure PATH contain a go.mod file
If main.go is located in sub folder, make sure the WORKDIR in dockerfile same as the sub folder after COPY all need, or else go build or go install fail to compile
Your Dockerfile is too nested. Since your go build relies on relative paths - paths that are in parent directories - a docker build . will not see any parent-directory files.
Move the Dockerfile to the top e.g.
Dockerile
lib/
and update to build the nested build directory:
FROM golang:1.15
WORKDIR /go/src/app
# just need to copy lib tree
COPY lib .
# working from here now
WORKDIR /go/src/app/lib/golang/example/golang
RUN go env -w GO111MODULE=auto
RUN go env -w GOPROXY=https://goproxy.io,direct
RUN go get -d -v ./...
RUN go install -v ./...
CMD ["app"]
You can use go mod vendor before to build with Docker, it will centralise all your mod in vendor folder.
I did a a new file called build.sh with this inside:
#! /bin/sh
go mod vendor
docker build . -t myapp/myservice
rm -rf ./vendor
and run it whenever i need to build and by deleting vendor i can still use go run *.go with fresh version of my libraries

Not able to create docker image of .net core web API - COPY failed: stat /var/lib/docker/tmp/../netcoreapp2.1/publish

I'm trying to build an image of a .netcore v2.2 WebAPI app from docker file. But I'm getting a "no such file or directory" error while I try to build the image using following command:
docker build -t helloworld .
I have both microsoft/dotnet "latest" and "2.2-aspnetcore-runtime" base images installed in my machine.
Here's my docker file
FROM microsoft/dotnet
RUN mkdir /app
COPY ./bin/Release/netcoreapp2.2 /app
WORKDIR /app
ENTRYPOINT ["dotnet", "/app/HelloWorld.dll"]
# ASP.NET Core: Kestrel should listen on all IPs
ENV ASPNETCORE_URLS="http://0.0.0.0:5000"
Here's the folder structure of my solution (I tried the command inside the src):
Here's the output directory
Here's the full error details from the docker:
COPY failed: stat /var/lib/docker/tmp/docker-builder896068669/bin/Release/netcoreapp2.2: no such file or directory
you may change ./bin/Release/netcoreapp2.2 to ./bin/release/netcoreapp2.2
folder names are case sensitive
It seems the file ./bin/Release/netcoreapp2.2/publish does not exists on the machine on which you are running docker build
When you run docker build -t helloworld . it will copy all the file in . (i.e. the current directory from which you run the command) to the build context which will then be used when running COPY instructions, so you need to make sure you are running your command from the proper directory where files will be available to COPY
You can also run the same command from another directory by specifying the path to be used as build context such as docker build -t helloworld /path/to/my/dir

COPY command failing when running DOCKER BUILD

I'm trying to create a Docker image but it's not working. I'm trying to run this Docker command from the CLI and I'm getting the following error:
(from the <repo root>/src/Services/Accounts/Accounts.Api)
> docker build .
Error message:
Step 7/18 : COPY ["src/Services/Accounts/Accounts.Api/Accounts.Api.csproj", "src/Services/Accounts/Accounts.Api/"]
COPY failed: stat /var/lib/docker/tmp/docker-builder937056687/src/Services/Accounts/Accounts.Api/Accounts.Api.csproj: no such file or directory
and here's my Dockerfile
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
RUN ls -al
COPY ["src/Services/Accounts/Accounts.Api/Accounts.Api.csproj", "src/Services/Accounts/Accounts.Api/"]
COPY ["src/Services/Accounts/Accounts.Api/NuGet.config", "src/Services/Accounts/Accounts.Api/"]
RUN dotnet restore "src/Services/Accounts/Accounts.Api/Accounts.Api.csproj"
COPY . .
WORKDIR "/src/src/Services/Accounts/Accounts.Api"
RUN dotnet build "Accounts.Api.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "Accounts.Api.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Hornet.Accounts.Api.dll"]
I've also tried running this from the repo root directory:
> docker build .\src\Services\Accounts\Accounts.Api
Why is it trying to find my files in some /var/lib/docker/tmp/blah folder?
Further info:
Windows 10 OS
Docker CE with Linux Containers
VSCode
I think we can solve this by clarifying how to use the build context and how to specify the location of the Dockerfile.
The Docker build command usage looks like this:
docker build [OPTIONS] PATH
Your build context is the location that you define with PATH, Docker can use the files in the build context for the build. (You cannot use files outside the build context for the build.)
In the Dockerfile in your COPY statements you are specifying the source files' location relative to the root of your repo. This implies that
You should run your build command from the <root of your repo> with PATH . like this:
docker build .
You have not specified the path of your Dockerfile in the question, but it seems to me that it's in a subfolder of your repo. To make your build work issue the docker build command from the <root of your repo> like this:
docker build -f <path to Dockerfile> .
Your Dockerfile must be in the build context.
I think getting the build context and the location of the Dockerfile right will fix your build.
Also remember to use the -t flag to tag your image.
Why is it trying to find my files in some /var/lib/docker/tmp/blah folder?
As you probably know, Docker uses a Linux VM under the hood in the Docker for Windows app to develop Linux based containers. This location simply refers to the location in the VM.

Ubuntu Docker - php composer build from the official Dockerfile fails, COPY failed Step 9/12 stat no such file or directory

I want to build a docker image of a composer from this page composer:latest and taking exactly this Dockerfile
but when I do this in console:
$ wget -O Dockerfile https://raw.githubusercontent.com/composer/docker/edf4f0abf50da5d967408849434b9053a195b65f/1.7/Dockerfile
$ docker build -t mycomposer:latest .
i got this build error:
Step 9/12 : COPY docker-entrypoint.sh /docker-entrypoint.sh COPY
failed: stat
/var/lib/docker/tmp/docker-builder787686173/docker-entrypoint.sh: no
such file or directory
How come I have any error building from official Dockerfile?
This way works:
$ docker pull composer:latest
but I need to build an image basing on a local Dockerfile instead of just pulling it.
The command
COPY docker-entrypoint.sh /docker-entrypoint.sh
in the Dockerfile tries to copy the file docker-entrypoint.sh from your current directory.
However you have only downloaded the Dockerfile.
If you visit the actual directory on the repository you will notice another file entitled
docker-entrypoint.sh. If you download this file too and place it in the same directory
as the Dockerfile the image will be built without errors.

Resources