Docker install on ubuntu

5 minute read

Setup the docker via Repository

1. Update the apt package index and install packages to allow apt to use a repository over HTTPS:

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

Error: WARNING: unsafe ownership on homedir β€˜/home/user/.gnupg’

$ sudo gpgconf --kill dirmngr
$ sudo chown -R $USER ~/.gnupg

2. Add Docker’s official GPG key:

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

3. Use the following command to set up the stable repository. To add the nightly or test repository, add the word nightly or test (or both) after the word stable in the commands below. Learn about nightly and test channels.

$ echo \
  "deb [arch=amd64 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

Install Docker Engine

1. Update the apt package index, and install the latest version of Docker Engine and containerd, or go to the next step to install a specific version:

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

2. Verify that Docker Engine is installed correctly by running the hello-world image.

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

Docker add & remove user authorization

add

$ sudo usermod -aG docker [your-user]

Remove

$ sudo deluser [your-user] docker 

Remove Docker

# 도컀 νŒ¨ν‚€μ§€ 제거
$ sudo apt-get purge docker-ce

# 이미지, μ»¨ν…Œμ΄λ„ˆ, λ³Όλ₯¨, μ‚¬μš©μž 지정 μ„€μ • νŒŒμΌμ€ νŒ¨ν‚€μ§€ 제거둜 제거 λ˜μ§€ μ•ŠμŒ. 별도 제거 ν•„μš”ν•¨.
$ sudo rm -rf /var/lib/docker

Docker Software

  • 도컀 엔진(Docker Engine): νŠΉμ •ν•œ μ†Œν”„νŠΈμ›¨μ–΄λ₯Ό 도컀 μ»¨ν…Œμ΄λ„ˆλ‘œ λ§Œλ“€κ³  μ‹€ν–‰ν•˜κ²Œ ν•΄μ£ΌλŠ” 데λͺ¬(Daemon)을 μ˜λ―Έν•©λ‹ˆλ‹€. 도컀 엔진과 도컀 ν΄λΌμ΄μ–ΈνŠΈ μ‚¬μ΄μ—λŠ” REST APIκ°€ μ‚¬μš©λ©λ‹ˆλ‹€. REST API μ„œλ²„μ— μš”μ²­μ„ λ³΄λ‚΄λŠ” 것이 도컀 ν΄λΌμ΄μ–ΈνŠΈμž…λ‹ˆλ‹€.

  • 도컀 ν΄λΌμ΄μ–ΈνŠΈ(Docker Client): 도컀 엔진과 ν†΅μ‹ ν•˜λŠ” μ†Œν”„νŠΈμ›¨μ–΄λ‘œ κ°œλ°œμžκ°€ 직접 μ΄μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μœˆλ„μš°(Window)/λ§₯(Mac)/λ¦¬λˆ…μŠ€(Linux)λ₯Ό μ§€μ›ν•©λ‹ˆλ‹€. λ¬Όλ‘  μœˆλ„μš°λŠ” 도컀λ₯Ό μ‚¬μš©ν•˜κΈ°μ— μ΅œμ•…μ˜ 쑰건이고, λ¦¬λˆ…μŠ€κ°€ 제일 μ΅œμƒμ˜ μ‘°κ±΄μ΄μ§€λ§Œ λ„μ»€λŠ” κ³΅μ‹μ μœΌλ‘œ μœˆλ„μš°λ₯Ό μ œλŒ€λ‘œ μ§€μ›ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

  • 도컀 호슀트 운영체제(Docker Host OS): 도컀 엔진을 μ„€μΉ˜ν•  수 μžˆλŠ” 운영체제 ν™˜κ²½μ„ μ˜λ―Έν•©λ‹ˆλ‹€. 64λΉ„νŠΈ λ¦¬λˆ…μŠ€ 컀널 버전 3.10 이상 ν™˜κ²½μ„ μ˜λ―Έν•˜κ³ , 32λΉ„νŠΈ ν™˜κ²½μ—μ„œλŠ” 도컀 엔진이 λŒμ•„κ°€μ§€ μ•ŠλŠ”λ‹΅λ‹ˆλ‹€. μ• μ΄ˆμ— 초기의 도컀 μ΄λ―Έμ§€λŠ” 심지어 였직 μš°λΆ„νˆ¬(Ubuntu) 운영체제 μ „μš©μ΄μ—ˆμ–΄μš”. ν˜„μž¬λŠ” μš°λΆ„νˆ¬, CentOS, Debian, Fedora λ“±μ—μ„œ μ‚¬μš©ν•  수 있게 λ˜μ—ˆμ§€λ§Œμš”.

  • 도컀 λ¨Έμ‹ (Docker Machine): 둜컬 및 원격지 μ„œλ²„μ— 도컀 엔진을 μ„€μΉ˜ν•˜κ³ , λ‹€μ–‘ν•œ ν™˜κ²½ 섀정을 μžλ™μœΌλ‘œ μˆ˜ν–‰ν•΄μ£ΌλŠ” ν΄λΌμ΄μ–ΈνŠΈλ₯Ό μ˜λ―Έν•©λ‹ˆλ‹€.

