Dsu*_*jan 8 linux node.js express docker docker-compose
我正在 docker 容器内运行 Express api 服务器。现在,当我尝试通过multer设置dest参数来上传文件时,/它会上传到 docker 容器内的某个位置(很可能/dist/api/)。
该 API 是从 dist 运行的,因此控制台日志__dirname给了我/dist/api/它位于 docker 容器内而不是在真实主机中。我需要上传里面的文件/home/uploads/images/。我怎样才能做到这一点?也许这很容易,但我对 docker 不太了解。
API 的 Docker 文件
FROM node:8.10
# Set a timezone
ENV TZ=UTC
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
COPY . .
RUN yarn install
RUN yarn run build
CMD node dist
HEALTHCHECK --interval=5s --timeout=30s --retries=50 \
CMD curl -f localhost:8080/api || exit 1
Run Code Online (Sandbox Code Playgroud)
Docker 撰写文件
webapi:
depends_on:
serviceapi:
condition: service_healthy
restart: always
networks:
- internal_network
build: ./api/
expose:
- "8080"
ports:
- "8080:8080"
文件上传代码
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, '/home/uploads/images/')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
})
var upload = multer({ storage: storage })
app.post('/profile', upload.any(), function (req, res, next) {
//logic goes over here
})
Run Code Online (Sandbox Code Playgroud)
您可以将主机的文件夹映射到容器,
Docker 撰写文件
webapi:
depends_on:
serviceapi:
condition: service_healthy
restart: always
networks:
- internal_network
build: ./api/
volumes:
- "/home/uploads/images/:/folder/in/container"
expose:
- "8080"
ports:
- "8080:8080"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6505 次 |
| 最近记录: |