server.js 에서 설정한 것들이 잘 실행 되려면 선행적으로 redis-server가 잘 실행되어야 한다.
nodejs-docker-compose-app 디렉토리 생성 후
npm init 하여 엔터다발로 package.json을 만든다
{
"name": "nodejs-docker-compose-app",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start":"node server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies":{
"express":"4.17.1",
"redis":"3.0.2"
},
"author": "",
"license": "ISC",
"description": ""
}
server.js
const express = require("express");
const redis = require("redis");
//레디스 클라이언트 생성
const client = redis.createClient({
host:"redis-server",
port: 6379
})
const app = express();
//숫자는 0부터 시작합니다.
client.set("number",0);
app.get('/', (req,res) => {
client.get("number", (err,number) => {
// 현재 숫자를 가져온 후에 1씩 올려줍니다.
client.set("number",parseInt(number) + 1)
res.send("숫자가 1씩 올라갑니다. 숫자:" + number)
})
})
app.listen(8080)
console.log('server is running');
FROM node:10
WORKDIR /usr/src/app
# ./ = package.json ./ =server.js
COPY ./ ./
# npm install = 종속성 다운
RUN npm install
# 컨테이너가 시작될 때 실행할 커멘드
CMD ["node","server.js"]
레디스 서버를 먼저 실행
(base) hongtae@user:~/바탕화면/nodejs-docker-compose-app$ sudo docker run redis
이미지 생성후 컨테이너 실행
sudo docker run gusxoqkqh1/docker-compose-app
server is running
events.js:174
throw er; // Unhandled 'error' event
^
레디스가 레디스 서버와 연결이 실패했다.
Error: Redis connection to redis-server:6379 failed - getaddrinfo ENOTFOUND redis-server redis-server:6379
at GetAddrInfoReqWrap.onlookup as oncomplete
Emitted 'error' event at:
at RedisClient.on_error (/usr/src/app/node_modules/redis/index.js:341:14)
at Socket. (/usr/src/app/node_modules/redis/index.js:222:14)
at Socket.emit (events.js:198:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
에러 이유는 무엇일까?
컨테이너 간에 접근을 가능하게 해주는 무언가가 필요하다
노트 앱+ 레디스 클라이언트가 레디스 서버로 접근을 시도해야 한다.
version:"3"
services:
redis-server:
image: "redis"
node-app:
build: .
ports:
-"5000:8000"
도커가 설치 되어있는데 또 설치를 하라 한다..
(base) hongtae@user:~/바탕화면/nodejs-docker-compose-app$ docker-compose up
Command 'docker-compose' not found, but can be installed with:
sudo snap install docker # version 19.03.13, or
sudo apt install docker-compose # version 1.25.0-1
See 'snap info docker' for additional versions.
docker -v
Docker version 20.10.6, build 370c289
sudo apt install docker-compose
sudo docker-compose up
ERROR: yaml.scanner.ScannerError: mapping values are not allowed here
in "./docker-compose.yml", line 2, column 9
간격 잘 맞춰라
version: "3"
services:
redis-server:
image: "redis"
node-app:
build: .
ports:
- "5000:8080"
sudo docker-compose up
Starting nodejs-docker-compose-app_redis-server_1 ... done
Starting nodejs-docker-compose-app_node-app_1 ... done
Attaching to nodejs-docker-compose-app_node-app_1, nodejs-docker-compose-app_redis-server_1
redis-server_1 | 1:C 27 May 2021 07:47:51.513 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis-server_1 | 1:C 27 May 2021 07:47:51.513 # Redis version=6.2.3, bits=64, commit=00000000, modified=0, pid=1, just started
redis-server_1 | 1:C 27 May 2021 07:47:51.513 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis-server_1 | 1:M 27 May 2021 07:47:51.513 monotonic clock: POSIX clock_gettime
redis-server_1 | 1:M 27 May 2021 07:47:51.514 Running mode=standalone, port=6379.
redis-server_1 | 1:M 27 May 2021 07:47:51.514 # Server initialized
redis-server_1 | 1:M 27 May 2021 07:47:51.514 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis-server_1 | 1:M 27 May 2021 07:47:51.514 Loading RDB produced by version 6.2.3
redis-server_1 | 1:M 27 May 2021 07:47:51.514 RDB age 17 seconds
redis-server_1 | 1:M 27 May 2021 07:47:51.514 RDB memory usage when created 0.79 Mb
redis-server_1 | 1:M 27 May 2021 07:47:51.514 DB loaded from disk: 0.000 seconds
redis-server_1 | 1:M 27 May 2021 07:47:51.514 * Ready to accept connections
node-app_1 | server is running
server.js 에서 13 줄 client > client.get 으로 수정
다시 sudo docker-compose up --build
docker compose로 컨테이너 한꺼번에 멈추기
sudo docker-compose up -d (백그라운드 실행)
sudo docker-compose up -d --build
(base) hongtae@user:~/바탕화면/nodejs-docker-compose-app$ sudo docker-compose down
Stopping nodejs-docker-compose-app_node-app_1 ... done
Stopping nodejs-docker-compose-app_redis-server_1 ... done
Removing nodejs-docker-compose-app_node-app_1 ... done
Removing nodejs-docker-compose-app_redis-server_1 ... done
Removing network nodejs-docker-compose-app_default
(base) hongtae@user:~/바탕화면/nodejs-docker-compose-app$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f5917a9f29c3 redis "docker-entrypoint.s…" 51 minutes ago Up 51 minutes 6379/tcp elastic_diffie
df9c2edfb81c gusxoqkqh1/nodejs "docker-entrypoint.s…" 3 hours ago Up 3 hours 0.0.0.0:4000->8080/tcp, :::4000->8080/tcp frosty_shannon
1fe58523dd97 f1e8d1c19a48 "docker-entrypoint.s…" 5 hours ago Up 5 hours clever_davinci
374a124e4202 f1e8d1c19a48 "docker-entrypoint.s…" 5 hours ago Up 5 hours relaxed_wiles
(base) hongtae@user:~/바탕화면/nodejs-docker-compose-app$