[docker | postgres] Build a Backend REST API - 16

Hyeseong·2021년 2월 28일
0
post-custom-banner

Add postgres support to Dockerfile

장고가 도커와 커뮤니케이팅 하기 위해서 약간의 dependencies를 빌드해보도록 할게요.

requirements.txt

외부 패키지를 다운 받아야하는데요.
더불어 장고와 postgres간의 통신에 필요한 여러 의존성 패키지들도 설치해줘야 해요.

아래 한 줄을 삽입 해주도록 할게요.

psycopg2>=2.7.5,<2.8.0

변경 후

Django>=2.1.3,<2.2.0
djangorestframework>=3.9.0,<3.10.0
psycopg2>=2.7.5,<2.8.0

flake8>=3.6.0,<3.7.0

----------------------

Dockerfile

변경 전

FROM python:3.7-alpine
LABEL maintainer="someone@something.com"

ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt

RUN mkdir /app
WORKDIR /app
COPY ./app /app

RUN adduser -D user
USER user

변경 후

중간에 안보이던 apk명령어가 보이조.

  • 알파인의 패키지 관리자 명령어에요.(예. apt, apt-get)

아래 구체적인 --update --no-cache같은 옵션 및 특징들을 알고 싶다면 여기를 클릭해주세요.

FROM python:3.7-alpine
LABEL maintainer="someone@something.com"

ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt /requirements.txt
RUN apk add --update --no-cache postgresql-client
RUN apk add --update --no-cache --virtual .tmp-build-deps \
        gcc libc-dev linux-headers postgresql-dev
RUN pip install -r /requirements.txt
RUN apk del .tmp-build-deps

RUN mkdir /app
WORKDIR /app
COPY ./app /app

RUN adduser -D user
USER user

그리고 docker-compose build 명령어를 넣도록 할게요.

----------------------------

참고할 만한 도커컴포즈 명령어 👣

The following only builds the images, does not start the containers:

docker-compose build

The following builds the images if the images do not exist and starts the containers:

docker-compose up

If you add the --build option, it is forced to build the images even when not needed:

docker-compose up --build

The following skips the image build process:

docker-compose up --no-build

If the images aren't built beforehand, it fails.

The --no-cache option disables the Docker build cache in the image creation process. This is used to cache each layer in the Dockerfile and to speed up the image creation reusing layers (~ Dockerfile lines) previously built for other images that are identical.

profile
어제보다 오늘 그리고 오늘 보다 내일...
post-custom-banner

0개의 댓글