
이번 실습에서는 Node.js로 만든 간단한 웹 애플리케이션을 Docker로 감싸고, 이미지 레지스트리에 업로드하며, 인수 테스트까지 적용해보았습니다.
도커 이미지를 외부에서 사용할 수 있도록 하기 위해 DockerHub에 계정을 만들고, 레포지토리를 생성했습니다.
# DockerHub 로그인
docker login
# 이미지 태그 지정 (예: 사용자명/my-node-app)
docker tag my-node-app your-dockerhub-username/my-node-app:latest
# 이미지 푸시
docker push your-dockerhub-username/my-node-app:latest
이제 다른 환경에서도 docker pull your-dockerhub-username/my-node-app:latest를 통해 이미지를 사용할 수 있습니다.
Node.js의 http 모듈을 이용해 만든 API 서버가 정상 동작하는지 테스트합니다.
test.sh)#!/bin/bash
response=$(curl -s http://localhost:3000)
expected="Hello World"
if [[ "$response" == *"$expected"* ]]; then
echo "✅ 인수 테스트 통과"
exit 0
else
echo "❌ 인수 테스트 실패"
exit 1
fi
chmod +x test.sh
./test.sh
더 구조적인 테스트를 위해 jest 프레임워크를 적용해봤습니다.
npm install --save-dev jest supertest
app.test.js)const http = require('http');
describe('GET /', () => {
it('should return Hello World', (done) => {
const options = {
hostname: 'localhost',
port: 3000,
path: '/',
method: 'GET'
};
const req = http.request(options, (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => {
expect(data).toContain('Hello World');
done();
});
});
req.on('error', (e) => {
done(e);
});
req.end();
});
});
npx jest
package.json에 아래와 같이 스크립트를 등록해도 됩니다:"scripts": { "test": "jest" }
CI 환경에서도 쉽게 적용 가능하며, 코드 변경 후 자동 테스트 → 이미지 빌드 → 푸시하는 과정도 연결 가능해집니다.