실습 7 - 도커 설치 및 기본 명령어 살펴보기

 

실습영상

Video preview
 

실습 메뉴얼

Ubuntu 20.04 서버에 도커 설치하기

# 도커 설치 스크립트 다운로드 curl -fsSL https://get.docker.com -o get-docker.sh # 도커 설치 스크립트 실행 sudo sh get-docker.sh # 도커 Cli 명령 실행 sudo docker # 시스템 재부팅 시에 docker가 동작하도록 설정 sudo systemctl enable docker # 도커 실행여부 sudo service docker status # docker 그룹 생성 sudo groupadd docker # docker 그룹에 현재 유저 추가 sudo usermod -aG docker $USER
# hello world 이미지 실행 docker run hello-world # nginx 실행하기 docker run -p 80:80 nginx # getting-started 이미지 실행하기 docker run -d -p 80:80 docker/getting-started # 실행중인 컨테이너 목록 docker ps # 모든 컨테이너 목록 docker ps -a # 로그 보기 docker logs -f ecstatic_sanderson # 컨테이너 중지하기 docker stop serene_hypatia # 컨테이너 삭제하기 docker rm ecstatic_sanderson # 컨테이너 이미지 삭제하기 docker rmi docker/getting-started
 
  1. AWS 라이트세일에서 인스턴스 생성 버튼을 클릭합니다.
    1. notion imagenotion image
       
  1. 플랫폼은 Linux/Unix, 블루프린트는 Ubuntu 20.04 LTS를 선택합니다.
    1. notion imagenotion image
       
  1. 인스턴스 플랜은 $10를 선택합니다.
    1. notion imagenotion image
       
  1. 인스턴스이름은 userOOO-docker로 입력합니다.
    1. notion imagenotion image
       
  1. 인스턴스 생성 버튼을 클릭하여 서버를 생성합니다.
    1. notion imagenotion image
       
  1. 생성을 한 직후 에는 회색으로 비활성화 되어있습니다. 2~3분정도 기다리면 이후 색이 활성화 됩니다.
    1. notion imagenotion image
       
  1. 방금 생성한 user000-docker 인스턴스명을 선택하여 인스턴스 상세화면으로 이동합니다.
    1. notion imagenotion image
       
  1. 아래와 같이 인스턴스의 상세화면이 조회됩니다.
    1. notion imagenotion image
       
  1. 서버에 접속하기 위해서 SSH를 사용하여 연결 버튼을 클릭합니다.
    1. notion imagenotion image
       
  1. 서버를 생성한지 얼마 안된 경우 1~2분 후에 다시 시도하라는 안내가 나옵니다.
    1. notion imagenotion image
       
  1. 정상적으로 접속이 완료되면 아래와 같은 SSH 연결된 화면이 나옵니다.
    1. notion imagenotion image
       
  1. 서버에 docker를 설치하기 위하여 설치스크립트 다운로드 명령어를 복사합니다
    1. # 도커 설치 스크립트 다운로드 curl -fsSL https://get.docker.com -o get-docker.sh
      notion imagenotion image
      notion imagenotion image
       
  1. 복사한 설치스크립트 다운로드 명령어를 붙여넣기 합니다.
    1. notion imagenotion image
      notion imagenotion image
       
  1. Docker 설치 스크립트 실행 명령어를 복사합니다.
    1. # 도커 설치 스크립트 실행 sudo sh get-docker.sh
      notion imagenotion image
       
  1. 터미널에 설치스크립트 실행명령어를 붙여넣기 한 뒤 실행합니다.
    1. notion imagenotion image
      notion imagenotion image
      notion imagenotion image
       
  1. 도커 명령을 실행하여 정상적으로 동작하는지 확인합니다.
    1. sudo docker
      notion imagenotion image
      notion imagenotion image
       
  1. 시스템 재부팅 시에 docker가 항상 동작하도록 설정하는 명령을 실행합니다.
    1. # 시스템 재부팅 시에 docker가 동작하도록 설정 sudo systemctl enable docker
      notion imagenotion image
      notion imagenotion image
       
  1. 도커가 현재 정상적으로 실행되고 있는지 여부를 체크하는 명령어를 입력합니다.
    1. # 도커 실행여부 sudo service docker status
      notion imagenotion image
       
  1. 도커 상태조회 명령어 실행결과 active(running) 표시가 되고 있습니다. 잘 실행되고 있는 것을 확인하였습니다.
    1. notion imagenotion image
       
  1. 도커명령을 실행할때는 sudo를 앞에 붙여서 루트 권한으로 실행해야 합니다. 별도로 sudo를 붙이지 않고 docker를 사용하기 위해서는 1. docker 유저 그룹을 추가하고 2. 해당 docker 유저 그룹에 현재 사용자를 추가해야 합니다
  1. docker 유저그룹을 추가하는 명령을 입력하여 유저그룹을 추가합니다.
    1. # docker 그룹 생성 sudo groupadd docker
      notion imagenotion image
       
  1. 생성된 docker 유저 그룹에 현재 유저를 추가합니다.
    1. # docker 그룹에 현재 유저 추가 sudo usermod -aG docker $USER
      notion imagenotion image
      notion imagenotion image
       
  1. 터미널의 내용을 지우는 명령어는 clear 입니다.
    1. notion imagenotion image
       
  1. hello-world 라는 도커 컨테이너를 실행해 보겠습니다.
    1. docker run hello-world
      notion imagenotion image
      notion imagenotion image
       
  1. 실행결과 권한이 없다는 에러 메세지가 발생합니다.
    1. notion imagenotion image
       
  1. 서버에 다시 접속 하기 위해서 exit 명령어를 입력합니다.
    1. notion imagenotion image
       
  1. 다시 연결 버튼을 클릭하여 서버에 다시 연결합니다.
    1. notion imagenotion image
      notion imagenotion image
       
  1. clear 명령을 이용하여 화면을 깨끗하게 지웁니다.
    1. notion imagenotion image
       
  1. 다시 hello-world 컨테이너 명령을 실행합니다.
    1. docker run hello-world
      notion imagenotion image
       
  1. 실행결과 다음과 같은 결과가 출력됩니다.
    1. notion imagenotion image
       
  1. 이제 nginx 컨테이너를 실행해보겠습니다. -p 80:80 : 호스트서버의 80포트와 nginx 컨테이너의 80포트를 연결하는 옵션입니다.
    1. docker run -p 80:80 nginx
      notion imagenotion image
      notion imagenotion image
       
  1. 실행결과 다음과 같이 nginx가 실행됩니다.
    1. notion imagenotion image
       
  1. AWS Lightsail의 인스턴스 상세페이지에서 퍼블릭 IP를 확인할 수 있습니다.
    1. notion imagenotion image
       
  1. 퍼블릭 IP를 복사하여 웹브라우저 주소창에 붙여넣기 하여 접속합니다.
    1. notion imagenotion image
      notion imagenotion image
       
  1. Welcome to nginx! 라는 결과를 볼 수 있습니다.
    1. notion imagenotion image
       
  1. 화면을 새로고침하면 터미널창의 로그가 추가로 생기는 것을 볼 수 있습니다.
    1. notion imagenotion image
      notion imagenotion image
       
  1. 컨테이너의 중지를 위해서는 터미널창에서 Control + c 를 입력합니다.
    1. notion imagenotion image
       
  1. 도커 공식홈페이지에서 제공하는 튜토리얼 페이지 컨테이너를 실행해보겠습니다. 실행 명령어는 다음과 같습니다. 이번에는 추가로 -d 옵션이 붙습니다. -d 옵션은 컨테이너가 detached 모드(백그라운드 실행)에서 실행됩니다.
    1. # getting-started 이미지 실행하기 docker run -d -p 80:80 docker/getting-started
      notion imagenotion image
      notion imagenotion image
       
  1. 실행결과 다음과 같이 실행후에 다시 명령어를 입력할 수 있도록 돌아 옵니다.
    1. notion imagenotion image
       
  1. 웹브라우저에서 퍼블릭 IP로 다시 접속해보겠습니다.
    1. notion imagenotion image
      notion imagenotion image
       
  1. 현재 실행중인 컨테이너를 조회하는 명령은 docker ps 입니다.
    1. notion imagenotion image
       
  1. 실행중인 컨테이너 목록의 내용중에 끝부분에 컨테이너 이름이 표시됩니다. 이 컨테이너 이름을 컨테이너 로그조회, 중지, 시작, 삭제 등 하나의 컨테이너에 직접 명령을 실행할때 사용됩니다.
    1. notion imagenotion image
       
  1. 컨테이너 이름을 복사하겠습니다.
    1. notion imagenotion image
       
  1. 컨테이너의 로그를 조회하는 명령어는 다음과 같습니다. <컨테이너 이름> 영역에 방금전 복사한 컨테이너 이름을 붙여넣어서 실행합니다.
    1. docker logs -f <컨테이너 이름>
      notion imagenotion image
       
  1. 다음과 같이 현재 실행중인 컨테이너의 로그를 조회 할 수 있습니다.
    1. notion imagenotion image
       
  1. 웹페이지를 새로고침하면 컨테이터 로그에 새로운 로그가 추가되는것을 볼 수 있습니다.
    1. notion imagenotion image
      notion imagenotion image
       
  1. 로그 표시를 중지하기 위해서는 Control + c 를 입력하면 됩니다.
    1. notion imagenotion image
       
  1. 다시 docker ps 명령어 입력하여 현재 실행중인 컨테이너 목록을 조회 하였습니다. 현재 1개의 컨테이너가 실행중인 것을 볼 수 있습니다.
    1. notion imagenotion image
       
  1. 이번에는 컨테이너를 중지시키겠습니다. 중지시키는 명령어는 다음과 같습니다.
    1. docker stop <컨테이너 이름>
      <컨테이너 이름> 영역에 중지시키고자 하는 컨테이너의 이름을 입력합니다.
      notion imagenotion image
       
  1. 중지 명령 실행뒤에 다시 현재 실행중인 컨테이너를 조회 합니다.
    1. docker ps
      notion imagenotion image
      실행 결과 실행중인 컨테이너가 없는 것을 확인할 수 있습니다.
       
  1. 실행되고 있는 컨테이너 뿐 아니라 실행중지 되어 있는 컨테이너까지 모든 컨테이너 조회해 보겠습니다.
    1. docker ps -a
      notion imagenotion image
       
  1. 실행중지 되어 있는 컨테이너를 삭제해 보겠습니다. 컨테이너를 삭제하는 명령은 다음과 같습니다.
    1. docker rm <컨테이너 이름>
      docker ps -a 를 통해서 조회된 컨테이너 이름을 이용하여 삭제할 수 있습니다.
      notion imagenotion image
       
  1. 삭제 이후 다시 모든 컨테이너 조회 명령을 실행하겠습니다.
    1. docker ps -a
      notion imagenotion image
      조회 결과 삭제명령을 한 컨테이너가 없는것을 확인 할 수 있습니다.