본문 바로가기
서버 인프라/Docker

Docker 설치및 명령어 간단정리

by hyunleeM 2019. 9. 4.

CENTOS7 기준


1. 도커 설치
# 다운로드 설치
curl -fsSL https://get.docker.com/ | sudo sh
# yum 설치
yum install -y yum-utils device-mapper-persistent-data lvm2

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

yum install docker-ce

 


# 부팅시 실행하도록 등록
systemctl enable docker.service
# 도커 실행
systemctl start docker.service
# 상태 확인
systemctl status docker.service
# 확인
docker version

2. 도커 그룹 주기
# 도커를 사용하기 위해선 root권한이 필요함 특정 계정에 docker그룹을 주는것으로 해결
sudo usermod -aG docker your-user

3. 도커 compose 설치
# 도커의 복잡한 설정을 관리하기위해 docker-compose라는 yml파일로 관리 해줌 
# 사용하기 위해선 설치 필요
curl -L "https://github.com/docker/compose/releases/download/1.9.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
# test
docker-compose version

4. 도커 명령어 정리
# 컨테이너 실행
docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
-d : detached mode 흔히 말하는 백그라운드 모드
-p : 호스트와 컨테이너의 포트를 연결 (포워딩)
-v : 호스트와 컨테이너의 디렉토리를 연결 (마운트)
-e : 컨테이너 내에서 사용할 환경변수 설정
–name : 컨테이너 이름 설정
–rm : 프로세스 종료시 컨테이너 자동 제거
-it : -i와 -t를 동시에 사용한 것으로 터미널 입력을 위한 옵션
–link : 컨테이너 연결 [컨테이너명:별칭]
※ run 실행시 다운받은 이미지가 없는경우는 자동으로 pull해줌

# 이미지 목록 확인
docker images [OPTIONS] [REPOSITORY[:TAG]]
docker images
# 이미지 다운로드
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
# 이미지 삭제
docker rmi [OPTIONS] IMAGE [IMAGE...]
#컨테이너 로그
docker logs [OPTIONS] CONTAINER
# 컨테이너 프로세스 확인
docker ps
docker ps -a (모든 프로세스)
# 컨테이너 stop/start
docker stop/start 컨테너 아이디
# 컨테이너 삭제
docker rm 컨테이너 아이디
# 실행중인 컨테이너 접속
docker exec -it 컨테이너 아이디 command
ex) docker exec -it i3jir3 bash
# 이미지 빌드
docker build -t 이미지명:tag .
# 컨테이너 > 이미지 빌드
docker commit 컨테이너명 이미지명

5. 도커 컴포즈 명령어 정리
docker up -d
docker up build -d

 

예제)

톰캣 설치해서 서비스 올리기

1. 필요 파일 구성

  - 동일한 디렉토리에 필요한 파일 모두 ..

   docker-compose.yml

   Dockerfile

   logs(로그파일 볼륨 연결)

   app-1.0.0-SNAPSHOT.war

   start-tomcat.sh

2. Dockerfile작성

#version 1.0
FROM centos:7.6.1810
MAINTAINER hyunlee <이메일주소>

ARG war_path

RUN yum update -y
RUN yum install -y wget
RUN yum install -y java-1.8.0-openjdk-devel.x86_64

RUN useradd tomcat_user

USER tomcat_user 

RUN mkdir /home/tomcat_user/webapp

ENV TOMCAT_HOME=/home/tomcat_user/webapp/apache-tomcat-8.5.45/

WORKDIR /home/tomcat_user/webapp

RUN wget http://mirror.navercorp.com/apache/tomcat/tomcat-8/v8.5.45/bin/apache-tomcat-8.5.45.tar.gz
RUN tar zxvf apache-tomcat-8.5.45.tar.gz

COPY --chown=tomcat_user./start-tomcat.sh $TOMCAT_HOME/bin/start-tomcat.sh
RUN chmod 755 $TOMCAT_HOME/bin/start-tomcat.sh
COPY --chown=tomcat_user$war_path $TOMCAT_HOME/webapps/app-1.0.0-SNAPSHOT.war

