Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

파이썬 개발환경 구성하기의 끝판왕 - Docker Compose

4,706 views

Published on

2018년 5월 26일에 격월 파이썬 세미나에서 발표했던 내용입니다.

Published in: Technology
  • Be the first to comment

파이썬 개발환경 구성하기의 끝판왕 - Docker Compose

  1. 1. 1
  2. 2. 2
  3. 3. 3
  4. 4. 4
  5. 5. easy_install pip setup.py install 5
  6. 6. 6
  7. 7. $ pip install -r requirements.txt 7
  8. 8. 8
  9. 9. $ pip install -r requirements.txt ... build/temp.macosx-10.11-x86_64-3.5/_openssl.c:400:10: fatal error: 'openssl/aes.h' file not found #include <openssl/aes.h> ^ 1 error generated. error: command 'clang' failed with exit status 1 9
  10. 10. ... ld: library not found for -lpq collect2: ld returned 1 exit status ld: library not found for -lpq collect2: ld returned 1 exit status error: command 'clang' failed with exit status 1 10
  11. 11. ... building '_imaging' extension creating build/temp.macosx-10.8-intel-2.7 creating build/temp.macosx-10.8-intel-2.7/libImaging ... unable to execute clang: No such file or directory error: command 'clang' failed with exit status 1 11
  12. 12. ... Skipping installation of /Library/Python/2.7/site-packages/ virtualenvwrapper/init.py (namespace package) copying virtualenvwrapper/hook_loader.py -> /Library/Python/ 2.7/site-packages/virtualenvwrapper error: /Library/Python/2.7/site-packages/virtualenvwrapper: Permission denied 12
  13. 13. export XXXXXXX=-xxxxxx 13
  14. 14. $ sudo pip install xxx 14
  15. 15. $ brew install xxx 15
  16. 16. ImportError: No module named xxx 16
  17. 17. apt install xxx 17
  18. 18. 18
  19. 19. 19
  20. 20. 20
  21. 21. 21
  22. 22. 22
  23. 23. 23
  24. 24. 24
  25. 25. 25
  26. 26. 26
  27. 27. 27
  28. 28. 28
  29. 29. 29
  30. 30. 30
  31. 31. 31
  32. 32. 32
  33. 33. 33
  34. 34. 34
  35. 35. $ docker run -it python:3.7.0b4-stretch Unable to find image 'python:3.7.0b4-stretch' locally 3.7.0b4-stretch: Pulling from library/python cc1a78bfd46b: Extracting 16.52MB/45.32MB 6861473222a6: Download complete 7e0b9c3b5ae0: Download complete 3ec98735f56f: Downloading 14.75MB/50.03MB 9b311b87a021: Downloading 57.12MB/213.2MB 048165938570: Download complete b98bafe8d054: Pulling fs layer 9e173e93364f: Pulling fs layer 279629a74d4e: Waiting 35
  36. 36. $ docker run -it python:3.7.0b4-stretch Unable to find image 'python:3.7.0b4-stretch' locally 3.7.0b4-stretch: Pulling from library/python cc1a78bfd46b: Pull complete 6861473222a6: Pull complete 7e0b9c3b5ae0: Pull complete 3ec98735f56f: Pull complete 9b311b87a021: Pull complete 048165938570: Pull complete b98bafe8d054: Pull complete 9e173e93364f: Pull complete 279629a74d4e: Pull complete Digest: sha256:4b4314d7020b5b2730ddc1fbe5be3a6f2f6cd2478156bc652a37bec012ff89dd Status: Downloaded newer image for python:3.7.0b4-stretch Python 3.7.0b4 (default, May 5 2018, 02:57:38) [GCC 6.3.0 20170516] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 36
  37. 37. 37
  38. 38. 38
  39. 39. 39
  40. 40. $ docker run -it python:3 Python 3.6.5 (default, May 5 2018, 03:09:35) [GCC 4.9.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 1 + 1 2 >>> 40
  41. 41. $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4fb53528e038 python:3 "python3" 2 seconds ago Up 1 second flamboyant_lichterman $ docker logs 4fb53528e038 Python 3.6.5 (default, May 5 2018, 03:09:35) [GCC 4.9.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 1 + 1 2 $ 41
  42. 42. $ docker exec -it 4fb53528e038 bash root@4fb53528e038:/# ls bin dev home lib64 mnt proc run srv tmp var boot etc lib media opt root sbin sys usr root@4fb53528e038:/# 42
  43. 43. $ docker run -it python:3 $ docker logs 4fb53528e038 python:3 4fb53528e038 43
  44. 44. $ docker run -it python:3 Python 3.6.5 (default, May 5 2018, 03:09:35) [GCC 4.9.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> exit() $ docker run -it python:3 bash root@b1ae1f2de579:/# 44
  45. 45. $ docker run -it --env CONF_NAME=pycon_seminar python:3 bash root@04cc7afe7362:/# echo $CONF_NAME pycon_seminar root@04cc7afe7362:/# 45
  46. 46. $ ls djangosample __init__.py settings.py urls.py wsgi.py $ docker run -it --volume `pwd`/djangosample:/djangosample python:3 bash root@04cc7afe7362:/# ls /djangosample __init__.py settings.py urls.py wsgi.py root@04cc7afe7362:/# 46
  47. 47. 47
  48. 48. 48
  49. 49. python:3 --volume bash --env 49
  50. 50. 50
  51. 51. 51
  52. 52. FROM python:3 RUN apt-get update && apt-get -y install libpq-dev ADD ./djangosample /app/ WORKDIR /app CMD ["python", "manage.py", "runserver", "0:8000"] 52
  53. 53. $ git clone https://github.com/raccoonyy/django-sample-for-docker-compose $ cd django-sample-for-docker-compose $ docker build -t my-django . 53
  54. 54. $ docker run -d --env POSTGRES_DB=djangosample --env POSTGRES_USER=sampleuser --env POSTGRES_PASSWORD=samplesecret --name db postgres $ docker run -it --env DJANGO_DEBUG=True --env DJANGO_DB_HOST=postgres --link db:postgres --publish 8000:8000 my-django $ open http://127.0.0.1:8000 54
  55. 55. --name db db --link db:postgres db postgres --publish 8000:8000 55
  56. 56. $ mkdir data $ docker run -d --env POSTGRES_DB=djangosample --env POSTGRES_USER=sampleuser --env POSTGRES_PASSWORD=samplesecret --name db --volume data:/var/lib/postgresql/data <-- New! postgres 56
  57. 57. $ docker run -d --env POSTGRES_DB=djangosample --env POSTGRES_USER=sampleuser --env POSTGRES_PASSWORD=samplesecret --name db --volume `pwd`/data:/var/lib/postgresql/data --rm <-- New! postgres 57
  58. 58. $ docker run -it --env DJANGO_DEBUG=True --env DJANGO_DB_HOST=postgres --link my-postgres:postgres --publish 8000:8000 --volume `pwd`:/app <-- New! my-django 58
  59. 59. 59
  60. 60. $ docker-compose up 60
  61. 61. version: '3' volumes: django_sample_db_dev: {} services: db: image: postgres volumes: - django_sample_db_dev:/var/lib/postgresql/data environment: - POSTGRES_DB=sampledb - POSTGRES_USER=sampleuser - POSTGRES_PASSWORD=samplesecret django: build: context: . dockerfile: ./compose/django/Dockerfile-dev environment: - DJANGO_DEBUG=True - DJANGO_DB_HOST=postgres - DJANGO_DB_PORT=5432 - DJANGO_DB_NAME=sampledb - DJANGO_DB_USERNAME=sampleuser - DJANGO_DB_PASSWORD=samplesecret - DJANGO_SECRET_KEY=dev_secret_key ports: - "8000:8000" depends_on: - db links: - db:postgres command: ["/wait-for-it.sh", "postgres:5432", "-t", "10", "--", "/start-dev.sh"] volumes: - ./djangosample:/app/djangosample 61
  62. 62. version: '3' volumes: django_sample_db_dev: {} services: db: image: postgres volumes: - django_sample_db_dev:/var/lib/postgresql/data environment: - POSTGRES_DB=sampledb - POSTGRES_USER=sampleuser - POSTGRES_PASSWORD=samplesecret 62
  63. 63. django: build: context: . dockerfile: ./compose/django/Dockerfile-dev environment: - DJANGO_DEBUG=True - DJANGO_DB_HOST=postgres - DJANGO_DB_PORT=5432 - DJANGO_DB_NAME=sampledb - DJANGO_DB_USERNAME=sampleuser - DJANGO_DB_PASSWORD=samplesecret - DJANGO_SECRET_KEY=dev_secret_key ports: - "8000:8000" depends_on: - db links: - db:postgres volumes: - ./:/app/ 63
  64. 64. python:3 image build --volume volumes --env environment --link links depends_on --publish ports 64
  65. 65. 65
  66. 66. $ docker-compose ps Name Command State Ports --------------------------------------------------------------------------------------------------------- django-sample-for-docker-compose_db_1 docker-entrypoint.sh postgres Up 5432/tcp django-sample-for-docker- bash -c python manage.py m ... Up 0.0.0.0:8000->8000/tcp compose_django_1 66
  67. 67. $ docker-compose up $ docker-compose up -d 67
  68. 68. $ docker-compose logs [service name] $ docker-compose logs -f [service name] 68
  69. 69. $ docker-compose exec django bash root@3707e50b0c9a:/app# 69
  70. 70. $ docker-compose down Stopping django-sample-for-docker-compose_django_1 ... done Stopping django-sample-for-docker-compose_db_1 ... done Removing django-sample-for-docker-compose_django_1 ... done Removing django-sample-for-docker-compose_db_1 ... done Removing network django-sample-for-docker-compose_default $ docker-compose down -v 70
  71. 71. 71
  72. 72. # docker-compose.yml django: ... stdin_open: true tty: true $ docker attach CONTAINER_ID 72
  73. 73. bash command # docker-compose.yml service: ... django: ... command: - bash - -c - | python manage.py migrate python manage.py runserver 0:8000 73
  74. 74. # Dockerfile ENV PYTHONUNBUFFERED 0 74
  75. 75. $ docker-compose up --build $ docker-compose up --force-recreate 75
  76. 76. 76
  77. 77. 77
  78. 78. 78
  79. 79. 79
  80. 80. 80

×