首页 > 解决方案 > 让 Docker 映像和 Docker-Compose 文件正常工作

问题描述

我的项目目录中有一个 docker 文件和 docker compose 字段。我正在使用以下命令运行 docker compose 文件

docker-compose up

它为服务器和数据库构建并运行不同的图像,但我收到一个错误,说我的 package.json 文件不在正确的目录中。我不确定哪里出了问题。

这是我的码头文件

FROM node:10
WORKDIR /app
COPY package.json ./app
RUN npm install
COPY . /app
CMD npm start
EXPOSE 5585

这是我的码头工人撰写文件

web:
  image: node
  command: npm start
  ports:
    - "5585:5588"
  links:
    - db
  working_dir: /app
  environment:
    SEQ_DB: addidas
    SEQ_USER: sdfsdf
    SEQ_PW: sdfsdfs
    PORT: 4242
    DATABASE_URL: postgres://sdfsdf:sdfsdfs@localhost:5432/addidas
db:
  image: postgres
  ports:
  - "5432:5432"
  environment:
    POSTGRES_USER: sdfsdf
    POSTGRES_PASSWORD: sdfsdfs

我在终端中遇到的错误如下:

Attaching to addidas_db_1, addidas_web_1
db_1   | The files belonging to this database system will be owned by user "postgres".
db_1   | This user must also own the server process.
db_1   | 
db_1   | The database cluster will be initialized with locale "en_US.utf8".
db_1   | The default database encoding has accordingly been set to "UTF8".
db_1   | The default text search configuration will be set to "english".
db_1   | 
db_1   | Data page checksums are disabled.
db_1   | 
db_1   | fixing permissions on existing directory /var/lib/postgresql/data ... ok
db_1   | creating subdirectories ... ok
db_1   | selecting default max_connections ... 100
db_1   | selecting default shared_buffers ... 128MB
db_1   | selecting dynamic shared memory implementation ... posix
db_1   | creating configuration files ... ok
db_1   | running bootstrap script ... ok
db_1   | performing post-bootstrap initialization ... ok
db_1   | syncing data to disk ... ok
db_1   | 
db_1   | Success. You can now start the database server using:
db_1   | 
db_1   |     pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1   | 
db_1   | 
db_1   | WARNING: enabling "trust" authentication for local connections
db_1   | You can change this by editing pg_hba.conf or using the option -A, or
db_1   | --auth-local and --auth-host, the next time you run initdb.
db_1   | waiting for server to start....2018-11-06 17:38:51.968 UTC [43] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2018-11-06 17:38:51.983 UTC [44] LOG:  database system was shut down at 2018-11-06 17:38:51 UTC
db_1   | 2018-11-06 17:38:51.987 UTC [43] LOG:  database system is ready to accept connections
db_1   |  done
db_1   | server started
db_1   | CREATE DATABASE
db_1   | 
db_1   | 
db_1   | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1   | 
db_1   | waiting for server to shut down...2018-11-06 17:38:52.438 UTC [43] LOG:  received fast shutdown request
db_1   | .2018-11-06 17:38:52.441 UTC [43] LOG:  aborting any active transactions
db_1   | 2018-11-06 17:38:52.443 UTC [43] LOG:  background worker "logical replication launcher" (PID 50) exited with exit code 1
db_1   | 2018-11-06 17:38:52.444 UTC [45] LOG:  shutting down
db_1   | 2018-11-06 17:38:52.459 UTC [43] LOG:  database system is shut down
db_1   |  done
db_1   | server stopped
db_1   | 
db_1   | PostgreSQL init process complete; ready for start up.
db_1   | 
db_1   | 2018-11-06 17:38:52.556 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2018-11-06 17:38:52.556 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2018-11-06 17:38:52.560 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2018-11-06 17:38:52.575 UTC [61] LOG:  database system was shut down at 2018-11-06 17:38:52 UTC
db_1   | 2018-11-06 17:38:52.580 UTC [1] LOG:  database system is ready to accept connections
db_1   | 2018-11-06 17:46:15.922 UTC [1] LOG:  received smart shutdown request
db_1   | 2018-11-06 17:46:15.926 UTC [1] LOG:  background worker "logical replication launcher" (PID 67) exited with exit code 1
db_1   | 2018-11-06 17:46:15.928 UTC [62] LOG:  shutting down
db_1   | 2018-11-06 17:46:15.944 UTC [1] LOG:  database system is shut down
db_1   | 2018-11-06 17:46:19.284 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2018-11-06 17:46:19.284 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2018-11-06 17:46:19.288 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2018-11-06 17:46:19.309 UTC [24] LOG:  database system was shut down at 2018-11-06 17:46:15 UTC
db_1   | 2018-11-06 17:46:19.316 UTC [1] LOG:  database system is ready to accept connections
web_1  | npm ERR! path /app/package.json
web_1  | npm ERR! code ENOENT
web_1  | npm ERR! errno -2
web_1  | npm ERR! syscall open
web_1  | npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
web_1  | npm ERR! enoent This is related to npm not being able to find a file.
web_1  | npm ERR! enoent 
web_1  | 
web_1  | npm ERR! A complete log of this run can be found in:
web_1  | npm ERR!     /root/.npm/_logs/2018-11-06T17_47_14_825Z-debug.log
addidas_web_1 exited with code 254

标签: node.jspostgresqldockerdocker-compose

解决方案


你没有使用你的 docker 镜像你的 docker-compose.yml。

您应该指向您的 Dockerfile:

web:
  build: ./path/to/Dockerfile

您的配置也有一些错误。您应该在同一网络上共享容器(您的 Web 服务器和数据库),以便能够从 Web 服务器访问数据库。

networks:
  mynetwork:
    driver: bridge

web:
  build: ./path/to/Dockerfile
  networks:
    - mynetwork
  links:
    - db
  environment:
    SEQ_DB: addidas
    SEQ_USER: sdfsdf
    SEQ_PW: sdfsdfs
    PORT: 4242
    DATABASE_URL: postgres://sdfsdf:sdfsdfs@db:5432/addidas

db:
  image: postgres
  ports:
    - "5432:5432"
  networks:
    - mynetwork
  environment:
    POSTGRES_USER: sdfsdf
    POSTGRES_PASSWORD: sdfsdfs

推荐阅读