본문 바로가기
카테고리 없음

docker with nodejs

by pishio 2024. 7. 25.

Ubuntu 20.04 버전을 기반으로 Node.js 18과 Cordova를 설치하는 Dockerfile을 작성하겠습니다. 아래는 해당 Dockerfile입니다:

```Dockerfile
# Use the official Ubuntu 20.04 base image
FROM ubuntu:20.04

# Set the maintainer label
LABEL maintainer="your-email@example.com"

# Prevent prompts during package installations
ARG DEBIAN_FRONTEND=noninteractive

# Update the package list and install necessary packages
RUN apt-get update && apt-get install -y \
    curl \
    gnupg \
    build-essential \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Add NodeSource APT repository for Node.js 18
RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash -

# Install Node.js 18 and npm
RUN apt-get update && apt-get install -y nodejs

# Install Cordova globally
RUN npm install -g cordova

# Verify installations
RUN node -v && npm -v && cordova -v

# Set working directory
WORKDIR /app

# Default command
CMD ["bash"]
```

### Dockerfile 설명
1. **베이스 이미지 설정**:
    ```Dockerfile
    FROM ubuntu:20.04
    ```

2. **메인테이너 라벨 설정**:
    ```Dockerfile
    LABEL maintainer="your-email@example.com"
    ```

3. **환경 변수 설정**: 패키지 설치 중에 인터랙티브 프롬프트를 방지하기 위해 `DEBIAN_FRONTEND`를 설정합니다.
    ```Dockerfile
    ARG DEBIAN_FRONTEND=noninteractive
    ```

4. **필요한 패키지 설치**:
    ```Dockerfile
    RUN apt-get update && apt-get install -y \
        curl \
        gnupg \
        build-essential \
        && apt-get clean \
        && rm -rf /var/lib/apt/lists/*
    ```

5. **NodeSource APT 리포지토리 추가 및 Node.js 18 설치**:
    ```Dockerfile
    RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
    RUN apt-get update && apt-get install -y nodejs
    ```

6. **Cordova를 글로벌로 설치**:
    ```Dockerfile
    RUN npm install -g cordova
    ```

7. **설치 확인**:
    ```Dockerfile
    RUN node -v && npm -v && cordova -v
    ```

8. **작업 디렉토리 설정**:
    ```Dockerfile
    WORKDIR /app
    ```

9. **기본 명령어 설정**:
    ```Dockerfile
    CMD ["bash"]
    ```

### Docker 이미지 빌드 및 실행
1. 터미널에서 Dockerfile이 있는 디렉토리로 이동한 후 다음 명령어를 실행하여 이미지를 빌드합니다:
    ```sh
    docker build -t my-node-cordova-image .
    ```

2. 빌드가 완료되면 다음 명령어를 사용하여 컨테이너를 실행합니다:
    ```sh
    docker run -it my-node-cordova-image
    ```

이 명령어는 생성된 이미지를 기반으로 컨테이너를 실행하고, 셸에 들어가도록 합니다.

댓글