Skip to content

Docker compose Flask container throws error during start-up (exec /usr/local/bin/gunicorn: exec format error)

0

I want to deploy a service contains a flask, Nginx and Postgres db. Upon creating a cluster in ECS, I get the error from the flask container error message exec /usr/local/bin/gunicorn: exec format error. Dockerfile

# pull official base image
FROM python:3.9.6-alpine

# Update pip
RUN python -m pip install --upgrade pip

# Expose port
EXPOSE 8080

# set work directory
WORKDIR /app

# Prevents Python from writing pyc files to disc (equivalent to python -B option)
ENV PYTHONDONTWRITEBYTECODE=1

# Prevents Python from buffering stdout and stderr (equivalent to python -u option)
ENV PYTHONUNBUFFERED=1

RUN ls -ltr /app
# install dependencies
RUN pip install --upgrade pip
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
RUN pip install gunicorn

COPY . /app

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser


ENV FLASK_RUN_PORT 8080
ENV FLASK_APP app.py
CMD ["/usr/local/bin/gunicorn", "--config", "gunicorn_config.py", "app:app"]

Docker compose file


x-aws-vpc: "vpc-063f8b21a548c037c"
#x-aws-cluster: "Flask-API-Cluster"

services:
  flask_api:
    container_name: flaskapi
    image: dockerelvis/flask:latest
    logging:
      driver: awslogs
      options:
        awslogs-region: eu-west-2
    restart: unless-stopped
    volumes:
      - flask-data:/app
    networks:
      - flask-network
      - db-network
    depends_on:
      - database
    links:
      - database
    env_file:
      - backend/.env
    healthcheck:
      test: wget --no-verbose --tries=1 --spider http://localhost || exit 1
      interval: 60s
      retries: 5
      start_period: 20s
      timeout: 10s

  database:
    container_name: postgresdb
    image: postgres:latest
    restart: unless-stopped
    env_file:
      - backend/.env
    networks:
      - db-network
    volumes:
      - postgres-data:/var/lib/postgresql/data

  nginx:
    image: dockerelvis/nginx:latest
    restart: always
    volumes:
      - static_volume:/home/app/web/staticfiles
    ports:
      - target: 9000
        x-aws-protocol: http
    networks:
      - flask-network
    depends_on:
      - flask_api

volumes:
  postgres-data:
    external: false
    name: postgres-data
  static_volume:
    external: false
    name: static_volume
  flask-data:
    external: false
    name: flask-data

networks:
  flask-network:
    external: false
    name: flask-network
  db-network:
    external: false
    name: db-network

x-aws-logs_retention: 10
3 Answers
0

Does it work locally? (is this an AWS issue or a docker compose issue) I would simplify down as much as possible until it works, then build in extra features. Looks like you have both custom networks defined as well as "links" in your Flask API definition. Looks like "links" is deprecated and I don't think you need that if you're defining custom networks.

One other thing to check is make sure there is no architecture mis-match. Your cluster is not running arm64 is it?

answered 3 years ago
0

That error is most likely arising from a mismatch between the architecture the container was build on vs. the architecture you are running it on. To resolve this, if you are using docker take advantage of the docker buildx command. An example of this would be:

docker buildx build --platform=linux/amd64 -t somecontainer .

Replace the linux/amd64 with the architecture you wish to run your container on and it will build for that specific architecture.

AWS
answered 3 years ago
0

Hi, thanks for the assistance. I did build the image on three architectures. It is again running locally but now gives me a different error when I attempt to run it on ECS. I used the below command to create the image docker buildx build --platform linux/arm64,linux/amd64,linux/amd64/v2 -t dockerelvis/awsflask:latest --push . I did create images using one of each of the above platforms but all throw same error. unable to locate files in the container. Error: 'gunicorn_config.py' doesn't exist GitHub repo: https://github.com/Elvis-aws/FlaskAWSApp Cloudwatch-error Help will be much appreciated, thanks

answered 3 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.