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

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

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

问题

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

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

我的Dockerfile:

  1. #syntax=docker/dockerfile:1
  2. #使用最新版本
  3. FROM golang:latest
  4. WORKDIR /app
  5. #下载必要的Go模块
  6. COPY go.mod ./
  7. COPY go.sum ./
  8. RUN go mod download
  9. #将源代码复制到镜像中
  10. COPY *.go ./
  11. #在根文件系统中创建静态应用程序
  12. RUN docker build -t internhw:latest -f .Dockerfile .
  13. EXPOSE 8080
  14. #当镜像用于启动容器时执行的命令
  15. CMD ["internhw"]

我在终端中运行的命令:

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

终端的输出是:

  1. [+] Building 0.1s (2/2) FINISHED
  2. => [internal] load build definition from .Dockerfile 0.1s
  3. => => transferring dockerfile: 2B 0.0s
  4. => [internal] load .dockerignore 0.0s
  5. => => transferring context: 2B 0.0s
  6. 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的输出:

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

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

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

更新后的版本

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

  1. #syntax=docker/dockerfile:1
  2. FROM golang:latest
  3. WORKDIR /app
  4. #下载必要的Go模块
  5. COPY go.mod ./
  6. COPY go.sum ./
  7. RUN go mod download
  8. #将源代码复制到镜像中
  9. COPY *.go ./
  10. #在根文件系统中创建静态应用程序
  11. RUN build -t internhw:latest .Dockerfile .
  12. EXPOSE 8080
  13. #当镜像用于启动容器时执行的命令
  14. CMD ["internhw"]

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

  1. "docker build"需要正好1个参数。
  2. 请参阅'docker build --help'
  3. 用法:docker build [OPTIONS] PATH | URL | -
  4. Dockerfile构建镜像
  5. <details>
  6. <summary>英文:</summary>
  7. 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.
  8. my file path is `Users/maheenk/Desktop/infra`
  9. my docker file :
  10. ```sh
  11. #syntax=docker/dockerfile:1
  12. #using latest version
  13. FROM golang:latest
  14. WORKDIR /app
  15. #Download necessary Go modules
  16. COPY go.mod ./
  17. COPY go.sum ./
  18. RUN go mod download
  19. #copies source code onto image
  20. COPY *.go ./
  21. #static application in rootfilesystem
  22. RUN docker build -t internhw:latest -f .Dockerfile .
  23. EXPOSE 8080
  24. #command to execute when image is used to start a container
  25. CMD [ &quot;internhw&quot;]

the command I run in terminal:

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

what the output is in terminal:

  1. [+] Building 0.1s (2/2) FINISHED
  2. =&gt; [internal] load build definition from .Dockerfile 0.1s
  3. =&gt; =&gt; transferring dockerfile: 2B 0.0s
  4. =&gt; [internal] load .dockerignore 0.0s
  5. =&gt; =&gt; transferring context: 2B 0.0s
  6. 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

  1. total 160
  2. drwxrwxr-x@ 9 maheenk staff 288 Apr 24 11:42 .
  3. drwxrwxrwx@ 110 maheenk staff 3520 Apr 23 17:24 ..
  4. -rw-r--r--@ 1 maheenk staff 6148 Apr 24 11:42 .DS_Store
  5. drwxr-xr-x 14 maheenk staff 448 Apr 24 11:00 .git
  6. -rw-r--r--@ 1 maheenk staff 523 Apr 24 10:54 Dockerfile
  7. -rw-r--r-- 1 maheenk staff 669 Apr 23 16:26 go.mod
  8. -rw-r--r-- 1 maheenk staff 56262 Apr 23 16:26 go.sum
  9. -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)

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

UPDATED VERSION:

redid the run line in docker as suggested

  1. #syntax=docker/dockerfile:1
  2. FROM golang:latest
  3. WORKDIR /app
  4. # Download necessary Go modules
  5. COPY go.mod ./
  6. COPY go.sum ./
  7. RUN go mod download
  8. #copies source code onto image
  9. COPY *.go ./
  10. #static application in rootfilesystem
  11. RUN build -t internhw:latest .Dockerfile .
  12. EXPOSE 8080
  13. #command to execute when image is used to start a container
  14. CMD [ &quot;internhw&quot; ] ```

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

  1. &quot;docker build&quot; requires exactly 1 argument.
  2. See &#39;docker build --help&#39;.
  3. Usage: docker build [OPTIONS] PATH | URL | -
  4. Build an image from a Dockerfile
  5. </details>
  6. # 答案1
  7. **得分**: 1
  8. Dockerfile中执行`docker build`确实没有太多意义,因为Dockerfile本身就是通过'docker build'构建的。
  9. 如果你需要向现有镜像添加资源,可以在你的Dockerfile中使用COPYADD命令。
  10. 如果你需要编译某些内容并将结果放入现有镜像中,可以使用多阶段构建。你可以参考[多阶段构建文档][1]。
  11. [1]: https://docs.docker.com/develop/develop-images/multistage-build/
  12. <details>
  13. <summary>英文:</summary>
  14. It does make little sense to do a `docker build` inside a Dockerfile which is itself... &#39;docker built&#39;.
  15. If you need to add resources to an existing image, you can use COPY or ADD inside your `Dockerfile`.
  16. If you need to compile something and put the result in an existing image, you can use a [multi-stage build][1].
  17. [1]: https://docs.docker.com/develop/develop-images/multistage-build/
  18. </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:

确定