불친절한 블로그

[Docker] docker-compose 내가 원하는 환경으로 만들기(연습중!!) 본문

IT Infrastructure

[Docker] docker-compose 내가 원하는 환경으로 만들기(연습중!!)

오누! 2020. 5. 20. 15:00
728x90
반응형

세팅해야하는 환경 : 파이썬, 쥬피터, DB(postgres), pgadmin -> docker-compose

 

docker 공식홈페이지 예제 실시

 

app.py

import time

import redis
from flask import Flask

app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)


def get_hit_count():
    retries = 5
    while True:
        try:
            return cache.incr('hits')
        except redis.exceptions.ConnectionError as exc:
            if retries == 0:
                raise exc
            retries -= 1
            time.sleep(0.5)


@app.route('/')
def hello():
    count = get_hit_count()
    return 'Hello World! I have been seen {} times.\n'.format(count)

requirements.txt

flask
redis

Dockerfile

FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP app.py
ENV FLASK_RUN_HOST 0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["flask", "run"]

docker-compose.yml

version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"

docker-compose up 입력

실행화면

docker 공식홈페이지에선 redis를 이용해서 웹화면을 띄우는 예제가 있다.

 

작성자는 웹을 띄울 필요가 없으니  내가 원하는 환경을 만들어보았다.

 

Dockerfile

# Dockerfile

FROM jupyter
MAINTAINER onu6024 <onu6024@naver.com>

RUN apt-get -y update && yes | apt-get upgrade
RUN apt-get install -y build-essential
RUN apt-get install -y python3 python3-pip openjdk-8-jre wget
RUN pip install --upgrade pip
WORKDIR /home/root/
RUN pip install jupyter
RUN jupyter notebook --generate-config --allow-root

EXPOSE 8888
ENTRYPOINT jupyter notebook --allow-root --ip=0.0.0.0 --port=8888 --no-browser

docker-compose.yml

version: '3'

services:
 python:
  image: python

 db:
  image: postgres
  volumes:
   - ./docker/data:/var/lib/postgresql/data
  environment:
   - POSTGRES_DB=sampledb
   - POSTGRES_USER=sampleuser
   - POSTGRES_PASSWORD=1234
   - POSTGRES_INITDB_ARGS=--encoding=UTF-8
 
 pgadmin:
  image: dpage/pgadmin4
 
 jupyter:
  build: ./Dockerfile

docker-compose up -d 입력

쥬피터 실행

docker run -it -p 8888:8888 -p 6006:6006 -v ~/docker:/data docker-compose-test_jupyter jupyter notebook --no-browser --ip=0.0.0.0 --allow-root --NotebookApp.token= --notebook-dir='/data'

실행화면

728x90
반응형
Comments