1. Docker Node.js 이미지 다운로드
Docker에서 Node.js 이미지를 다운로드하고 새로운 이미지를 빌드하기 위해서 먼저 기본 Node.js이미지를 다운받아야 합니다. 프로젝트 디렉토리 터미널에서 아래 명령어로 Node.js 이미지를 다운받습니다.
docker pull node
// 특정 Node.js 버전 이미지 다운로드
docker pull node:14 # ex. Node.js 14 버전
2. 새로운 이미지 빌드
새로운 이미지를 빌드하기위해 Dockerfile을 작성해야합니다. 프로젝트 디렉토리에 Dockerfile을 생성하고 아래 Dockerfile에 다음 사항을 추가해줍니다.
mkdir Docker-images-Download
cd Docker-images-Download
touch Dockerfile
# Base image
FROM node:14
# Set the working directory
WORKDIR /the/src/app
# Copy package.json and package-lock.json
COPY package*.json ./
# Install dependencies
RUN yarn install
# Copy the rest of the application code
COPY . .
# Expose the application port
EXPOSE 3000
# Command to run the application
CMD ["node", "app.js"]
3. Node.js 세팅
nodejs 프로젝트를 세팅하고 package.json과 app .js파일을 다음과 같이 수정합니다.
// package.json
{
"name": "docker-images-download",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "node app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "commonjs",
"dependencies": {
"express": "^4.21.2"
}
}
// app.js
const express = require("express");
const app = express();
const port = 3000;
app.get("/", (req, res) => {
res.send("Hello, Nodejs & Docker!");
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});
4. 이미지 빌드
이제 docker를 사용해 이미지를 빌드해야합니다. 터미널에 다음 명령어로 이미지를 빌드합니다.
docker build -t docker-images .
빌드가 완료되었다면 다음 명령어로 Docker 컨테이너를 실행합니다.
docker run -p 3000:3000 docker-images
위 명령어를 입력하고 http://localhost:3000으로 접속하면 "Hello, Nodejs & Docker!"가 뜨는 것을 볼 수 있습니다.
GitHub - Koras02/docker-node-bloging: https://thinky.tistory.com/category/Back-End/Docker
https://thinky.tistory.com/category/Back-End/Docker - Koras02/docker-node-bloging
github.com
LIST
'Back-End > Docker' 카테고리의 다른 글
[Docker] 2장 도커 이미지 확인 (1) | 2025.02.27 |
---|---|
[Docker] 1장 Docker란 무엇인가? (0) | 2025.02.25 |