Docker 설치

Ubuntu에 Docker engine 설치

사전 준비

  • Docker engine은 현재 아래 우분투 버전을 지원합니다.
    • 22.10 Kinetic
    • 22.04 Jammy (LTS)
    • 20.04 Focal (LTS)
    • 18.04 Bionic (LTS)
  • CPU 아키텍쳐는 x86_64, AMD64, armhf, afm64, s390x를 지원합니다.

기존 설치 버전 제거

새로운 버전을 설치하기 전 아래 명령어로 설치되어있을지 모르는 이전 설치 버전을 삭제합니다.

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

이전 버전의 네트워크, 컨테이너, 이미지 등의 완전 삭제 후 클린 설치를 원하는 경우 /var/lib/docker 디렉토리도 삭제합니다.

sudo rm -rf /var/lib/docker/

리포지토리 설정

OS의 Docker engine 버전이 현재 docker.com에서 제공하는 버전과 맞지 않는 구 버전일 수 있으므로 Docker 리포지토리를 추가하여 docker.com의 최신 버전을 받아오도록 설정합니다.

1. apt 패키지 인덱스를 업데이트하고 필요한 패키지를 설치합니다.

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

2. Docker의 공식 GPG 키를 추가합니다.

sudo mkdir -m 0755 -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

3. Docker 리포지토리를 설정합니다.

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

Docker Engine 설치

1. apt 패키지 인덱스를 업데이트합니다.

sudo apt-get update

2. Docker engine, containerd, Docker compose를 설치합니다.

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  • docker compose는 별도의 단독 실행형 프로그램으로도 설치되지만 현재는 Docker engine에 플러그인 형태로 통합되어 OS 버전에 따라 Docker engine 플러그인 버전과 단독 실행형 프로그램의 버전이 상이한 경우가 있습니다. 가급적 Docker engine 플러그인 버전을 사용하시는 것이 최신 버전을 사용할 수 있습니다.
  • 명령어는 동일하므로 종전 docker-compose up -d 이런 형태에서 docker compose up -d 이런 형태로 docker와 compose 문자열 사이의 -(대시)를 빼면 됩니다.

3. 완료 후 정상적으로 작동하는지 테스트 이미지를 실행해봅니다.

sudo docker run hello-world

4. 아래와 같이 이미지 pull → run이 정상적으로 실행되면 설치가 완료됩니다.

$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
7050e35b49f5: Pull complete 
Digest: sha256:6e8b6f026e0b9c419ea0fd02d3905dd0952ad1feea67543f525c73a0a790fefb
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.
    (arm64v8)
 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를 붙여 실행하는 것은 귀찮기 때문에 docker가 일반 사용자 그룹에서도 실행되도록 추가 설정합니다.

1. 공식 문서나 다른 문서에서는 docker 사용자 그룹을 추가하라고 나와있는데, APT 패키지로 설치한 경우 docker 그룹은 자동으로 추가됩니다.

2. 사용자 계정을 docker 그룹에 추가해줍니다. 현재 로그인한 사용자를 추가하는 경우 $USER 인자를 입력하면 자동으로 추가됩니다.

sudo usermod -aG docker $USER

3. 로그아웃 후 로그인한 뒤 sudo를 빼고 docker run을 실행해봅니다. 서버가 아주 느린게 아니라면 재부팅을 해도 됩니다.

docker run hello-world
$ 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.
    (arm64v8)
 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/

4. Debian이나 Ubuntu에서는 APT 패키지 설치시 docker 서비스가 부팅시 자동 실행되도록 설정되지만, 혹시 모르니 system 서비스를 한번 더 등록합니다(한번 더 등록된다고 두번 실행되지 않습니다).

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