Docker

Posted by Casval's Storage on November 18, 2020

Docker를 설치하고 도커 실행 및 이미지 관리 등에 관한 명령어 정리

참조 사이트 : https://subicura.com/2017/01/19/docker-guide-for-beginners-2.html

Docker 설치

1
$ curl -fsSL https://get.docker.com/ | sudo sh

sudo 없이 사용

사용자를 docker 그룹에 추가

1
2
$ sudo usermod -aG docker $USER   # 현재 접속중인 사용자에게 권한 주기
$ sudo usermod -aG docker your-user  # your-user 사용자에게 권한 주기

명령어

컨테이너 다운로드 및 실행

1
docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
  • run: 이미지가 저장되어 있는지 확인 후 없으면 다운로드 후 생성하고 시작.
    • pull - create -start
options description
-d detached mode 흔히 말하는 백그라운드 모드
-p 호스트와 컨테이너의 포트를 연결 (포워딩)
-v 호스트와 컨테이너의 디렉토리를 연결 (마운트)
-e 컨테이너 내에서 사용할 환경변수 설정
–name 컨테이너 이름 설정
–rm 프로세스 종료시 컨테이너 자동 제거
-it -i와 -t를 동시에 사용한 것으로 터미널 입력을 위한 옵션
–link 컨테이너 연결 [컨테이너명:별칭]
1
2
3
4
5
6
7
8
9
docker run --rm -it ubuntu:16.04 /bin/bash
 
# in container
$ cat /etc/issue
Ubuntu 16.04.1 LTS \n \l
 
$ ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr

기본 명령어

컨테이너 목록 확인(ps)

1
docker ps [OPTIONS]
options   description  
-a –all Show all containers (default shows just running)  
-f –filter filter Filter output based on conditions provided  
    –format string Pretty-print containers using a Go template
-n –last int Show n last created containers (includes all states) (default -1)  
-l –latest Show the latest created container (includes all states)  
    –no-trunc Don’t truncate output
-q –quit Only display numeric IDs  
-s –size Display total file sizes  

컨테이너 중지(stop)

1
docker stop [OPTIONS] CONTAINER [CONTAINER...]
options   description
-t –time int Seconds to wait for stop before killing it (default 10)
1
2
3
docker ps # get container ID
docker stop ${TENSORFLOW_CONTAINER_ID}
docker ps -a # show all containers

####컨테이너 제거하기 (rm)

1
docker rm [OPTIONS] CONTAINER [CONTAINER...]
options   description
-f –force Force the removal of a running container (uses SIGKILL)
-l –link Remove the specified link
-v –volumes Remove the volumes associated with the container
1
2
3
docker ps -a # get container ID
docker rm ${UBUNTU_CONTAINER_ID} ${TENSORFLOW_CONTAINER_ID}
docker ps -a # check exist

이미지 목록 확인 (images)

1
docker images [OPTIONS] [REPOSITORY[:TAG]]
options   description  
-a –all Show all images (default hides intermediate images)  
    –digests Show digests
-f –filter filter Filter output based on conditions provided  
    –format string Pretty-print images using a Go template
    –no-trunc Don’t truncate output
-q –quiet Only show numeric IDs  

이미지 다운로드 (pull)

1
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
options   description  
-a –all-tags Download all tagged images in the repository  
    –disable-content-trust Skip image verification (default true)

이미지 삭제하기 (rmi)

1
docker rmi [OPTIONS] IMAGE [IMAGE...]
options   description  
-f –force Force removal of the image  
    –no-prune Do not delete untagged parents

컨테이너 로그보기 (logs)

1
docker logs [OPTIONS] CONTAINER
options   description  
    –details Show extra details provided to logs
-f –follow Follow log output  
    –since string Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
    –tail string Number of lines to show from the end of the logs (default “all”)
-t –timestamps Show timestamps  
1
2
# 마지막 10줄만 출력
docker logs --tail 10 ${CONTAINER_ID}

컨테이너 명령어 실행 (exec)

1
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
options   description    
-d –detach Detached mode: run command in the background    
    –detach-keys string Override the key sequence for detaching a container  
-e -env list Set environment variables    
-i –interactive Keep STDIN open even if not attached    
    –privileged Give extended privileges to the command  
-t –tty Allocate a pseudo-TTY    
-u –user string Username or UID (format: <name uid>[:<group gid>])

다른 컨테이너와 링크

  • mysql 을 연결 할때 컨테이너가 새로시작되면 매번 ip가 바뀌기 때문에 다음과 같이 –link 옵션을 주어 /ect/hosts 파일 안에 alias를 할당해 사용한다.
    1
    
    --link <other container name>:<alias name of host container>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    $ docker run -it --name test-co --link test-mysql:workbench-db centos:7.2.1511
    $ cat /etc/hosts
    127.0.0.1    localhost
    ::1    localhost ip6-localhost ip6-loopback
    fe00::0    ip6-localnet
    ff00::0    ip6-mcastprefix
    ff02::1    ip6-allnodes
    ff02::2    ip6-allrouters
    172.17.0.4    workbench-db 133f5b660750 test-mysql
    172.17.0.3    e9808b165c8f