英文:
starting container process caused: exec: "air": executable file not found
问题
我是新手使用Docker。我正在尝试使用docker-compose将我的Go应用程序容器化。
使用的技术有:
Golang、Docker 20.10.8和Air(用于实时重载)。
我的Dockerfile如下所示:
FROM base as dev
WORKDIR /opt/app/api
RUN apk update
RUN apk add git gcc musl-dev
RUN apk add curl
RUN curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
# RUN go get
# RUN go mod tidy
CMD ["air"]
我的docker-compose.yml如下所示:
version: "3.9"
services:
app:
build:
dockerfile: Dockerfile.local
context: .
target: dev
container_name: 'server'
volumes:
- .:/opt/app/api
env_file:
- .env
ports:
- "8080:8080"
restart:
always
depends_on:
- db
- rabbitmq
db:
image: postgres:13-alpine
volumes:
- data:/var/lib/postgresql/data
container_name: 'postgres'
ports:
- 5432:5432
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_HOST_AUTH_METHOD: trust
POSTGRES_PASSWORD: postgres
rabbitmq:
image: rabbitmq:3-management-alpine
container_name: 'rabbitmq'
ports:
- 5672:5672
- 15672:15672
volumes:
- rabbitmq:/var/lib/rabbitmq
- rabbitmq-log:/var/log/rabbitmq
migrate: &basemigrate
profiles: ["tools"]
image: migrate/migrate
entrypoint: "migrate -database postgresql://thursday:postgres@db/postgres?sslmode=disable -path /tmp/migrations"
command: up
depends_on:
- db
volumes:
- ./migrations:/tmp/migrations
create-migration:
<<: *basemigrate
entrypoint: migrate create -dir /tmp/migrations -ext sql
command: ""
depends_on:
- db
down-migration:
<<: *basemigrate
entrypoint: migrate -database postgresql://thursday:postgres@db/postgres?sslmode=disable -path /tmp/migrations
command: down
depends_on:
- db
volumes:
data:
rabbitmq:
rabbitmq-log:
运行命令sudo docker-compose up -d
时,我遇到了以下错误:
Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "air": 在$PATH中找不到可执行文件: unknown
英文:
I am new to docker. I am trying to containerise my Go application using docker-compose.
Technology used
Golang, Docker 20.10.8 and Air (for live reloading).
My Dockerfile looks like this.
FROM base as dev
WORKDIR /opt/app/api
RUN apk update
RUN apk add git gcc musl-dev
RUN apk add curl
RUN curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
# RUN go get
# RUN go mod tidy
CMD ["air"]
My docker-compose.yml is this.
version: "3.9"
services:
app:
build:
dockerfile: Dockerfile.local
context: .
target: dev
container_name: 'server'
volumes:
- .:/opt/app/api
env_file:
- .env
ports:
- "8080:8080"
restart:
always
depends_on:
- db
- rabbitmq
db:
image: postgres:13-alpine
volumes:
- data:/var/lib/postgresql/data
container_name: 'postgres'
ports:
- 5432:5432
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_HOST_AUTH_METHOD: trust
POSTGRES_PASSWORD: postgres
rabbitmq:
image: rabbitmq:3-management-alpine
container_name: 'rabbitmq'
ports:
- 5672:5672
- 15672:15672
volumes:
- rabbitmq:/var/lib/rabbitmq
- rabbitmq-log:/var/log/rabbitmq
migrate: &basemigrate
profiles: ["tools"]
image: migrate/migrate
entrypoint: "migrate -database postgresql://thursday:postgres@db/postgres?sslmode=disable -path /tmp/migrations"
command: up
depends_on:
- db
volumes:
- ./migrations:/tmp/migrations
create-migration:
<<: *basemigrate
entrypoint: migrate create -dir /tmp/migrations -ext sql
command: ""
depends_on:
- db
down-migration:
<<: *basemigrate
entrypoint: migrate -database postgresql://thursday:postgres@db/postgres?sslmode=disable -path /tmp/migrations
command: down
depends_on:
- db
volumes:
data:
rabbitmq:
rabbitmq-log:
On running command sudo docker-compose up -d I am getting the following error
Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "air": executable file not found in $PATH: unknown
答案1
得分: 1
如在“docker: executable file not found in $PATH
”中提到的:
当您使用执行格式的命令(在您的情况下:CMD ["air"]
,一个带有双引号的JSON数组)时,它将在没有shell的情况下执行。
这意味着大多数环境变量将不会存在。
只要满足以下条件,CMD air
应该可以工作:
-
air
是一个可执行文件(chmod 755
) -
air
已经交叉编译到Linux(除非运行Docker的主机已经是Linux)
英文:
As mentioned in "docker: executable file not found in $PATH
":
When you use the exec format for a command (in your case: CMD ["air"]
, a JSON array with double quotes) it will be executed without a shell.
This means that most environment variables will not be present.
CMD air
should work, provided:
-
air
is an executable (chmod 755
) -
air
was cross-compiled to Linux (unless your host running docker is already Linux)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论