Docker Compose无法在Mac上构建特定服务

时间:2020-01-22 16:19:20

标签: python macos docker docker-compose

我的一个项目具有Python,PostgreSQL和Node依赖项。我有以下regions <- data.frame(ID = LETTERS[1:3], rows = c(100,8,50)) library(ggplot2) ggplot(regions, aes(x = reorder(ID,-rows), y = rows))+ geom_col()+ scale_y_continuous(expand = c(0,0))+ xlab("New Name")+ theme_classic()+ theme(axis.text.x = element_text(angle = 45, hjust = 1), axis.line.x = element_blank(), axis.ticks.x = element_blank(), plot.title = element_text(hjust = 0.5))+ ggtitle("Main Title") 文件,根据构建目录有一个docker-compose.yaml。一切都很好,并且可以在Linux机器上运行。但是当我在Mac上工作时,Dockerfile服务未运行。但是我可以从Mac的Docker Desktop运行,也可以通过键入server从控制台运行。但是,当我尝试使用docker-compose up --build server构建所有对象时。在Mac上无法使用。
我在控制台上发现以下错误:

docker-compose up --build

return func(*args, **kwargs) File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 260, in cursor return self._cursor() File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 236, in _cursor self.ensure_connection() File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection self.connect() File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 197, in connect self.connection = self.get_new_connection(conn_params) File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 185, in get_new_connection connection = Database.connect(**conn_params) File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 126, in connect conn = _connect(dsn, connection_factory=connection_factory, **kwasync) django.db.utils.OperationalError: could not connect to server: Connection refused Is the server running on host "db" (172.19.0.2) and accepting TCP/IP connections on port 5432? Traceback (most recent call last): psycopg2.OperationalError: could not connect to server: Connection refused Is the server running on host "db" (172.19.0.3) and accepting The above exception was the direct cause of the following exception: File "/app/server/manage.py", line 15, in <module> execute_from_command_line(sys.argv) File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line utility.execute() File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 328, in run_from_argv self.execute(*args, **cmd_options) File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 369, in execute output = self.handle(*args, **options) File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 83, in wrapped res = handle_func(*args, **kwargs) File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 86, in handle executor = MigrationExecutor(connection, self.migration_progress_callback) File "/usr/local/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__ self.loader = MigrationLoader(self.connection) File "/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 49, in __init__ self.build_graph() File "/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 212, in build_graph self.applied_migrations = recorder.applied_migrations() File "/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 76, in applied_migrations if self.has_table(): File "/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 56, in has_table return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())

docker-compose.yaml

version: '3.7' services: db: image: postgres:11.1-alpine container_name: baraticn_db ports: - '5434:5432' volumes: - ./postgres_data:/var/lib/postgresql/data/ server: build: ./server container_name: baraticn_python command: sh -c "python /app/server/manage.py migrate && python /app/server/manage.py runserver 0.0.0.0:8081" working_dir: /app/server environment: - PYTHONDONTWRITEBYTECODE=1 - PYTHONUNBUFFERED=1 volumes: - ./server:/app/server:cached ports: - '8081:8081' depends_on: - db client: build: ./client container_name: baraticn_vuejs command: sh -c "yarn install && yarn serve" working_dir: /app/client volumes: - ./client:/app/client:cached ports: - '8080:8080'

server/Dockerfile

1 个答案:

答案 0 :(得分:0)

在对此进行了一些研究之后,我发现根本没有问题。在Mac上,我为Docker容器分配了1个CPU和1GB RAM。因此,我正在运行并缓慢启动容器。我的Python应用服务器depends_on PostgreSQL数据库服务器。 数据库容器正在运行,但尚未准备就绪。因此,python服务器首次出现故障。

快速解决方案:

restart: on-failure:10

已在python server服务上添加。我的意思是,每次失败后,它将尝试运行10次。