[Infra] 도커(docker)(1) 설치, hello world

업데이트:

도커(docker)(1) 설치, hello world

참고링크

운영체제 프론트엔드 백엔드 데이터베이스 인프라
리눅스구조 js필터 아파치에러로그 행삭제 아파치스쿱
프로세스 헬로월드 웹서버개념 ES기초 로그분석
네임스페이스 프로젝트생성 아파치설치 MySQL기초 beeline
디렉토리 헤더생성 flask연동 큐브리드 하둡기초
리다이렉션 async-get 장고MsSQL연결 null공백 나이파이
쓰레드 async-post 장고MySQL연결 MySQL설치(win) 백본
라즈베리파이설치 로그인페이지 장고inpectdb MySQL테이블생성 제플린
OSI7계층소개   장고read   SSL인증
OSI1계층   장고insert   커버로스
OSI2계층   장고put   도커개념
OSI3계층   장고del   도커설치
OSI4계층   flask한글요청   도커기초
OSI5,6,7계층       도커이미지
DNS서버       컨테이너네트워크
DHCP       도커API
bashrc       도커컴포즈
bash       도커볼륨
ifconfig       장고이미지
소켓프로그래밍       도커postgre
리눅스유저생성       도커이미지삭제
netstat포트열기       도커Redis
컴파일러       k8s구조
운영체제vs커널       k8s설치
작업스케쥴링       k8s서비스배포
디스크추가       POD네트워크
aws유저추가       퍼시스턴트볼륨
기초명령어       k8s에러
포트번호        

참고 링크

이번 포스팅부터는 도커를 설치하고 hello world를 출력해 봅니다. 먼저 저는 우분투(ubuntu) 운영체제를 사용합니다.

1. 도커가 설치가 되어 있는지 확인

먼저 도커가 설치되어 있는지 확인해봅니다.

$ docker
Command 'docker' not found, but can be installed with:

snap install docker     # version 19.03.13, or
apt  install docker.io  # version 20.10.7-0ubuntu1~20.04.1

See 'snap info docker' for additional versions.

위와 같이 커맨드 창에 docker라고 치면 찾을수 없다고 합니다. 왜냐면 설치를 안했기 때문이죠. 안내문에 따르면 snap install docker를 입력하면 도커 버전 19.03.13으로 설치되고 apt install docker.io를 입력하면 20.10.7버전이 설치된다고 합니다.

2. 도커 설치

2.1. apt 명령어로 설치

$ sudo apt install docker.io

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  containerd runc
Suggested packages:
  aufs-tools btrfs-progs cgroupfs-mount | cgroup-lite debootstrap docker-doc rinse zfs-fuse | zfsutils
Recommended packages:
  pigz ubuntu-fan
The following NEW packages will be installed:
  containerd docker.io runc
0 upgraded, 3 newly installed, 0 to remove and 108 not upgraded.
Need to get 74.1 MB of archives.
After this operation, 359 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
...(중략)
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /lib/systemd/system/docker.service.
Created symlink /etc/systemd/system/sockets.target.wants/docker.socket → /lib/systemd/system/docker.socket.
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...

2.2. 스크립트로 설치

apt 명령어로 도커를 설치해도 되지만 저는 스크립트를 이용해 설치해 보도록 하겠습니다.

$ export VERSION=19.03     (만약 특정 버전의 도커를 깔고 싶다면 입력) 
$ wget -qO- https://get.docker.com | sh -
...(중략)
================================================================================

To run Docker as a non-privileged user, consider setting up the
Docker daemon in rootless mode for your user:

    dockerd-rootless-setuptool.sh install

Visit https://docs.docker.com/go/rootless/ to learn about rootless mode.


To run the Docker daemon as a fully privileged service, but granting non-root
users access, refer to https://docs.docker.com/go/daemon-access/

WARNING: Access to the remote API on a privileged Docker daemon is equivalent
         to root access on the host. Refer to the 'Docker daemon attack surface'
         documentation for details: https://docs.docker.com/go/attack-surface/

================================================================================

위 명령어가 바로 도커를 설치하는 명령어 입니다. (주의: qO에서 O는 숫자 아니고 영어대문자 O입니다. 그리고 -qO 아니고 -qO- 입니다.) 위 명령어는 다름 아닌 http://get.docker.com에 존재하는 스크립트를 돌리는 것인데요. 실제로 사이트에 접속해 보시면 쉘 스크립트 임을 알 수 있습니다. 따라서 위 명령어를 실행하면 스크립트 내용대로 혼자서 알아서 쭉 설치되는 것을 알 수 있습니다. 그리고 설치가 완료되면 위와 같은 안내문이 나옵니다.

3. 도커 설치 확인

앞서 도커를 설치했는데 제대로 설치되었는지 확인해봅니다.

$ docker --version
Docker version 20.10.8, build 3967b7d

위와 같이 도커 버전을 확인하는 커맨드를 입력했을 때 위와 같은 결과가 나온다면 도커가 제대로 설치 된 것 입니다.

4. Hello world!

설치를 했다면 프로그래밍 필수 입문 코스인 Hello world 출력 한번 해보겠습니다. 참고로 도커 명령어는 sudo로 하셔야 합니다.

$ sudo -i
# docker run hello-world

첫번째 줄에서 sudo -i를 하면 현재 유저를 루트 권한으로 바꿉니다. ($는 일반유저, #은 루트로 표시하겠습니다.) 위와 같은 코드를 입력하면 아래와 같이 hello-world 결과를 볼 수 있습니다. 아직 위 코드가 무슨 뜻 인지는 몰라도 됩니다. 차근차근 알아가도록 하겠습니다.

$ sudo -i
# docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

지금까지는 루트 권한으로 docker 명령어를 실행했는데 일반 유저모드에서 docker 명령어를 사용하려면 다음과 같이 입력합니다.

$ whoami
dlinfo

$ sudo usermod -aG docker dlinfo

위 코드에서 dlinfo는 유저 이름입니다. 즉 ‘sudo usermod -aG docker <유저이름>'을 입력하면 됩니다. 그리고 잊지말아야 할점은 위 커맨드를 입력했다고 바로 유저모드에서 도커를 사용할 수 있는것은 아니고 로그아웃 했다가 다시 로그인 해야 적용됩니다.

$ exec $SHELL

5. 다른 서버에도 똑같이 도커 설치(1~4 반복)

우리는 추후에 쿠버네티스를 하게 될 것인데 그 때를 위해 미리 쿠버네티스로 운영할 서버들에게 모두 도커를 설치합니다.

6. 도커 삭제

만약 도커를 삭제하고 싶다면 다음과 같이 입력합니다.

# dpkg -l | grep -i docker

# sudo apt-get purge -y docker-engine docker docker.io docker-ce docker-ce-cli
# sudo apt-get autoremove -y --purge docker-engine docker docker.io docker-ce  

# sudo rm -rf /var/lib/docker /etc/docker
# sudo rm /etc/apparmor.d/docker
# sudo groupdel docker
# sudo rm -rf /var/run/docker.sock

7. 도커 시스템 명령어

# 도커 엔진 시작
$ sudo systemctl start docker
# 도커 엔진 종료
$ sudo systemctl stop docker
# 자동 실행 설정
$ sudo systemctl enable docker