无法读取Dockerfile MacOS GoLang;尝试创建一个镜像。

huangapple go评论75阅读模式
英文:

failed to read Dockerfile MacOS GoLang; Trying to create an image

问题

我在最后一行遇到了这个错误,并尝试将Dockerfile放在tmp文件夹中,但仍然得到相同的错误;我甚至尝试将Dockerfile的文件名改为大写和小写,但问题没有解决。

我的文件路径是Users/maheenk/Desktop/infra

我的Dockerfile:

#syntax=docker/dockerfile:1

#使用最新版本
FROM golang:latest

WORKDIR /app

#下载必要的Go模块
COPY go.mod ./
COPY go.sum ./
RUN go mod download

#将源代码复制到镜像中
COPY *.go ./

#在根文件系统中创建静态应用程序
RUN docker build -t internhw:latest -f .Dockerfile .

EXPOSE 8080

#当镜像用于启动容器时执行的命令
CMD ["internhw"]

我在终端中运行的命令:

docker build -t internhw:latest -f .Dockerfile .

终端的输出是:

[+] Building 0.1s (2/2) FINISHED                                                                                                       
 => [internal] load build definition from .Dockerfile                                                                             0.1s
 => => transferring dockerfile: 2B                                                                                                0.0s
 => [internal] load .dockerignore                                                                                                 0.0s
 => => transferring context: 2B                                                                                                   0.0s
failed to solve with frontend dockerfile.v0: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount200403056/.Dockerfile: no such file or directory

终端中运行命令ls -al的输出:

total 160
drwxrwxr-x@   9 maheenk  staff    288 Apr 24 11:42 .
drwxrwxrwx@ 110 maheenk  staff   3520 Apr 23 17:24 ..
-rw-r--r--@   1 maheenk  staff   6148 Apr 24 11:42 .DS_Store
drwxr-xr-x   14 maheenk  staff    448 Apr 24 11:00 .git
-rw-r--r--@   1 maheenk  staff    523 Apr 24 10:54 Dockerfile
-rw-r--r--    1 maheenk  staff    669 Apr 23 16:26 go.mod
-rw-r--r--    1 maheenk  staff  56262 Apr 23 16:26 go.sum
-rw-r--r--    1 maheenk  staff   2126 Apr 23 16:09 main.go

我在文件夹中添加了一个.Dockerfile(实际上是将Dockerfile复制并重命名为前面加了一个点):

total 160
drwxrwxr-x@   9 maheenk  staff    288 Apr 24 13:03 .
drwxrwxrwx@ 111 maheenk  staff   3552 Apr 24 12:43 ..
-rw-r--r--@   1 maheenk  staff   6148 Apr 24 13:03 .DS_Store
-rw-r--r--@   1 maheenk  staff    513 Apr 24 13:06 .Dockerfile
drwxr-xr-x   14 maheenk  staff    448 Apr 24 13:00 .git
-rw-r--r--@   1 maheenk  staff    523 Apr 24 10:54 Dockerfile
-rw-r--r--    1 maheenk  staff    669 Apr 23 16:26 go.mod
-rw-r--r--    1 maheenk  staff  56262 Apr 23 16:26 go.sum
-rw-r--r--    1 maheenk  staff   2126 Apr 23 16:09 main.go

更新后的版本

按照建议,我重新修改了Docker中的运行行:

#syntax=docker/dockerfile:1

FROM golang:latest

WORKDIR /app

#下载必要的Go模块
COPY go.mod ./
COPY go.sum ./
RUN go mod download

#将源代码复制到镜像中
COPY *.go ./

#在根文件系统中创建静态应用程序
RUN build -t internhw:latest .Dockerfile .

EXPOSE 8080

#当镜像用于启动容器时执行的命令
CMD ["internhw"]

当我在终端中运行docker build -t internhw:latest .Dockerfile .时,输出是:

"docker build"需要正好1个参数。
请参阅'docker build --help'。

用法:docker build [OPTIONS] PATH | URL | -
从Dockerfile构建镜像

<details>
<summary>英文:</summary>

I keep running into that error on the last line and have tried putting the dockerfile in the tmp folder but still get the same error; I even tried upper casing and lowercasing the docker file name, it didn&#39;t resolve the issue.

my file path is `Users/maheenk/Desktop/infra`

