我想在docker
(我在nest.js
上写的服务器)上运行服务器,我使用mysql
和ormtype
来数据库。这是我的dockerfile
和docker-compose.yml
。
#dockerfile
FROM node:12.13.0-alpine
WORKDIR /src
COPY ./package.json .
COPY ./package-lock.json .
RUN npm install
COPY . .
EXPOSE 8081
CMD npm run start:dev
# docker-compose.yml
version: "3.3"
services:
mysql:
container_name: mysql
image: mysql:5.7.27
ports:
- 3306:3306
expose:
- 3306
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_DATABASE: root
restart: always
server:
container_name: sever
command: ["./wait-for-it.sh", "mysql:3306", "--", "npm", "run", "start:dev"]
depends_on:
- mysql
links:
- mysql
build:
context: server
dockerfile: Dockerfile
environment:
PORT: 8081
ports:
- 8081:8081
restart: on-failure
volumes:
- ./server:/src/
我让服务器等待数据库启动。但是服务器启动后,出现错误。
user1@ps:~/documents/projects/project1$ sudo docker-compose up --build
Creating network "project1_default" with the default driver
Building server
Step 1/8 : FROM node:12.13.0-alpine
---> 5d187500daae
Step 2/8 : WORKDIR /src
---> Using cache
---> b6f4bc8d73d2
Step 3/8 : COPY ./package.json .
---> Using cache
---> 83ece116c790
Step 4/8 : COPY ./package-lock.json .
---> Using cache
---> 24c334b23d56
Step 5/8 : RUN npm install
---> Using cache
---> 1cf26bbbf0b0
Step 6/8 : COPY . .
---> Using cache
---> 7760703813f9
Step 7/8 : EXPOSE 8081
---> Using cache
---> 8edce63e71c9
Step 8/8 : CMD npm run start:dev
---> Using cache
---> 5c726b1b930b
Successfully built 5c726b1b930b
Successfully tagged project1_server:latest
Creating mysql ... done
Creating sever ... done
Attaching to mysql, sever
mysql | Initializing database
sever | env: can't execute 'bash': No such file or directory
mysql | 2019-11-14T10:20:22.270941Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
mysql | 2019-11-14T10:20:22.912746Z 0 [Warning] InnoDB: New log files created, LSN=45790
mysql | 2019-11-14T10:20:23.040600Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
mysql | 2019-11-14T10:20:23.117486Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 5e28b9cc-06c8-11ea-bd8a-0242ac120002.
mysql | 2019-11-14T10:20:23.123840Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
mysql | 2019-11-14T10:20:23.124967Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
sever exited with code 0
sever exited with code 127
mysql | Database initialized
mysql | Initializing certificates
mysql | Generating a RSA private key
mysql | ................+++++
mysql | ...............................................+++++
mysql | unable to write 'random state'
mysql | writing new private key to 'ca-key.pem'
mysql | -----
mysql | Generating a RSA private key
mysql | ....+++++
mysql | ................................................+++++
mysql | unable to write 'random state'
mysql | writing new private key to 'server-key.pem'
mysql | -----
mysql | Generating a RSA private key
mysql | .......................................................................................................................+++++
mysql | .............+++++
mysql | unable to write 'random state'
mysql | writing new private key to 'client-key.pem'
mysql | -----
mysql | Certificates initialized
mysql | MySQL init process in progress...
sever exited with code 127
mysql | 2019-11-14T10:20:29.192610Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
mysql | 2019-11-14T10:20:29.193767Z 0 [Note] mysqld (mysqld 5.7.27) starting as process 91 ...
mysql | 2019-11-14T10:20:29.196140Z 0 [Note] InnoDB: PUNCH HOLE support available
mysql | 2019-11-14T10:20:29.196154Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
mysql | 2019-11-14T10:20:29.196157Z 0 [Note] InnoDB: Uses event mutexes
mysql | 2019-11-14T10:20:29.196159Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
mysql | 2019-11-14T10:20:29.196162Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
mysql | 2019-11-14T10:20:29.196164Z 0 [Note] InnoDB: Using Linux native AIO
mysql | 2019-11-14T10:20:29.196387Z 0 [Note] InnoDB: Number of pools: 1
mysql | 2019-11-14T10:20:29.196491Z 0 [Note] InnoDB: Using CPU crc32 instructions
mysql | 2019-11-14T10:20:29.197706Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
mysql | 2019-11-14T10:20:29.203604Z 0 [Note] InnoDB: Completed initialization of buffer pool
mysql | 2019-11-14T10:20:29.205704Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
mysql | 2019-11-14T10:20:29.216931Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
mysql | 2019-11-14T10:20:29.232083Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
mysql | 2019-11-14T10:20:29.232827Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
mysql | 2019-11-14T10:20:29.389401Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
mysql | 2019-11-14T10:20:29.391548Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
mysql | 2019-11-14T10:20:29.391575Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
mysql | 2019-11-14T10:20:29.392343Z 0 [Note] InnoDB: Waiting for purge to start
mysql | 2019-11-14T10:20:29.442516Z 0 [Note] InnoDB: 5.7.27 started; log sequence number 2625438
mysql | 2019-11-14T10:20:29.442792Z 0 [Note] Plugin 'FEDERATED' is disabled.
mysql | 2019-11-14T10:20:29.446146Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
mysql | 2019-11-14T10:20:29.447290Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
mysql | 2019-11-14T10:20:29.447479Z 0 [Warning] CA certificate ca.pem is self signed.
mysql | 2019-11-14T10:20:29.447503Z 0 [Note] InnoDB: Buffer pool(s) load completed at 191114 10:20:29
mysql | 2019-11-14T10:20:29.466159Z 0 [Warning] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
mysql | 2019-11-14T10:20:29.473779Z 0 [Note] Event Scheduler: Loaded 0 events
mysql | 2019-11-14T10:20:29.474160Z 0 [Note] mysqld: ready for connections.
mysql | Version: '5.7.27' socket: '/var/run/mysqld/mysqld.sock' port: 0 MySQL Community Server (GPL)
sever exited with code 127
mysql | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
mysql | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
mysql | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
mysql | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
mysql | mysql: [Warning] Using a password on the command line interface can be insecure.
mysql | mysql: [Warning] Using a password on the command line interface can be insecure.
mysql | ERROR 1396 (HY000) at line 1: Operation CREATE USER failed for 'root'@'%'
sever exited with code 127
sever | env: can't execute 'bash': No such file or directory
sever | env: can't execute 'bash': No such file or directory
sever | env: can't execute 'bash': No such file or directory
sever | env: can't execute 'bash': No such file or directory
sever | env: can't execute 'bash': No such file or directory
sever | env: can't execute 'bash': No such file or directory
我在哪里弄错了?如何在Docker容器中正确启动服务器。
最佳答案
您的基本镜像node:12.13.0-alpine
是基于alpine的,其中wait-for-it
是基于bash的,而ash
在alpine no
bash中。
错误日志看起来非常清晰。
can't execute 'bash': No such file or directory
您可以尝试的一种选择是在您的nodejs Dockerfile中添加bash
FROM node:12.13.0-alpine
WORKDIR /src
RUN apk add --no-cache bash
然后尝试运行。
但这是 Alpine 的问题
Can you make an /bin/sh version for use with alpine linux ? #18
或者您可以使用docker-wait-for-it-alpine
关于docker - 启动数据库后容器未启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58854686/