https://docs.docker.com/language/python/develop/
도커 공식 홈페이지의 Python 튜토리얼의 글을 보고 작성한 글입니다,
1. 이미지 생성법
해당 튜토리얼은 간단한 Flask 앱 기반으로 Docker의 사용법을 제시합니다.
튜토리얼에서 제시하는 Flask App은 간단히 서버 호출시 hello, docker!를 반환하는 앱서버입니다 ;)

1-1) 이미지 생성시 사용되는 Dockerfile 생성
우선 미리보기로 Dockerfile 내부는 이렇게 생겼다
확장자는 없다
Dockerfile
# syntax=docker/dockerfile:1
FROM python:3.8-slim-buster
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]
차근차근 하나하나 뜯어 보도록 하자
1번줄
# syntax=docker/dockerfile:1
필수아님
#syntax 파서 지시문
파서 지시문이란
dockerfile 내용이 처리/해석되어야 하는 방법을 제시한다. 따라서 Dockerfile의 맨 위에 존재한다. 또한 공백또한 허용된다.
아래와 같은 지시문이 지원된다 (2가지 :: syntax, escape )
만약 오타가 나는경우에는 걱정하지 않아도된다. 인식하지 못하여 주석처리된다.
본격적인 의미는 버전1의 최신 버전을 가르킨다는 의미이다.
docker 튜토리얼에서 구문 업데이트를 파서 지시문을 이용하여 자동으로 확인하여 최신 버전을 사용하는것을 권장한다.
2번줄
FROM python:3.8-slim-buster
Docker hub에 이미 구축되어있는 공식 python 어플리케이션 구동이 가능한 도구와 패키지가 있는 패키지를 사용한다.
https://hub.docker.com/_/python
python - Official Image | Docker Hub
We and third parties use cookies or similar technologies ("Cookies") as described below to collect and process personal data, such as your IP address or browser information. You can learn more about how this site uses Cookies by reading our privacy policy
hub.docker.com
3번줄
WORKDIR /app
기본 DIR 상대 경로를 지정한다.
4번줄
COPY requirements.txt requirements.txt
이미지에 가져올 내용들을 복사한다.
현재 내가 사용하고있는것은 pipenv 환경이므로 pipfile 및 pipfile.lock 을 복사할듯 싶다
copy "from" "to"
앱 위치해있는 requirements.txt 에서 dockerfile 내부에 requirements.txt 파일을 생성하여 내용을 복사해서 옮긴다는 뜻이다
5번줄
RUN pip3 install -r requirements.txt
위에서 copy한 내용을 RUN 명령으로 실행한다
환경설정에 대한 파일을 가지고 있다면 이를 실행하여 바로 다운받을수 있는 로컬에서 사용하는 방식으로 적어두면 된다.
6번줄
COPY . .
위에서는 패키지 같은 것들( 종속성 )을 모두 옮겨 놓은것이라면
여기는 내 소스코드를 옮기는 명령어이다.
7번줄
CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]
컨테이너 내부에서 이미지를 실행시킬때 명령어이다.
참고로 튜토리얼의 Flask는 애플리케이션을 실행시킬때
python3 -m flask run
해당 명령어로 실행시킨다.
디렉터리 구조
python-docker
|____ app.py
|____ requirements.txt
|____ Dockerfile
이미지 구축
docker build --tag python-docker .
tag를 이용하여 image name 을 설정할 수 있다.
안하면 latest로 지정됨
이미지 확인 명령어
docker images # 생성된 이미지들 확인
docker tag python-docker:lastest python-docker:v1.0.0 # tag를 이용하여 tag 변경
python rmi python-docker:v1.0.0 # 이미지 삭제
Reference
https://docs.docker.com/engine/reference/builder/#parser-directives
Dockerfile reference
docs.docker.com
https://stackoverflow.com/questions/44453463/what-is-for-parser-directive-in-docker
What is for parser directive in docker
I read this link which explains the valid directives. And it says "Parser directive is a special form of comment" I really don't understand what is the need for this and if it is comment, why docker
stackoverflow.com
'TIL' 카테고리의 다른 글
[운영체제 1~2장] 파이썬과 자바가 속도차이가 나는 이유 (0) | 2022.09.15 |
---|---|
[Docker] 개발중인 Django Postgresql 프로젝트에 Dockerfile 생성 및 docker-compose 적용하기 (0) | 2022.08.25 |
[Docker] 기초내용 (1) - 이미지, 컨테이너, 구성 (0) | 2022.08.15 |
[Docker] Basic - Docker 기본 정리 (0) | 2022.07.03 |
[git Error] There isn’t anything to compare.main and master are entirely different commit histories. (0) | 2022.07.02 |