my docker file :
```sh
#syntax=docker/dockerfile:1

#using latest version
FROM golang:latest

WORKDIR /app

#Download necessary Go modules
COPY go.mod ./
COPY go.sum ./
RUN go mod download

#copies source code onto image
COPY *.go ./

#static application in rootfilesystem
RUN docker build -t internhw:latest -f .Dockerfile .

EXPOSE 8080

#command to execute when image is used to start a container
CMD [ &quot;internhw&quot;]

the command I run in terminal:

docker build -t internhw:latest -f .Dockerfile .

what the output is in terminal:

[+] Building 0.1s (2/2) FINISHED                                                                                                       
 =&gt; [internal] load build definition from .Dockerfile                                                                             0.1s
 =&gt; =&gt; transferring dockerfile: 2B                                                                                                0.0s
 =&gt; [internal] load .dockerignore                                                                                                 0.0s
 =&gt; =&gt; transferring context: 2B                                                                                                   0.0s
failed to solve with frontend dockerfile.v0: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount200403056/.Dockerfile: no such file or directory

Output of command in terminal: ls -al

total 160
drwxrwxr-x@   9 maheenk  staff    288 Apr 24 11:42 .
drwxrwxrwx@ 110 maheenk  staff   3520 Apr 23 17:24 ..
-rw-r--r--@   1 maheenk  staff   6148 Apr 24 11:42 .DS_Store
drwxr-xr-x   14 maheenk  staff    448 Apr 24 11:00 .git
-rw-r--r--@   1 maheenk  staff    523 Apr 24 10:54 Dockerfile
-rw-r--r--    1 maheenk  staff    669 Apr 23 16:26 go.mod
-rw-r--r--    1 maheenk  staff  56262 Apr 23 16:26 go.sum
-rw-r--r--    1 maheenk  staff   2126 Apr 23 16:09 main.go

I added a .Dockerfile to the folder(basically a copy of the dockerfile that i renamed by putting a period in front of)

drwxrwxr-x@   9 maheenk  staff    288 Apr 24 13:03 .
drwxrwxrwx@ 111 maheenk  staff   3552 Apr 24 12:43 ..
-rw-r--r--@   1 maheenk  staff   6148 Apr 24 13:03 .DS_Store
-rw-r--r--@   1 maheenk  staff    513 Apr 24 13:06 .Dockerfile
drwxr-xr-x   14 maheenk  staff    448 Apr 24 13:00 .git
-rw-r--r--@   1 maheenk  staff    523 Apr 24 10:54 Dockerfile
-rw-r--r--    1 maheenk  staff    669 Apr 23 16:26 go.mod
-rw-r--r--    1 maheenk  staff  56262 Apr 23 16:26 go.sum
-rw-r--r--    1 maheenk  staff   2126 Apr 23 16:09 main.go```

UPDATED VERSION:

redid the run line in docker as suggested

#syntax=docker/dockerfile:1

FROM golang:latest


WORKDIR /app

# Download necessary Go modules
COPY go.mod ./
COPY go.sum ./
RUN go mod download

#copies source code onto image
COPY *.go ./

#static application in rootfilesystem
RUN build -t internhw:latest .Dockerfile .


EXPOSE 8080

#command to execute when image is used to start a container
CMD [ &quot;internhw&quot; ] ```

when i run in terminal docker build -t internhw:latest .Dockerfile .

&quot;docker build&quot; requires exactly 1 argument.
See &#39;docker build --help&#39;.

Usage:  docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile

</details>


# 答案1
**得分**: 1

在Dockerfile中执行`docker build`确实没有太多意义,因为Dockerfile本身就是通过'docker build'构建的。

如果你需要向现有镜像添加资源,可以在你的Dockerfile中使用COPY或ADD命令。

如果你需要编译某些内容并将结果放入现有镜像中,可以使用多阶段构建。你可以参考[多阶段构建文档][1]。

  [1]: https://docs.docker.com/develop/develop-images/multistage-build/

<details>
<summary>英文:</summary>

It does make little sense to do a `docker build` inside a Dockerfile which is itself... &#39;docker built&#39;.

If you need to add resources to an existing image, you can use COPY or ADD inside your `Dockerfile`.

If you need to compile something and put the result in an existing image, you can use a [multi-stage build][1].


  [1]: https://docs.docker.com/develop/develop-images/multistage-build/

</details>



huangapple
  • 本文由 发表于 2022年4月25日 01:32:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/71990772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定