EC2에서 Docker와
Docker-compose 사용하기

AWSKRUG 변규현

2017.06.08

발표자 소개

  • Before
    • 전기공학 전공자
    • PCB 제조회사 설비기술팀
    • Parisien
  • Now
    • AFI 스타트업
    • Back-end programmer
    • Front-end programmer
    • CD/CI Management

개요

  • Docker란?
  • Docker의 사용방법
    • Docker image
    • Dockerfile
    • docker-compose
  • docker-hub에서
    automated-build 사용하기
  • docker image를 Elastic Beanstalk를 통해 배포하기
  • docker 사용 경험
  • 참고 자료

Docker란?

Docker is the world’s leading software container platform. Developers use Docker to eliminate “works on my machine” problems when collaborating on code with co-workers. Operators use Docker to run and manage apps side-by-side in isolated containers to get better compute density. Enterprises use Docker to build agile software delivery pipelines to ship new features faster, more securely and with confidence for both Linux and Windows Server apps.

 

https://www.docker.com/what-docker

Docker - Container란?

Using containers, everything required to make a piece of software run is packaged into isolated containers. Unlike VMs, containers do not bundle a full operating system - only libraries and settings required to make the software work are needed. This makes for efficient, lightweight, self-contained systems and guarantees that software will always run the same, regardless of where it’s deployed.

 

https://www.docker.com/what-docker

Docker - Container란?

Docker - Container란?

Package software into standardized units for development, shipment and deployment

A container image is a lightweight, stand-alone, executable package of a piece of software that includes everything needed to run it: code, runtime, system tools, system libraries, settings. Available for both Linux and Windows based apps, containerized software will always run the same, regardless of the environment. Containers isolate software from its surroundings, for example differences between development and staging environments and help reduce conflicts between teams running different software on the same infrastructure.

https://www.docker.com/what-container

Docker - Container란?

Comparing Containers and Virtual Machines

Docker - Container란?

Containers and Virtual Machines Together

그래서 간단히 Docker를 말하자면?

사용할 어플리케이션이 설치된 OS에
실행할 명령어들을 적용하여
빠르고 간단하게 나만의 이미지를
생성할 수 있는 도구!

무엇보다도 VM과 달리 가벼운게 장점!

Docker의 사용방법

  1. Docker image를 직접 실행

# docker hub에서 이미지 가져오기
$ docker pull ubuntu

# ubuntu image를 데몬으로 실행하기
$ docker run -dit --name myUbuntu ubuntu

# novemberde/node-pm2를 base image로 하는 docker container 실행하기
$ docker run -it -p 8080:3000 -v /home/docker/docker_node_server:/src \
    --name docker_node_server novemberde/node-pm2

# daemon으로 실행하기
$ docker run -dit -p 8080:3000 -v /home/docker/docker_node_server:/src \
    --name docker_node_server novemberde/node-pm2

# 실행하고 있는 docker container 확인하기
$ docker ps

# docker container 설정 사항 확인하기
$ docker inspect docker_node_server

# 모든 docker container 확인하기
$ docker ps -a

# 컨테이너 삭제하기
$ docker rm docker_node_server

Docker의 사용방법

  2. Dockerfile을 작성하여 사용

FROM novemberde/node-pm2

MAINTAINER KH BYUN "novemberde.github.io"

# RUN npm install -g pm2 node-gyp

ENV NODE_ENV production

EXPOSE 3000

COPY ./ /src

RUN npm install --prefix /docker_node_server

CMD ["pm2-docker", "/src/app.js"]

Docker의 사용방법

  3. docker-compose를 사용

api:
  build: ./
  ports:
    - "80:3000"
  log_opt: 
    max-size: 40m

Docker hub의 automated build

  1. Create > Create Automated build 들어가기

Docker hub의 automated build

  2. Bitbucket 선택 후 내용 채우기

Docker hub의 automated build

  3. 생성 완료

Docker hub의 automated build

  4. Trigger 해보기

Docker hub의 automated build

  5. build된 이미지를 실행해보기

# docker-compose를 사용할 경우
api:
  image: novemberde/docker_node_server
  ports:
    - "80:3000"
  log_opt: 
    max-size: 40m

# docker-cli 명령어로 사용할 경우
docker run -dit --name docker_node_server -p 80:3000 novemberde/docker_node_server

Docker image를 EB를 통해 배포하기

Docker image를 EB를 통해 배포하기

Docker를 어떻게 활용했을까?

  1. 배포환경과 테스트 환경을 분리하자.

    근데 중복된 아키텍쳐를 사용하면 돈이 줄줄 새지 않나?

  • ELB - EC2 - RDS 와 같은 기본적인 구조로 t2 micro 인스턴스를 생성하더라도 고정적인 지출이 발생한다.
  • DB정보는 Development 환경과 Production 환경을 분리하고 싶다.
  • NoSQL을 사용할 경우도 분리하고 싶다.
  • 그럼 하나의 개발용 EC2에 mariadb-container와 mongodb-container를 올리자.
  • local 환경에서 개발할 때 실제 배포서버에는 영향을 끼치지 않는다.

Docker를 어떻게 활용했을까?

  2. ElasticCache의 Cache를 갱신하고 싶은데 어떡하지?

  • ElasticCache는 Public IP가 할당되어 있지 않으며 Elastic IP를 부여할 수도 없다. 그럼 보통 어떻게 접근할까
    • VPC에서 Virtual Private Gateways를 열고 VPN connection을 통해 접근한다.  => 귀찮고 돈이 나간다.
    • 개발 서버 내의 인스턴스를 활용하여 VPN으로 접속한다. => 귀찮은 작업이다.
    • ElasticCache를 reboot 한다. => 그때동안 개발은?
  • VPC내의 인스턴스에서 redis-cli container를 통해 접근하여 명령어를 날린다. => 별도의 redis-cli 설치가 필요하지 않다.

Docker를 어떻게 활용했을까?

  3. 특정 서버가 실행될 때 사용되는 명령어를 모아둔다.

  • Dockerfile에는 container내에서 사용되는 명령어를 모아둔다.
  • docker-compose.yml에는 container를 실행할 때 사용되는 명령어를 모아둔다.
  • 배포할 때 여러 명령줄이 필요없이
    $ docker-compose up -d
    로 모든 프로세스를 시작할 수 있다.

Docker 참고 자료

감사합니다 !

EC2에서 Docker와 Docker-compose 사용하기

By BYUN Kyuhyun

EC2에서 Docker와 Docker-compose 사용하기

  • 1,722