我正在尝试通过链接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
manage.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()
Dockerfile:
# 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"]
entrypoint.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
config.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!'
}
create.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
答案 0 :(得分:0)
我已经省略了入口点中的#!/ bin / sh 部分。sh