Docker commands

  • docker ps: ν˜„μž¬ λŒμ•„κ°€κ³  μžˆλŠ” μ»¨ν…Œμ΄λ„ˆλ₯Ό ν™•μΈν•˜λŠ” λͺ…λ Ήμ–΄μž…λ‹ˆλ‹€.

  • docker images: ν˜„μž¬ 도컀 머신에 μ„€μΉ˜λœ 도컀 이미지λ₯Ό ν™•μΈν•˜λŠ” λͺ…λ Ήμ–΄μž…λ‹ˆλ‹€.

  • docker --version: 도컀 버전확인

  • sudo docker info: μƒμ„Έν•œ 버전 확인

  • docker run hello-world: λ„μ»€μ—μ„œ ν—¬λ‘œμš° μ›”λ“œ(Hello World)λ₯Ό λ„μš°λŠ” λͺ…λ Ήμ–΄μž…λ‹ˆλ‹€. 우리 λˆˆμ—λŠ” 보이지 μ•Šμ§€λ§Œ λ‹€μŒμ˜ 과정이 ν¬ν•¨λ˜λŠ” κ²λ‹ˆλ‹€.
    • 곡식 μ‚¬μ΄νŠΈμ—μ„œ install ν• λ•Œ 좜λ ₯ν•΄λ΄€μŒ.
  • docker images: ν˜„μž¬ 도컀 머신에 μ‘΄μž¬ν•˜λŠ” 이미지 λͺ©λ‘μ„ 좜λ ₯ν•©λ‹ˆλ‹€.
      (base)  yilgukseo ξ‚° ~/DL ξ‚° sudo docker images
      REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
      hello-world   latest    d1165f221234   4 months ago   13.3kB
    
  • docker ps -a: ν˜„μž¬ 도컀 머신에 μ‘΄μž¬ν•˜λŠ” μ»¨ν…Œμ΄λ„ˆλ₯Ό 좜λ ₯ν•©λ‹ˆλ‹€.
      (base)  yilgukseo ξ‚° ~/DL ξ‚° sudo docker ps -a
      CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                     PORTS     NAMES
      fad8e07c603f   hello-world   "/hello"   9 minutes ago   Exited (0) 9 minutes ago             eloquent_poincare
    
    • ID, IMAGE, Command, CREATED, STATUS, PORTS, NAMES λ₯Ό 확인
  • docker rm (μ»¨ν…Œμ΄λ„ˆ ID): νŠΉμ • μ»¨ν…Œμ΄λ„ˆλ₯Ό μ‚­μ œν•©λ‹ˆλ‹€.

  • docker rm (μ»¨ν…Œμ΄λ„ˆ ID) -f: νŠΉμ • μ»¨ν…Œμ΄λ„ˆλ₯Ό κ°•μ œλ‘œ μ‚­μ œν•©λ‹ˆλ‹€.

  • docker run -it python: 파이썬 이미지λ₯Ό λ‹€μš΄λ‘œλ“œ λ°›μ•„μ„œ μ‹€ν–‰ν•΄μ£ΌλŠ” λͺ…λ Ήμ–΄μž…λ‹ˆλ‹€.
    • -it μ˜΅μ…˜μ€ ν‘œμ€€ μž…μΆœλ ₯을 μ΄μš©ν•΄ μ»¨ν…Œμ΄λ„ˆμ— λͺ…λ Ήμ–΄λ₯Ό μž…λ ₯ν•  수 있게 ν•΄μ€λ‹ˆλ‹€.
      (base)  yilgukseo ξ‚° ~/DL ξ‚° sudo docker run -it python
      Unable to find image 'python:latest' locally
      latest: Pulling from library/python
      627b765e08d1: Pull complete 
      c040670e5e55: Pull complete 
      073a180f4992: Pull complete 
      bf76209566d0: Pull complete 
      ca7044ed766e: Pull complete 
      7b16520e0e66: Pull complete 
      e121e5a178df: Pull complete 
      abbaf10bd160: Pull complete 
      4349f8e0b43a: Pull complete 
      Digest: sha256:a465eb577326845a6772c0de37d11c4de3da9a0248c85ae8a7b8629561cb2185
      Status: Downloaded newer image for python:latest
      Python 3.9.6 (default, Jul 22 2021, 15:16:20) 
      [GCC 8.3.0] on linux
      Type "help", "copyright", "credits" or "license" for more information.
      >>> print("hellow docker python")
      hellow docker python
      
  • quit(): 파이썬 μ½˜μ†”μ„ μ’…λ£Œμ‹œν‚€λŠ” λͺ…λ Ήμ–΄μž…λ‹ˆλ‹€.

  • docker start (μ»¨ν…Œμ΄λ„ˆ ID): μ’…λ£Œλœ μ»¨ν…Œμ΄λ„ˆλ₯Ό μž¬μ‹œμž‘ν•˜λŠ” λͺ…λ Ήμ–΄μž…λ‹ˆλ‹€.

  • docker attach (μ»¨ν…Œμ΄λ„ˆ ID): νŠΉμ • μ»¨ν…Œμ΄λ„ˆλ‘œ μž¬μ ‘μ†ν•˜λŠ” λͺ…λ Ήμ–΄μž…λ‹ˆλ‹€.

  • docker exec -it (μ»¨ν…Œμ΄λ„ˆ ID) bash: μ‹€ν–‰ 쀑인 μ»¨ν…Œμ΄λ„ˆμ— λ°°μ‹œ(Bash) μ‰˜λ‘œ μ ‘μ†ν•˜λŠ” λͺ…λ Ήμ–΄μž…λ‹ˆλ‹€.

  • sudo docker stop container_id: stop container
      (base)  ✘ yilgukseo ξ‚° ~/DL ξ‚° sudo docker ps -a
      CONTAINER ID   IMAGE         COMMAND     CREATED          STATUS                      PORTS     NAMES
      0a645f33513a   python        "python3"   11 minutes ago   Up About a minute                     busy_wescoff
      fad8e07c603f   hello-world   "/hello"    23 minutes ago   Exited (0) 23 minutes ago             eloquent_poincare
      (base)  yilgukseo ξ‚° ~/DL ξ‚° sudo docker stop 0a645f33513a
      0a645f33513a
      (base)  yilgukseo ξ‚° ~/DL ξ‚° sudo docker ps -a            
      CONTAINER ID   IMAGE         COMMAND     CREATED          STATUS                        PORTS     NAMES
      0a645f33513a   python        "python3"   15 minutes ago   Exited (137) 54 seconds ago             busy_wescoff
      fad8e07c603f   hello-world   "/hello"    27 minutes ago   Exited (0) 27 minutes ago               eloquent_poincare
    
    • stop ν•˜λ©΄ Exited 라고 ν‘œμ‹œλ¨.
      • startλŠ” 빨리 λ˜λŠ”λ° Stop 은 μ‹œκ°„μ΄ μ’€ 더 μ†Œμš”λ¨
    • Container nameμœΌλ‘œλ„ λ˜λŠ”μ§€ 확인
      • 됨.
        (base)  yilgukseo ξ‚° ~/DL ξ‚° sudo docker ps -a             
        CONTAINER ID   IMAGE         COMMAND     CREATED          STATUS                      PORTS     NAMES
        0a645f33513a   python        "python3"   16 minutes ago   Up 1 second                           busy_wescoff
        fad8e07c603f   hello-world   "/hello"    28 minutes ago   Exited (0) 28 minutes ago             eloquent_poincare
        (base)  yilgukseo ξ‚° ~/DL ξ‚° sudo docker stop busy_wescoff
        busy_wescoff
        (base)  yilgukseo ξ‚° ~/DL ξ‚° sudo docker ps -a            
        CONTAINER ID   IMAGE         COMMAND     CREATED          STATUS                        PORTS     NAMES
        0a645f33513a   python        "python3"   17 minutes ago   Exited (137) 15 seconds ago             busy_wescoff
        fad8e07c603f   hello-world   "/hello"    29 minutes ago   Exited (0) 28 minutes ago               eloquent_poincare
        
  • docker rm [-option] [container ID]: μ»¨ν…Œμ΄λ„ˆ μ‚­μ œ

  • docker rm $(docker ps -a -q -f status=exited): μ€‘μ§€λœ μ»¨ν…Œμ΄λ„ˆ μ‚­μ œ

  • docker rm $(docker ps -qa): λͺ¨λ“  μ»¨ν…Œμ΄λ„ˆ μ‚­μ œ

official docker commands: https://docs.docker.com/engine/reference/commandline/docker/

Appendix

Reference

Official site: https://docs.docker.com/engine/install/ubuntu/
https://shaul1991.medium.com/%EC%B4%88%EB%B3%B4%EA%B0%9C%EB%B0%9C%EC%9E%90-%EC%9D%BC%EC%A7%80-docker-%EC%84%A4%EC%B9%98%ED%95%B4%EB%B3%B4%EC%9E%90-d3d6a11ea098
https://unix.stackexchange.com/questions/452020/gpg-warning-unsafe-ownership-on-homedir-home-user-gnupg
ndb : https://ndb796.tistory.com/91

Leave a comment