我最近安装了docker工具箱,并创建了一个名为“ superawesomecontainer2”的新容器来训练豚鼠,每次运行该容器时,我都必须重新安装package.json,如果使用了-p 3001:3001,则可以在本地主机上运行我的服务器,它只是无法正常工作,每当我关闭该容器并重新运行它时,我都必须重新安装package.json,然后它才能到达本地主机?
我已经尝试解决这个问题了 -在该容器中删除我的package-look.json 清洁现金-力 -重新安装我的软件包 -重新安装docker it self
我的package.json
{
"name": "facerecognitionapi",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt-nodejs": "0.0.3",
"body-parser": "^1.18.3",
"clarifai": "^2.8.0",
"cors": "^2.8.4",
"dotenv": "^8.0.0",
"express": "^4.16.3",
"knex": "^0.15.1",
"pg": "^7.4.3"
},
"devDependencies": {
"nodemon": "^1.17.5"
}
}
我的docker文件
FROM node:10.16.0
WORKDIR C:\Users\C.M\Desktop\smart-barin-api
COPY ./ ./
RUN npm install
CMD ["/bin/bash"]
码头工人信息
E:\javascript\facerecognitionapi>docker info
Containers: 38
Running: 0
Paused: 0
Stopped: 38
Images: 51
Server Version: 18.09.6
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: bb71b10fd8f58240ca47fbb579b9d1028eea7c84
runc version: 2b18fe1d885ee5083ef9f0838fee39b62d653e30
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 4.14.116-boot2docker
Operating System: Boot2Docker 18.09.6 (TCL 8.2.1)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 989.4MiB
Name: default
ID: LRFB:QJNW:CCDA:2Y7X:XZWB:FREX:O4JB:D7PN:KWDK:ACPH:B6FC:Y4KY
Docker Root Dir: /mnt/sda1/var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
provider=virtualbox
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
期望:我的服务器在本地主机3001上运行 我不会在每次运行此容器时重新安装软件包
答案 0 :(得分:0)
我在您的Dockerfile
中看到了几个问题。
WORKDIR
:指容器内部 的目录结构。因此,它不应包含Windows之类的路径,而应包含WORKDIR /app
之类的东西。COPY
:由于您将当前文件夹中的所有文件复制到目标文件夹,因此您必须在与DockerFile
相同的文件夹中至少包含:{{ 1}}和index.js
。package.json
:您必须公开运行在容器中的服务器的端口,这是通过EXPOSE
指令完成的。例如EXPOSE
。然后,在运行容器EXPOSE 8000
时必须映射端口。根据需要或配置更改端口。-p 8000:8000
:包含将在容器开头执行的命令。如果它包含CMD
,它将执行一个外壳程序,如果不以交互方式运行它,只需退出即可。因此,您必须开始类似/bin/bash
最后,您的CMD ["npm", "start"]
应该看起来像这样。
Dockerfile
您应该像这样构建并运行它:
FROM node:10.16.0
# create and go to the app dir
WORKDIR /app
# copy all local file to the app dir
COPY ./ ./
# install the app
RUN ["npm", "install"]
# tell docker what port to expose
EXPOSE 8000
# start the server
CMD ["npm", "start"]
如果您还可以查看示例here。