도커 image를 다른 서버에서 활용하고 싶다면 Docker hub에 Push해서 다른 서버에서 pull해서 가져갈 수 있게 할 수 있다.
Dockerfile이란?
dockerfile은 Docker image 설정 파일이다.
dockerfile은 확장자가 따로 없다.
Docker는 Dockerfile의 명령문들을 차례대로 수행해 DockerImage를 생성한다.
Dockerfile 작성하기
- 파일 이름을 'Dockerfile'로 해야 한다.
FROM : 베이스 이미지 선택
WORKDIR : 작업 디렉토리 설정
COPY : 파일 복사(호스트 시스템의 파일을 Docker 이미지로 복사)
RUN : 필요한 패키지, 종속성 설치
ENV : 환경 변수 설정
EXPOSE : 포트번호 설정
CMD : 컨테이너가 시작될 때 실행될 명령 정의
# 베이스 이미지 선택
FROM python:3.9-slim
# 필요한 패키지 설치
RUN pip install fastapi uvicorn
# 애플리케이션 소스 코드 복사
# 현재 디렉토리의 모든 파일을 컨테이너 내의 '/app' 디렉토리로 복사
COPY . /app
# 작업 디렉토리 설정
WORKDIR /app
# 애플리케이션 실행
# Uvicorn을 사용해 FastAPI 실행, 8000번 포트에서 실행
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
#CMD로 port번호를 지정하지 않고
#EXPOSE 8000으로 해도 된다.
Docker Image Build
현재 디렉토리에 있는 Dockerfile을 사용해 이미지 빌드하고 'mywebserver:1.0'이라는 태그 부여
docker build --tag cowboysj/mywebserver:1.0 .
image 이름 : mywebserver
이미지 버전 : 1.0
. : 현재 디렉토리에서 Dockerfile을 사용해 이미지 빌드
Docker Image Run
docker run --name mywebserver -d -p 8000:8000 cowboysj/myewbserver:1.0
"cowboysj/mywebserver:1.0" 이미지에서 mywebserver라는 새로운 컨테이너가 생성되고, 이 컨테이너는 포그라운드 모드로 실행된다.
8000:8000 : 호스트의 8000번 포트와 컨테이너의 8000번 포트가 매핑되고, 이를 통해 컨테이너 내부의 애플리케이션에 접근할 수 있게 된다.
-d : 컨테이너를 백그라운드에서 실행하도록 하는 옵션(detached 모드로 실행 => 콘솔 차지하지 않고 다른 작업을 수행할 수 있다.)
-p : 포트포워딩 host port : comtainer port (host가 8000번 포트로 접근하면 컨테이너의 8000번 포트로 연결된다.)
--name : 컨테이너 이름
😭도커 이미지 업로드 권한 에러😭
denied : requested access to the resource is denied
원인1 : docker hub에 로그인되어 있지 않을 경우
=> docker login 하고 명령어 실행
원인2 : image의 username과 dockerhub에 가입된 id가 일치하지 않을 경우
=> docker image 태그 설정을 할 때 userid를 앞에 안 붙일 때의 오류다.
docker image tag [다시 태그할 이미지] [새 태그명]
으로 이미지명을 바꿔주면 된다.
Docker Hub에 이미지 Push
방법1. Docker허브 GUI에서 직접 push
방법2. 명령어로 push
docker push cowboysj/mywebserver:1.0
'study > Cloud Computing' 카테고리의 다른 글
Docker Volume를 활용해 Jenkins 설치하기 (2) | 2024.04.15 |
---|---|
[AWS] AWS Well-Architected Tool이란? (4) | 2024.03.29 |