启动容器进程引发错误:执行文件未找到 “air”

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

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 [&quot;air&quot;]

My docker-compose.yml is this.

version: &quot;3.9&quot;
services:
  app:
    build:
      dockerfile: Dockerfile.local
      context: .
      target: dev
    container_name: &#39;server&#39;
    volumes:
      - .:/opt/app/api
    env_file:
      - .env      
    ports:
      - &quot;8080:8080&quot;
    restart:
      always    
    depends_on:
      - db
      - rabbitmq
  
  db:
    image: postgres:13-alpine
    volumes:
      - data:/var/lib/postgresql/data
    container_name: &#39;postgres&#39;
    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: &#39;rabbitmq&#39;
    ports:
      - 5672:5672
      - 15672:15672
    volumes:
        - rabbitmq:/var/lib/rabbitmq
        - rabbitmq-log:/var/log/rabbitmq
  
  migrate: &amp;basemigrate
    profiles: [&quot;tools&quot;]
    image: migrate/migrate
    entrypoint: &quot;migrate -database postgresql://thursday:postgres@db/postgres?sslmode=disable -path /tmp/migrations&quot;
    command: up
    depends_on:
      - db
    volumes:
    - ./migrations:/tmp/migrations
  
  create-migration:
    &lt;&lt;: *basemigrate
    entrypoint: migrate create -dir /tmp/migrations -ext sql
    command: &quot;&quot;
    depends_on:
      - db
  
  down-migration:
    &lt;&lt;: *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 [&quot;air&quot;], 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)

huangapple
  • 本文由 发表于 2021年11月9日 12:13:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/69892758.html
匿名

发表评论

匿名网友

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

确定