EXPOSE 8080

CMD $TOMCAT_HOME/bin/start-tomcat.sh

 

2. start-tomcat.sh 작성

 server.xml을 수정하고 톰캣을 실행시킴

#!/bin/bash -e

HOST_CONTEXT='

<Context
path="'$TOMCAT_HOST_CONTEXT_PATH'" 
docBase="'$TOMCAT_HOST_CONTEXT_DOCBASE'" 
reloadable="'$TOMCAT_HOST_CONTEXT_RELOADABLE'" />
'

 

sed '/<\/Host>/i\'"$HOST_CONTEXT" $TOMCAT_HOME/conf/server.xml >> $TOMCAT_HOME/conf/server_tmp.xml
mv $TOMCAT_HOME/conf/server_tmp.xml $TOMCAT_HOME/conf/server.xml
exec $TOMCAT_HOME/bin/catalina.sh run

 

 

3. docker-compose.yml 작성

version: '2'
services:
  tomcat1:
    container_name: tomcat1
    hostname: tomcat1
    build:
      context: .
      dockerfile: Dockerfile
      args:
        war_path: ./app-1.0.0-SNAPSHOT.war
    image: tomcat_test:1.0
    ports:
      - 7777:8080
    restart: always
    volumes:
     - ./logs/tomcat1:/home/tomcat_user/webapp/apache-tomcat-8.5.45/logs
    environment:
      TOMCAT_ENV_FLAG: "TOMCAT1"
      TOMCAT_HOST_CONTEXT_PATH: "/hello"
      TOMCAT_HOST_CONTEXT_DOCBASE: "app-1.0.0-SNAPSHOT"
      TOMCAT_HOST_CONTEXT_RELOADABLE: "false"
  tomcat2:
    container_name: tomcat2
    hostname: tomcat2
    build:
      context: .
      dockerfile: Dockerfile
      args:
        war_path: ./app-1.0.0-SNAPSHOT.war
    image: tomcat_test:1.0
    ports:
      - 8888:8080
    restart: always
    volumes:
     - ./logs/tomcat2:/home/tomcat_user/webapp/apache-tomcat-8.5.45/logs
    environment:
      TOMCAT_ENV_FLAG: "TOMCAT2"
      TOMCAT_HOST_CONTEXT_PATH: "/hello"
      TOMCAT_HOST_CONTEXT_DOCBASE: "app-1.0.0-SNAPSHOT"
      TOMCAT_HOST_CONTEXT_RELOADABLE: "false"
  tomcat3:
    container_name: tomcat3
    hostname: tomcat3
    build:
      context: .
      dockerfile: Dockerfile
      args:
        war_path: ./app-1.0.0-SNAPSHOT.war
    image: tomcat_test:1.0
    ports:
      - 9999:8080
    restart: always
    volumes:
     - ./logs/tomcat3:/home/tomcat_user/webapp/apache-tomcat-8.5.45/logs
    environment:
      TOMCAT_ENV_FLAG: "TOMCAT3"
      TOMCAT_HOST_CONTEXT_PATH: "/hello"
      TOMCAT_HOST_CONTEXT_DOCBASE: "app-1.0.0-SNAPSHOT"
      TOMCAT_HOST_CONTEXT_RELOADABLE: "false"

 

4. 이미지 생성및 컨테이너 생성

docker-compose up --build -d

 

5. 컨테이너 확인

docker ps

6. 화면 확인

http://localhost:7777/hello/



참조 : https://subicura.com/2017/01/19/docker-guide-for-beginners-2.html#%EB%8F%84%EC%BB%A4-%EC%84%A4%EC%B9%98%ED%95%98%EA%B8%B0

https://docs.docker.com/install/linux/docker-ce/centos/

https://docs.docker.com/compose/install/

댓글