ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Python application with Docker Image build and Run
    Tech/Python 2019. 7. 14. 16:34
    반응형

    - create python app - (app.py)

    """
    샘플 app.py 에서는 아래 lib 가 필요 없으나, lib 설치 예시를 위해 작성 한것.
    """
    import aiohttp
    import yaml
    from aiohttp import ClientSession
    from bs4 import BeautifulSoup
    import asyncio
    
    
    def hello(_str):
        print(_str)
    
    if __name__ == '__main__':
        hello("Hello! Docker")

    - created requirements.txt

    aiohttp==3.5.4
    beautifulsoup4==4.7.1
    PyYAML==5.1.1

    - created Dockerfile - (DockerFile)

    # 파이썬 3.7 이미지
    FROM python:3.7
    
    # Docker image 에서 작업을 수행할 디렉터리 설정
    WORKDIR /app
    
    # 작업 디렉터리로 설치될 라이브러리 파일 복사 
    COPY requirements.txt /app/
    
    # 라이브러리 파일 로부터 설치
    RUN pip install --trusted-host pypi.python.org -r requirements.txt
    
    # 현재 디렉터리에 있는 파일들을 도커 이미지에 추가
    # COPY 는 파일을 이미지에 추가, 압축 파일 해제 안함. URL 사용 불가.
    # COPY 와 비슷한 ADD 의 경우 압축파일 해제 후 추가, URL 사용 가능
    COPY . /app
    
    #컨테이너안에서 실행 되어야 할 명령어 
    #docker run --entrypoint 옵션 사용시 해당 옵션 무시된다.
    ENTRYPOINT ["python"]
    
    #컨테이너안에서 실행 되어야 할 파이썬 스크립트
    #docker run 실행시 바로 명령어를 입력하는 경우 CMD 는 실행 되지 않는다.
    CMD ["app.py"]
    
    #CMD 의 명령어를 ENTRYPOINT 에 전달 하도록 구성
    

    - create Docker ignore file (.dockerignore)

    [도커 이미지 빌드시 ignore 처리할 파일 작성]

    .venv
    .vscode
    .gitignore
    README.md

    - Docker image build

    # hello 이미지로 현재 디렉토리를 빌드한다.
    # . (dot) -> 현재 디렉터리를 의미
    # -t: 이미지 명칭과 태그 지정하는 옵션 (생략할 경우 latest)
    docker build -t hello:hello .

    - Docker image run

    Docker run hello:hello

     

    반응형

    'Tech > Python' 카테고리의 다른 글

    Start Django project with Python DI manager poetry  (0) 2021.08.22
    python data class  (0) 2020.01.19
    python file line read and set  (0) 2019.06.15
    pymongo usage  (0) 2019.06.08
    Commands for Python with Git project  (0) 2019.02.10
Designed by Tistory.