gpt4 book ai didi

python-3.x - Docker,Ubuntu 18.04 python3.7.2 : standard_init_linux. go:207: exec 用户进程导致 "exec format error"

转载 作者:行者123 更新时间:2023-11-29 12:07:17 24 4
gpt4 key购买 nike

我正在尝试通过链接 https://testdriven.io/courses/microservices-with-docker-flask-and-react/part-one-postgres-setup/ 设置 docker 服务

我已有的代码:

代码结构

docker-compose-dev.yml
services/
users/
manage.py
Dockerfile-dev
entrypoint.sh
project/
__init__.py
config.py
db/
create.sql
Dockerfile

docker-compose.yml

version: '3.7'

services:

users:
build:
context: ./services/users
dockerfile: Dockerfile
volumes:
- './services/users:/usr/src/app'
ports:
- 5001:5000
environment:
- FLASK_APP=project/__init__.py
- FLASK_ENV=development
- APP_SETTINGS=project.config.DevelopmentConfig
- DATABASE_URL=postgres://postgres:postgres@users-db:5432/users_dev # new
- DATABASE_TEST_URL=postgres://postgres:postgres@users-db:5432/users_test # new
depends_on: # new
- users-db

users-db: # new
build:
context: ./services/users/project/db
dockerfile: Dockerfile
ports:
- 5435:5432
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres

管理.py:


from flask.cli import FlaskGroup

from project import app, db # new


cli = FlaskGroup(app)


# new
@cli.command('recreate_db')
def recreate_db():
db.drop_all()
db.create_all()
db.session.commit()


if __name__ == '__main__':
cli()

Docker 文件:

# base image
FROM python:3.7.2-alpine

# new
# install dependencies
RUN apk update && \
apk add --virtual build-deps gcc python-dev musl-dev && \
apk add postgresql-dev && \
apk add netcat-openbsd && \
apk add bind-tools

# set working directory
WORKDIR /usr/src/app

# add and install requirements
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt

# new
# add entrypoint.sh
COPY ./entrypoint.sh /usr/src/app/entrypoint.sh
RUN chmod +x /usr/src/app/entrypoint.sh

# add app
COPY . /usr/src/app

# new
# run server
CMD ["/usr/src/app/entrypoint.sh"]

入口点.sh

echo "Waiting for postgres..."

while ! nc -z users-db 5432; do
sleep 0.1
done

echo "PostgreSQL started"

python manage.py run -h 0.0.0.0

配置.py

import os  # new


class BaseConfig:
"""Base configuration"""
TESTING = False
SQLALCHEMY_TRACK_MODIFICATIONS = False # new


class DevelopmentConfig(BaseConfig):
"""Development configuration"""
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') # new


class TestingConfig(BaseConfig):
"""Testing configuration"""
TESTING = True
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_TEST_URL') # new


class ProductionConfig(BaseConfig):
"""Production configuration"""
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') # new

初始化.py

import os
from flask import Flask, jsonify
from flask_restful import Resource, Api
from flask_sqlalchemy import SQLAlchemy


# instantiate the app
app = Flask(__name__)

api = Api(app)

# set config
app_settings = os.getenv('APP_SETTINGS')
app.config.from_object(app_settings)

# instantiate the db
db = SQLAlchemy(app)


# model
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
username = db.Column(db.String(128), nullable=False)
email = db.Column(db.String(128), nullable=False)
active = db.Column(db.Boolean(), default=True, nullable=False)

def __init__(self, username, email):
self.username = username
self.email = email


class UsersPing(Resource):
def get(self):
return {
'status': 'success',
'message': 'pong!'
}

创建.sql

CREATE DATABASE users_prod;
CREATE DATABASE users_dev;
CREATE DATABASE users_test;

db 文件夹中的 Dockerfile:

# base image
FROM postgres:11.2-alpine

# run create.sql on init
ADD create.sql /docker-entrypoint-initdb.d

该应用程序有望成功构建并在端口 5001 上运行。但是 docker-compose 日志给出了以下结果:

users_1     | standard_init_linux.go:207: exec user process caused "exec format error"
users-db_1 | 2019-05-09 15:07:37.245 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
users-db_1 | 2019-05-09 15:07:37.245 UTC [1] LOG: listening on IPv6 address "::", port 5432
users-db_1 | 2019-05-09 15:07:37.282 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
users-db_1 | 2019-05-09 15:07:37.325 UTC [18] LOG: database system was shut down at 2019-05-09 15:07:35 UTC
users-db_1 | 2019-05-09 15:07:37.386 UTC [1] LOG: database system is ready to accept connections

最佳答案

我在 entrypoint.sh 中省略了 #!/bin/sh 部分

关于python-3.x - Docker,Ubuntu 18.04 python3.7.2 : standard_init_linux. go:207: exec 用户进程导致 "exec format error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56063605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com