Dockerfile
FROM node:alpine
WORKDIR /usr/app
COPY ./package.json ./
RUN npm install
COPY ./ ./
CMD ["npm","start"]
docker-compose.yml
version: '3'
services:
redis-server:
image: 'redis'
ports:
- "6379:6379"
node-app:
build: .
ports:
- "8081:8081"
app.js
const express = require('express');
const redis = require('redis');
const app = express();
const client = redis.createClient({ host:'redis-server', port: 6379 });
client.set('visits', 0);
app.get('/', (req, res) => { client.get('visits', (err, visits) => { res.send('Number of visits is ' + visits); client.set('visits', parseInt(visits) + 1); }); });
app.listen(8081, () => { console.log('Listening on port 8081'); });
错误
Error: getaddrinfo ENOTFOUND redis-server
Docker镜像创建和运行continer步骤
############################
Image ID - f419c79d6ce7
Image Name - myapp
Image ID - 739b59b96069
Image Name - redis
#############################
docker run -d --net "redis" --name redis redis
docker run -d -p 8081:8081 myapp
######### --------------error ----------------########
Error: getaddrinfo ENOTFOUND redis-server
我认为这个错误与这个问题无关。但为了具体回答这个标题,您可以将image
与build
一起使用,它将用于标记构建的图像。在您的情况下:
ersion: '3'
services:
redis-server:
image: 'redis'
ports:
- "6379:6379"
node-app:
build: .
image: 'myapp'
ports:
- "8081:8081"
您还可以添加具有常见image
属性的标记aas。像myapp:v1
。
对于错误本身来说,这是由于图像在不同的网络中。