ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Python/Docker
    Language/Python 2022. 9. 2. 13:29

    1차에서는 간단히 도커관련된 부분만 정리

     

    docker config

    docker/flask/config/requirements.txt

    Flask==1.0.2
    Flask-Cors==3.0.7
    pandas==0.23.4
    matplotlib==3.0.2
    bezier==0.9.0
    boto3==1.9.57
    flask-restplus==0.12.1
    Flask-SQLAlchemy==2.3.2
    PyMySQL==0.9.2
    Flask-Login==0.4.1
    Flask-Security==3.0.0
    pytz==2018.7
    Pillow==5.3.0
    Flask-SocketIO==3.1.0
    gevent==1.3.7
    gevent-websocket==0.10.1
    openpyxl==2.5.12
    requests==2.21.0
    lxml==4.3.0

     

    docker/flask/config/start-flask.sh

    #!/usr/bin/env bash
    # source flask.env/bin/activate
    
    export PYTHONIOENCODING=utf-8
    # export MODE='dev'
    
    python /home/test-docker/test-pattern/run_app.py migrate

     

    docker/flask/Dockerfile

    # 베이스 이미지
    FROM python:3.7.1
    
    # 필요한 Config ROOT에 복사
    ADD ./flask/config  /
    
    # pip upgrade & requirements.txt 모듈 설치 & 실행권한 부여
    RUN pip install --upgrade pip && \
        pip install --no-cache-dir --requirement /requirements.txt && \
        chmod +x /*.sh
    
    ## CREATE TEST-DOCKER USER ##
    # Create the home directory for the new test-docker user.
    RUN mkdir -p /home/test-docker
    
    # Create an test-docker user so our program doesn't run as root.
    RUN groupadd -r test-docker &&\
      useradd -r -g test-docker -d /home/test-docker -s /sbin/nologin -c "Docker image user" test-docker
    
    # Set the home directory to our test-docker user's home.
    ENV HOME=/home/test-docker
    ENV APP_HOME=/home/test-docker/test-pattern
    
    ## SETTING UP THE APP ##
    RUN mkdir $APP_HOME
    WORKDIR $APP_HOME
    
    # ***
    # Do any custom logic needed prior to adding your code here
    # ***
    
    # Copy in the application code.
    ADD . $APP_HOME
    
    # Chown all the files to the test-docker user.
    RUN chown -R test-docker:test-docker $APP_HOME
    
    # Change to the test-docker user.
    USER test-docker
    
    # Port
    # EXPOSE 8080

     

    docker-compose

    docker-compose.yml

    version: '2.1'
    
    services:
      flask:
        extends:
          file: docker-compose-common.yml
          service: flask
        environment:
          MODE: 'default'
        ports:
          - 0.0.0.0:10001:10001
        expose:
          - "10001"

     

    docker-compose.common.yml

    version: '2.1'
    
    services:
      flask:
        image: image-test-pattern
        container_name: server-test-pattern
        build:
          context: .
          dockerfile: ./flask/Dockerfile
        volumes:
          - ../../test-pattern:/home/test-docker/test-pattern
        command: ["sh", "/start-flask.sh"]

     

    docker-compose.dev.yml

    version: '2.1'
    
    services:
      flask:
        extends:
          file: docker-compose-common.yml
          service: flask
        environment:
          MODE: 'dev'
        ports:
          - 0.0.0.0:10001:10001
        expose:
          - "10001"

     

    docker-compose.qa.yml

    version: '2.1'
    
    services:
      flask:
        extends:
          file: docker-compose-common.yml
          service: flask
        environment:
          MODE: 'qa'
        ports:
          - 0.0.0.0:10001:10001
        expose:
          - "10001"

     

    docker-compose.prd.yml

    version: '2.1'
    
    services:
      flask:
        extends:
          file: docker-compose-common.yml
          service: flask
        environment:
          MODE: 'prd'
        ports:
          - 0.0.0.0:10001:10001
        expose:
          - "10001"

     

    build

    docker/docker-build.sh

    #!/usr/bin/env bash
    docker build -t image-test-pattern:0.1 -f $PWD/flask/Dockerfile .

     

    docker/docker-run.sh

    #!/usr/bin/env bash
    HOSTNAME=`hostname -s`
    # IP_ADDR=`ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`
    
    # echo $HOSTNAME
    # echo $IP_ADDR
    
    run_script() {
        if [[ $HOSTNAME =~ ^test-dev ]]; then
            # docker-compose -f /home/test-docker/test-pattern/docker/docker-compose.dev.yml up -d --build
            docker run --name server-test-pattern -u 1000 -d -p 10001:10001 --read-only -v /home/test-docker/test-pattern:/home/test-docker/test-pattern --env MODE=dev image-test-pattern:0.1 sh /start-flask.sh
        elif [[ $HOSTNAME =~ ^test-qa ]]; then
            # docker-compose -f /home/test-docker/test-pattern/docker/docker-compose.qa.yml up -d --build
            docker run --name server-test-pattern -u 1000 -d -p 10001:10001 --read-only -v /home/test-docker/test-pattern:/home/test-docker/test-pattern --env MODE=qa image-test-pattern:0.1 sh /start-flask.sh
        elif [[ $HOSTNAME =~ ^test- ]]; then
            # docker-compose -f /home/test-docker/test-pattern/docker/docker-compose.prd.yml up -d --build
            docker run --name server-test-pattern -u 1000 -d -p 10001:10001 --read-only -v /home/test-docker/test-pattern:/home/test-docker/test-pattern --env MODE=prd image-test-pattern:0.1 sh /start-flask.sh
        else
            # docker-compose -f /d/dev/project-src/python-src/test-pattern/docker/docker-compose.yml up -d --build
            docker run --name server-test-pattern -d -p 10001:10001 --read-only -v $PWD/../../test-pattern:/home/test-docker/test-pattern --env MODE=default image-test-pattern:0.1 sh /start-flask.sh
        fi
    }
    
    docker stop server-test-pattern
    docker rm server-test-pattern
    run_script

    start

    run_app.bat

    : set FLASK_APP=./app_flask/views/web_controller_v4.py
    : set FLASK_DEBUG=1
    : python ./app_flask/views/web_controller_v4.py migrate
    : flask run
    python ./run_app.py migrate

     

    run_app.py

    #!/usr/bin/env python
    # encoding: utf-8
    import os
    from apps.app_flask import app
    # from apps.app_flask import socketio
    # from apps.app_flask import sio
    
    
    @app.route('/hello')
    def hello():
        return "Hello World!"
    
    
    if __name__ == '__main__':
        print('IP : ', os.getenv('IP', '0.0.0.0'))
        print('PORT : ', app.config['PORT'])
        print('DEBUG : ', app.config['DEBUG'])
    
        # sio.run(app, host='0.0.0.0', port=app.config['PORT'])
        # sio.run(app, host='0.0.0.0', port=app.config['PORT'], debug=True)  # 어째서인지 디버그모드가 아니면 메세지 한꺼번에 보내줌.
        # app.run(host='0.0.0.0', debug=app.config['DEBUG'], port=app.config['PORT'], threaded=True)
    
        app.run(host=os.getenv('IP', '0.0.0.0'),
                port=int(app.config['PORT']),
                threaded=True
                )

    댓글

Designed by Tistory.