Server/docker

Ubuntu에 Docker 설치

Choiji 2021. 11. 10. 23:32

공식 문서에 이미 있지만 작성해봅니다.

https://docs.docker.com/engine/install/linux-postinstall/

 

 

Post-installation steps for Linux

 

docs.docker.com

 

 

우선 서버를 준비해주세요

VirtualBox를 이용한 웹 서버 만들기(1) 환경 준비

VirtualBox를 이용한 웹 서버 만들기(2) 네트워크 준비

 

VirtualBox를 이용한 웹 서버 만들기(1) 환경 준비

공부용으로 사용할 서버가 필요했는데 클라우드 서비스를 이용하면 무료기간이 지난 이후에는 비용이 청구되고 오라클은 선착순이라길래 집에서 놀고 있는 데스크톱에 가상 머신으로 서버를

choiiii-dev.tistory.com

 

도커를 설치하기전에 이전 버전을 제거 해 줍니다.

sudo apt-get remove docker docker-engine docker.io containerd runc

 

저장소 설정

1. HTTPS를 통해 저장소를 사용할 수 있도록 apt 패키지 인덱스 및 설치 패키지를 업데이트 합니다.

 sudo apt-get update
 sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

2. 도커의 공식 GPG 키 추가

도커와 암호화된 통신을 하기 위한 작업

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

현재 레파지토리에는 도커 설치 파일이 없습니다.

$ sudo apt list | grep docker-ce

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

$ sudo apt list | grep containerd.io

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

apt로 도커를 설치하기 위해

다음 명령어로 저장소를 설정하고 cat으로 레포지토리가 추가가 되었는지 확인합니다.

$ echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  
$ cat /etc/apt/sources.list.d/docker.list

 

패키지 목록을 업데이트 해주고 다시 저장소에 도커 설치 패키지가 존재하는지 조회 해봅니다.

$ sudo apt-get update
$ sudo apt list | grep docker-ce

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

docker-ce-cli/focal 5:20.10.10~3-0~ubuntu-focal amd64
docker-ce-rootless-extras/focal 5:20.10.10~3-0~ubuntu-focal amd64
docker-ce/focal 5:20.10.10~3-0~ubuntu-focal amd64
$ sudo apt list | grep containerd.io

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

containerd.io/focal 1.4.11-1 amd64

 

docker-ce, docker-ce-cli, containerd.io를 설치해줍니다.

sudo apt-get install docker-ce docker-ce-cli containerd.io

 

※ 특정 버전이 필요하시면 아래의 명령어로 사용 가능한 버전을 조회, 설치 합니다.

$ apt-cache madison docker-ce
 docker-ce | 5:20.10.10~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.9~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.8~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.7~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.6~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.5~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.4~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.3~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.2~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.1~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.0~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:19.03.15~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:19.03.14~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:19.03.13~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:19.03.12~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:19.03.11~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:19.03.10~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:19.03.9~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 
 $ sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io

VERSION_STRING = 5:20.10.10~3-0~ubuntu-focal

 

 

설치가 끝났으면 아래의 명령어로 설치가 올바르게 되었는지 확인합니다.

명령어를 입력하면 테스트 이미지를 다운로드하고 컨테이너에서 실행합니다.

컨테이너가 실행되면 메시지를 인쇄하고 종료합니다.

$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685
Status: Downloaded newer image for hello-world:latest

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/

메시지가 잘 나왔으면 이제는 루트가 아닌 사용자로 도커를 관리하는 설정을 해줘야합니다.

 

※ 도커 데몬은 TCP 포트 대신 유닉스 소켓에 바인딩됩니다. 기본적으로 Unix 소켓은 사용자 루트에 의해 소유되며 다른 사용자는 sudo를 통해서만 액세스할 수 있습니다. 도커 데몬은 항상 루트 사용자로 실행됩니다.

 

docker 그룹을 생성하고 사용자를 docker 그룹에 추가합니다.

sudo groupadd docker
sudo usermod -aG docker $USER

그룹 추가후 로그아웃 했다가 다시 로그인하거나 가상 머신이라면 가상 머신을 재시작합니다.

(저는 가상 머신이라서 재시작했습니다....)

아래 명령어를 입력하면 그룹에 대한 변경 사항을 활성화 할 수 있다고 합니다.

 

 

newgrp docker

 

이제 sudo 없이 명령을 실행할 수 있는지 확인합니다.

 

$ 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/

 

도커를 부팅시에 시작하도록 설정하겠습니다.

 sudo systemctl enable docker.service
 sudo systemctl enable containerd.service

다시 비활성화하려면 enable을 disable로 바꾸시면 됩니다.