Docker

JunePyo Suh·2020년 6월 24일
1

What is Docker?

Docker is a set of platform as a service products that facilitates application deployment process by using os-level virtualization and delivering software in packages called containers.

How Docker works

Docker containers act like little micro computers with very specific jobs, each with their own operating system and their own isolated CPU processes, memory, and network resources.

Thanks to such isolation, each container can be easily added, removed, stopped, or started again without affecting each other or the host machine. Each container usually runs one specific task such as a MySQL database or a Django application. The containers are then networked together and potentially scaled.

DockerHub

DockerHub is an online cloud repository of Docker containers. From Dockerhub you can pull an image containing a pre-configured environment for your specific programming language and framework.

Docker vs Virtual Machine

Docker is a form of virtualization, but unlike previous Virtual Machines, the resources are shared directly with the host. This feature allows you to run many Docker containers concurrently, whereas you may only run a few virtual machines at the same time. A vritual machine has to quarantine a set amount of resources such as memory and proecessing power. It also emulates hardware, so a user needs to boot an entire operating system when using a VM.

The VM communicates with the host computer via a translator application running on the host os, called "hypervisor." On the other hand, Docker communicates natively with the host system kernel, bypassing the middleman on Linux or Windows machines.

Morever, Docker uses less disk space, as it is able to reuse files efficiently by using a layered file system. If you have multiple docker images using the same base image, Docker will only keep a single copy of the files needed and share them with each container.

How to use Docker

  • Create Docker file
#./Dockerfile
FROM python:3 
#기반이 될 이미지

# 작업디렉토리(default)설정
WORKDIR /usr/src/app 

## Install packages
#현재 패키지 설치 정보를 도커 이미지에 복사
COPY requirements.txt ./ 
#설치정보를 읽어 들여서 패키지를 설치
RUN pip install -r requirements.txt

## Copy all src files
#현재경로에 존재하는 모든 소스파일을 이미지에 복사
COPY . . 


## Run the application on the port 8080
#8000번 포트를 외부에 개방하도록 설정
EXPOSE 8000   


#CMD ["python", "./setup.py", "runserver", "--host=0.0.0.0", "-p 8080"]
#gunicorn을 사용해서 서버를 실행
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "example.wsgi:application"]  
  • Build an image using 'docker build' command
docker build -t '도커허브에 가입한 계정명'/'이미지명(프로젝트명 권장)':'버전' .
ex) docker build -t wecode/wecodeproject:0.1.0 .
  • Verify your image's existence with 'docker images'
  • With your built image, you can run a container of that image or push it to the cloud to share with others.
docker run --name '컨테이너 명' -d'데몬으로 실행하기 위한 옵션' -p '호스트 포트':'컨테이너 포트' '이미지명'
ex) docker run --name project01 -d -p 8000:8000 project/djangoproject:0.1.0
  • If you don't want to create your own Docker image and just want to use a pre-made one, you can pull one from DockerHub using 'docker pull'

Docker increases deployment speed.
Initially, we would create EC2, initialize settings, git clone, and run server.
With Docker, first create an image with all the necessary settings, then simply import the container to an Amazon server.

Docker Compose and Swarm

Docker compose is a container management service that enables a user to define an application stack for a variety of containers and run the stack.
Docker swarm is a clustering tool for docker containers.

Docker Deployment Steps

  1. vi Dockerfile

  2. requirements.txt 에 gunicorn 없으면 추가하기 (의존성 정보)

  3. my_settings.py 혹은 .env 에 db 접속정보가 내 로컬이 아닌 amazon rds 여야 한다.

  4. docker build -t myID/project_name:0.1.0 .
    실행 됬는지 궁금하면 http 한번 해보기

  5. docker push myID/project_name:0.1.0
    (docker hub 에 push 가 일어남)

  6. ec2 login

  7. install docker again (but don't forget that it's ubuntu)

  8. sudo docker login

  9. sudo docker pull myID/project_name:0.1.0

  10. sudo docker run --name project01 -d -p 8000:8000 myID/project_name:0.1.0
    (container 실행)

  11. sudo docker ps -a 해서 체크

  12. sudo docker stop container_name

0개의 댓글