Today I Learn - 44

이정빈·2021년 4월 20일
0

클라우드 엔지니어

목록 보기
45/53
post-thumbnail

Django with Docker Container

호스트 os에 장고의 사전 설정을 해서 복사하여 컨테이너에 올린다.
컨테이너 내부에서 장고 설정을 해도 상관없다. 도커파일에서 경로를 지정하여 이미지로 패킹할 수 있다. 도커파일과 장고의 프로젝트는 같은 디렉토리에 있어야 한다.

sudo apt install python3-pip

mkdir django-hello && cd django-hello

virtualenv venv

pip3 install django

django-admin startproject helloworld

cd helloworld

vi helloworld/settings.py

...

DEBUG = False

ALLOWED_HOSTS = ['*']

INSTALLED_APPS = [
...
'hello.apps.HelloConfig',
]
...

python3 manage.py startapp hello

mkdir hello/templates

vi hello/templates/hello.html
...

<#h1>
hello world
<#/h1>

vi hello/views.py

...
...
def hello(request):
return render(request, 'hello.html')

vi helloworld/urls.py

...
import hello.views

urlpatterns = [
...
path('', hello.views.hello, name='hello'),

]

python3 manage.py runserver 0.0.0.0:8000

cd ..

pip3 freeze > requirements.txt

docker run -itd --name pybuild python:alpine

docker cp django-helloworld pybuild:django-helloworld

docker commit -m 'MSG' -c 'CMD python3 /django-helloworld/helloworld/manage.py runserver 0.0.0.0:8000' pybuild pyhello:v1

Flask with Docker Image

mkdir flask-hello && cd flask-hello
sudo pip3 install virtualenv
virtualenv venv
pip3 install flask
pip3 freeze > requirements.txt
vi hello.py
from flask import Flask

app = Flask(__name__)
@app.route("/")

def hello():
    return "Hello Flask World!"

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=int("5000"))
docker run -itd --name fbuild python:alpine
docker exec fbuild mkdir /app
docker cp hello.py fbuild:/app/hello.py
docker cp requirements.txt fbuild:/app/requirements.txt
docker exec fbuild pip3 install -r /app/requirements.txt
docker commit -m 'Add Flask App' -c "CMD python3 /app/hello.py" fbuild flask-hello:v1
docker run -d -p 80:5000 flask-hello:v1

Dockerfile

  • FROM: Base 이미지 지정 (반드시 존재해야 함)
  • RUN: 컨테이너 내에서 실행할 명령어 (docker exec)
  • ADD/COPY: 호스트에서 컨테이너로 파일복사 (docker cp)
  • ENV: 쉘 환경 변수
  • WORKDIR: 작업 디렉토리 변경 (RUN, ADD, COPY, CMD, ENTRYPOINT)
  • CMD: 실행 할 Application
  • ENTRYPOINT: 실행 할 Application
  • EXPOSE: 애플리케이션 노출 포트(애플리케이션 설정X, 기본 TCP)
  • VOLUME: 볼륨 사용할 경로
FROM centos:7
RUN yum -y install httpd
ADD index.html /var/www/html/index.html
ENV A 100
ENTRYPOINT ["/usr/sbin/httpd"]
CMD ["-D", "FOREGROUND"]
EXPOSE 80
VOLUME /var/www/html
docker build -t <IMAGE>:<TAG> .

CMD 및 ENTRYPOINT 변경

docker run --entrypoint <ENTRYPOINT> <IMAGE> <CMD>

CMD, ENTRYPOINT, RUN 실행 형식

Shell 형식

CMD ls -l

Exec 형식(*)

CMD ['ls', '-l']

Exec 형식에서 Shell을 사용해야 하는 경우(*)

CMD ['/bin/sh', '-c', 'ls', '-l']

주의: exec 형식인 경우 실행 파일을 절대 경로로 표현!

예) ENTRYPOINT , CMD 같이 사용하는 예

ENTRYPOINT python3 /django-helloworld/helloworld/manage.py runserver
CMD 0.0.0.0:8000
docker run -d xyz 0.0.0.0:5000
  • ubuntu 이미지에 apache 설치 이미지
  • python 이미지에 flask/django 앱 설치 이미지

Ubuntu Timezone 설정

단순 유예

ENV DEBIAN_FRONTEND=noninteractive

Timezone 설정

RUN DEBIAN_FRONTEND=noninteractive apt install tzdata
ENV TZ Asia/Seoul
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN dpkg-reconfigure --frontend noninteractive tzdata

Ubuntu with Apache Image

FROM ubuntu:focal
RUN apt update

RUN DEBIAN_FRONTEND=noninteractive apt install tzdata
ENV TZ Asia/Seoul
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localyime && echo $TZ > /etc/timezone
RUN dpkg-reconfigure --frontend noninteractive tzdata

RUN apt install -y apache2
ADD index.html /var/www/html/index.html
ENTRYPOINT ["/usr/sbin/apachectl"]
CMD ["-D", "FOREGROUND"]
EXPOSE 80
VOLUME /var/www/html

이미지 빌드 시 캐시 사용하지 않기

docker build --no-cache -t ubuntu-apache:v1.3 .

C with Docker Image

#include <stdio.h>

int main() {
  printf("Hello C World!\n");
  return 0;
}
sudo apt install gcc
gcc hello.c -o hello
./hello

Dockerfile

FROM ubuntu:focal
ADD hello /hello
CMD ["/hello"]
docker build -t chello .
docker run chello

실행 안됨

ldd hello
mkdir -p lib/x86_64-linux-gnu
mkdir lib64
cp /lib/x86_64-linux-gnu/libc.so.6 lib/x86_64-linux-gnu/
cp /lib64/ld-linux-x86-64.so.2 lib64/

Dockerfile

FROM scratch
ADD hello /hello
ADD lib /lib
ADD lib64 /lib64
CMD ["/hello"]
docker build -t chello .

이미지 내부 파일 경로

/hello
/lib/x86_64-linux-gnu/libc.so.6
/lib64/ld-linux-x86-64.so.2

스태틱 바이너리

gcc hello.c --static -o hello-static
ldd hello-static

Dockerfile

FROM scratch
ADD hello-static /hello-static
CMD ["/hello-static"]
docker build -t cstatichello .
docker run cstatichello
profile
WAS Engineer, Cloud Engineer(지망)

0개의 댓글