코드로 우주평화

docker-compose 작성하면서 생긴 이슈 세 가지 (feat. m1) 본문

나는 이렇게 학습한다/Debug

docker-compose 작성하면서 생긴 이슈 세 가지 (feat. m1)

daco2020 2022. 5. 10. 14:50

 

docker-compose를 이용해 app과 db를 각각 컨테이너로 구현해보고자 했다.

도커를 처음 사용하다 보니 여러가지 문제들이 있었는데 그 중에서 가장 애먹었던 세 가지 이슈에 대해서 적어본다.

 

 

 

이슈1.

도커 빌드 중에 asyncpg만 유독 설치되지 않는 문제가 있었다.

 

원인 - 정확히는 알기 어려우나 m1 관련 이슈로 보인다.

해결 - 빌드 과정에서 gcc를 추가 설치하여 해결할 수 있었다.

# Dockerfile 에 추가

RUN apt-get install -y gcc 

 

gcc는 ‘다양한 프로그래밍 언어를 위한 컴파일러’ 라고 한다.

 

자세한 내용은 위키를 참고하기 바란다.

 

 

참고 링크

 

Error with pip install in Docker on Mac M1 when using Slim distribution of Python

I have an application I run on Docker which works fine on my older intel-based Mac. However, on the M1 Mac, I get pip install errors when running docker-compose up. e.g asyncpg==0.24.0 error: co...

stackoverflow.com

 

 

 

 

이슈2.

Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?

 

원인 - db연결 주소 중 호스트 이름을 ‘localhost’로 보기 때문에 생긴 문제

해결 - 컨테이너가 분리되어 있으므로 localhost가 아닌 service명을 기재해주어야 함


docker-compose를 작성하고 build를 하는 도중,

컨테이너까지 잘 생성하고 port 번호도 맞는데 계속해서 DB 연결을 실패했다.

 

port 번호도 바꾸고 이것저것 시도하던 중에 DB 연결 주소 중 ‘localhost’가 문제라는 것을 알게되었다.

app과 db의 환경이 컨테이너로 분리되어 있으므로 이제 'localhost'는 잘못된 호스트인 것이다.

 

service 이름 확인 방법

# docker-compose.yml

version: "3"

services:
	...

  postgres: # <- 요거
    platform: linux/amd64
    image: postgres:14.2-alpine
    ports:
      - 5432:5432
    environment:
      POSTGRES_DB: db_name
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password

 

수정 예시

# localhost -> postgres(위에서 확인한 서비스 이름)

"postgresql+asyncpg://user:password@localhost:5432/db_name"

>>> 

"postgresql+asyncpg://user:password@postgres:5432/db_name"

 

참고 링크

 

Docker: Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?

I am getting issues while setup and run the docker instance on my local system with Ruby on Rail. Please see my docker configuration files:- Dockerfile FROM ruby:2.3.1 RUN useradd -ms /bin/bash ...

stackoverflow.com

 

 

 

 

 

 

이슈3.

pg_connect(): Unable to connect to PostgreSQL server: SCRAM authentication requires libpq version 10 or above.

 

원인 - m1(arm64)으로 구축된 도커 이미지는 Linux 또는 Windows 기반 amd64 환경에 이미지를 배포할 때 문제가 생길 수 있음

해결 - 빌드 단계에서--platform 옵션으로 linux/amd64로 지정해줘야 함

 

 

다행히 해당 이슈에 대한 해결책을 금방 찾을 수 있었고,

다음 해결방법대로 수정하니 정상 실행되는 것을 확인할 수 있었다.

 

 

 

단일 도커 이미지를 빌드하는 경우 해결방법 (택 1)

1번, 환경 변수 설정

export DOCKER_DEFAULT_PLATFORM=linux/amd64

2번, Dockerfile FROM명령에 플래그 포함

FROM --platform=linux/amd64 python:3.7-alpine

 

 

도커 컴포즈로 빌드하는 경우 해결방법

platform: linux/amd64 를 각 서비스에 포함시킨다.

version: "3"

services:  
  frontend:  
    platform: linux/amd64 # <-
    build: frontend  
    ports:
      - 80:80  
    depends_on:
      - backend  
  backend:  
    platform: linux/amd64 # <-
    build: backend

 

참고 링크

 

Forcing docker to use linux/amd64 platform by default on macOS

Current beta version of docker requires you to specify a --platform=linux/amd64 each time you need to build or run an amd64 image/container. The documentation mentions When running an image with m...

stackoverflow.com