docker build创建了两个映像,而不是一个包含两个组件的映像

gopyfrb3  于 2022-11-22  发布在  Docker
关注(0)|答案(1)|浏览(107)

我想建立一个简单的网络服务器,以取得联系,与docker组成,但有两个图像建立和网络服务器不运行。
app.js:

'use strict';

const cors = require('cors');
const { request } = require('express');
const express = require('express');
const {flaschenpost} = request('flaschenpost');
const http = require('http'); 

const logger = flaschenpost.getLogger(); 

const api = express(); 

api.use(cors());

api.get('/', (req,res) => {
    res.json(
        {
            now: Date.now()
        });
});

const server = http.createServer(api);
const port = 3_000;

server.listen(post, ()=> {
    logger.info('Server started.', {port});
});

我的Dockerfile看起来像这样:

FROM node:16.13.0-alpine

USER node
WORKDIR /home/node

COPY --chown=node:node ./package.json ./package.json
COPY --chown=node:node ./package-lock.json ./package-lock.json

RUN npm install --production

COPY --chown=node:node . .

CMD [ "node", "app.js"]

sudo docker build -t api
sudo docker run -d --init -p 3000:3000 --name api api
运行docker ps后的输出
|回购协议|标签|图像标识|已创建|尺寸|
|美国石油学会|最新的|二|4分钟前|127兆字节|
|节点|16.13.0-alpine| 44e24535dfbf型|12个月前|110兆字节|
curl http://localhost:3000
输出以下内容:
curl: (7) Failed to connect to localhost port 3000 after 0 ms: Connection refused

lymnna71

lymnna711#

出错了。Dockerfile

FROM node:17
ENV NODE_ENV=production

WORKDIR /app

COPY ["package.json", "package-lock.json*", "./"]

RUN npm install --production

COPY . .

CMD [ "node", "index.js" ]

现在开始构建

docker build -t api .

检查图像是否得到您真正想要的

docker inspect image api:latest 
[
    {
        "Id": "sha256:7bbad5c790f4583e19b1dcf3b3c1aeb0a45fc46ad997d54222a51f1867e8789b",
        "RepoTags": [
            "api:latest"
        ],
        "RepoDigests": [],
        "Parent": "sha256:e58f7a2e8bb5c548475104eb813a6f12b8f610aa939b69de92d349116c2d9a00",
        "Comment": "",
        "Created": "2022-11-14T09:51:03.762665424Z",

相关问题