티스토리 뷰

가상화/Docker_K8s

Docker 명령어 cheatsheet

청년정신 2019. 6. 25. 02:06
Update : 2019.06.20

알아두면 유용한 Docker 명령어 모음

docker --version
docker info
docker search “images file name"
    docker search --filter "is-official=true” centos
docker pull
docker images “images file name"
docker create “container”
    docker create --name centos01 -it centosdocker start
docker attach “container name or ID”
docker exec -it “container name or ID” /bin/bash
docker stop “container name or ID"
docker kill “container name or ID"
docker restart “container name or ID"
docker run “container name or ID”
    docker run --name centos02 -it centos
    docker run --name ngnix-test -p 8080:80 nginx
    docker run --name centos02 --hostname centos02 -it centos
docker ps
    docker ps -a
docker rm “container name or ID"
docker rmi “image name"
docker save
    docker save alpine -o alpine_edit.tar
    docker load -I alpine_edit.tar
docker export “container name or ID"
    docker export web_container -o  web_container.tar
docker import
    docker import web_container.tar
docker history “Image name or ID"
docker logs “Container name or ID”
    docker logs -f “Container name or ID"
docker stats
    docker stats —-no-stream
docker top “container name or ID”
docker inspect
    docker inspect -f "{{.NetworkSettings.IPAddress}}”
docker build -t
docker tag “source image:tag” “target image:tag"


Docker version 확인
[root@cent151 ~]# docker --version
Docker version 18.09.6, build 481bc77156

Docker 정보 확인
[root@cent151 ~]# docker info
Containers: 6
Running: 1
Paused: 0
Stopped: 5
Images: 7
Server Version: 18.09.6
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: bb71b10fd8f58240ca47fbb579b9d1028eea7c84
runc version: 2b18fe1d885ee5083ef9f0838fee39b62d653e30
init version: fec3683
Security Options:
seccomp
  Profile: default
Kernel Version: 3.10.0-957.5.1.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 7.638GiB
Name: cent151
ID: F2X4:DTCN:T52E:KK7U:YTCG:JDA5:AD4F:UQEP:D4TK:4B4Z:7ETK:JWOM
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Product License: Community Engine

Docker 이미지 검색

[root@cent151 ~]# docker search ngnix
NAME                                  DESCRIPTION                       STARS               OFFICIAL            AUTOMATED
userxy2015/ngnix                      ngnix                             7
ludwringliccien/ngnix-php7.1          ngnix-php7.1                      1                                       [OK]
jhuiting/ngnix                                                          1
covenant/ngnix                                                          0
hamropatrorepo/ngnix-reverse-proxy                                      0
sitek/ngnix-vsftpd                    ngnix-vsftpd                      0                                       [OK]
1app/ngnix                            configuração inicial              0
agualbbus/ngnix-php-proxy                                               0
theburi/ngnix                         ngnix                             0
sanghaiakshay/ngnix_https_webserver                                     0
jenorish/ngnix                                                          0
starlkj/ngnix                         tde test                          0                                       [OK]
alamalhoda/ngnix                                                        0
bguruprasad/ngnix-print-hostname      ngnix-print-hostname              0
afzal/ngnix_rate_limit                ngnix rate limit examples         0
sushanth53/ngnix_server               ngnix server                      0
silvade/ngnix-static                  ngnix docker image  with ubuntu   0
sunbuild/ngnix-fpm-azure              Linux app service : ngnix-fpm     0                                       [OK]
sunlitweb/ngnix                                                         0
iijjijijijjijiji/ngnix20170422                                          0
ranvirsingh/ngnix-marklogic                                             0
fnature/ngnix-curl                                                      0
rajdeeps/ngnix                                                          0
diegograssato/ngnix                                                     0
jerrycheung/ngnix                                                       0

[root@cent151 ~]# docker search --filter "is-official=true" centos
NAME                DESCRIPTION                     STARS               OFFICIAL            AUTOMATED
centos              The official build of CentOS.   5421                [OK]


docker 이미지 확인
[root@centrepo ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
postgres            latest              79db2bf18b4a        2 days ago          312MB
alpine              latest              4d90542f0623        4 days ago          5.58MB
mariadb             latest              f1e4084965e5        4 days ago          356MB
wordpress           latest              5e5e0b1aa9dd        5 days ago          447MB
haproxy             latest              7856fff2a391        5 days ago          73.6MB
composer            latest              859975b40129        6 days ago          160MB
php                 latest              f0357c41bff5        6 days ago          367MB
busybox             latest              e4db68de4ff2        9 days ago          1.22MB
docker              latest              805bea199b24        9 days ago          171MB
node                latest              b074182f4154        13 days ago         906MB
redis               latest              3c41ce05add9        13 days ago         95MB
httpd               latest              e77c77f17b46        13 days ago         140MB
memcached           latest              de8ed9aa9b7e        13 days ago         67.5MB
nginx               latest              719cd2e3ed04        13 days ago         109MB
mysql               latest              c7109f74d339        13 days ago         443MB
amazonlinux         latest              b94321659aca        4 weeks ago         162MB
centos              latest              9f38484d220f        3 months ago        202MB
registry            latest              f32a97de94e1        3 months ago        25.8MB
cirros              latest              bc94bceaae77        5 months ago        10.3MB
hello-world         latest              fce289e99eb9        5 months ago        1.84kB
swarm               latest              ff454b4a0e84        12 months ago       12.7MB

Docker create : 이미지와 신규 생성되는 컨테이너 매핑 
# name option을 통해서 컨테이너 이름을 지정하고, 이미지는 ubuntu로 지정
[root@cent151 ~]# docker create --name ubuntu01 ubuntu
6718ea98df32c6839a25b0eceb9c07f3c414f2e8537c41cbee0fcd88e3e05381

# -it 옵션을 통해서 interactive tty를 생성하여 컨테이너 구동하며, 컨테이너를 자동으로 시작 시킴.
[root@cent151 ~]# docker create --name ubuntu01 -it ubuntu start
b75e75b8b61696c7042749d003eeeb1b5c56868baadec0c00428c6bd61d7a42e

Docker start 
[root@cent151 ~]# docker start ubuntu01
ubuntu01

# option -a 는 attach와 같은 효과를 줄 수 있음.
[root@cent151 ~]# docker start -a ubuntu01
root@fdb17624280d:/#

Docker stop
[root@cent151 ~]# docker stop ubuntu01
ubuntu01

Docker restart
[root@cent151 ~]# docker restart ubuntu01
ubuntu01
[root@cent151 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
fdb17624280d        ubuntu              "/bin/bash"         7 minutes ago       Up 6 seconds                            ubuntu01

Docker kill - Docker Container Process 강제로 down
[root@cent151 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
fdb17624280d        ubuntu              "/bin/bash"         About a minute ago   Up About a minute                       ubuntu01
77b4357ed7e8        centos              "/bin/bash"         About an hour ago    Up 31 minutes                           centos01

[root@cent151 ~]# docker kill ubuntu01
ubuntu01

[root@cent151 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
77b4357ed7e8        centos              "/bin/bash"         About an hour ago   Up 32 minutes                           centos01

# Ubuntu01은 컨테이너만 정지됨.
[root@cent151 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                           PORTS               NAMES
fdb17624280d        ubuntu              "/bin/bash"         2 minutes ago       Exited (137) 54 seconds ago                          ubuntu01

Docker attach : 터미널에 도커 컨테이너 연결방법
[root@cent151 ~]# docker attach ubuntu01
root@fdb17624280d:/#

Docker exec : 터미널에 도커 컨테이너 연결방법
[root@cent151 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
8ce29e8e2303        nginx               "nginx -g 'daemon of…"   2 minutes ago       Up About a minute   0.0.0.0:8080->80/tcp   nginx-test
1f4574c2c036        ubuntu              "/bin/bash"              11 minutes ago      Up 11 minutes                              ubuntu02
fdb17624280d        ubuntu              "/bin/bash"              19 minutes ago      Up 12 minutes                              ubuntu01
77b4357ed7e8        centos              "/bin/bash"              2 hours ago         Up About an hour                           centos01

# nginx container에 bash shell연결 접속

[root@cent151 ~]# docker exec -it nginx-test bash
root@nginx-test:/# cat /etc/*-release
PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
NAME="Debian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
 
Docker run : Docker  컨테이너 동작
# Docker run을 통해 신규 이미지가 없으면 자동으로 다운로드 받고, 컨테이너 이름 지정과 컨테이너 호스트이름 지정 후에 attach까지 구성

[root@cent151 ~]# docker run --name ubuntu02 --hostname ubuntu02 -it ubuntu

root@ubuntu02:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@ubuntu02:/# cat /etc/*-release*
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.2 LTS"
NAME="Ubuntu"
VERSION="18.04.2 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.2 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

# -p 옵션을 통해 포트포워딩을 구성하고, -d 옵션을 통해 백그라운드 동작시킴.

[root@cent151 ~]# docker run --name nginx-test --hostname nginx-test -p 8080:80 -d nginx
278b927be07231057655a25ec19f1cb36f3163c9d78f8acc634d3e8b18552e24
[root@cent151 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
278b927be072        nginx               "nginx -g 'daemon of…"   4 seconds ago       Up 4 seconds        0.0.0.0:8080->80/tcp   nginxx-test
1f4574c2c036        ubuntu              "/bin/bash"              7 minutes ago       Up 7 minutes                               ubuntu02
fdb17624280d        ubuntu              "/bin/bash"              15 minutes ago      Up 8 minutes                               ubuntu01
77b4357ed7e8        centos              "/bin/bash"              About an hour ago   Up 45 minutes                              centos01
[root@cent151 ~]# curl http://10.72.78.151:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>


<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>


<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Docker ps : 도커 컨테이너 구동 상태 체크

# 현재 구동 중인 docker 컨테이너 열람
[root@cent151 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
1f4574c2c036        ubuntu              "/bin/bash"         2 minutes ago       Up 2 minutes                            ubuntu02
fdb17624280d        ubuntu              "/bin/bash"         10 minutes ago      Up 3 minutes                            ubuntu01
77b4357ed7e8        centos              "/bin/bash"         About an hour ago   Up 40 minutes                           centos01

# 정지 상태 포함 docker 컨테이너 열람
[root@cent151 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
1f4574c2c036        ubuntu              "/bin/bash"         3 minutes ago       Up 3 minutes                                   ubuntu02
fdb17624280d        ubuntu              "/bin/bash"         11 minutes ago      Up 4 minutes                                   ubuntu01
77b4357ed7e8        centos              "/bin/bash"         About an hour ago   Up 41 minutes                                  centos01
166bf873a7a0        centos              "/bin/bash"         2 hours ago         Exited (0) 2 hours ago                         centos02
276b823a0f56        alpine              "/bin/sh"           2 hours ago         Exited (137) 2 hours ago                       alpine04
999368649eef        alpine              "/bin/sh"           2 hours ago         Exited (137) 2 hours ago                       alpine03
251bcfc91dc8        alpine              "/bin/sh"           2 hours ago         Exited (137) 2 hours ago                       alpine02
c6813f1aa3f1        alpine              "/bin/sh"           2 hours ago         Exited (137) 2 hours ago                       alpine01

Docker rm : 컨테이너 삭제
[root@cent151 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS                  NAMES
8ce29e8e2303        nginx               "nginx -g 'daemon of…"   11 minutes ago      Up 9 minutes                 0.0.0.0:8080->80/tcp   nginx-test
1f4574c2c036        ubuntu              "/bin/bash"              19 minutes ago      Exited (127) 3 seconds ago                          ubuntu02
fdb17624280d        ubuntu              "/bin/bash"              28 minutes ago      Up 21 minutes                                       ubuntu01
77b4357ed7e8        centos              "/bin/bash"              2 hours ago         Up About an hour                                    centos01
166bf873a7a0        centos              "/bin/bash"              2 hours ago         Exited (0) 2 hours ago                              centos02
276b823a0f56        alpine              "/bin/sh"                2 hours ago         Exited (137) 2 hours ago                            alpine04
999368649eef        alpine              "/bin/sh"                2 hours ago         Exited (137) 2 hours ago                            alpine03
251bcfc91dc8        alpine              "/bin/sh"                2 hours ago         Exited (137) 2 hours ago                            alpine02
c6813f1aa3f1        alpine              "/bin/sh"                2 hours ago         Exited (137) 2 hours ago                            alpine01

# ubuntu02 컨테이너 삭제
# -f 옵션은 강제삭제

[root@cent151 ~]# docker rm ubuntu02
ubuntu02
[root@cent151 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS                  NAMES
8ce29e8e2303        nginx               "nginx -g 'daemon of…"   11 minutes ago      Up 10 minutes              0.0.0.0:8080->80/tcp   nginx-test
fdb17624280d        ubuntu              "/bin/bash"              28 minutes ago      Up 21 minutes                                     ubuntu01
77b4357ed7e8        centos              "/bin/bash"              2 hours ago         Up About an hour                                  centos01
166bf873a7a0        centos              "/bin/bash"              2 hours ago         Exited (0) 2 hours ago                            centos02
276b823a0f56        alpine              "/bin/sh"                2 hours ago         Exited (137) 2 hours ago                          alpine04
999368649eef        alpine              "/bin/sh"                2 hours ago         Exited (137) 2 hours ago                          alpine03
251bcfc91dc8        alpine              "/bin/sh"                2 hours ago         Exited (137) 2 hours ago                          alpine02
c6813f1aa3f1        alpine              "/bin/sh"                2 hours ago         Exited (137) 2 hours ago                          alpine01

Docker rmi : 도커 컨테이너 이미지 삭제
[root@cent151 ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
<none>                  <none>              dedf187fcd07        2 hours ago         307MB
alpine                  latest              4d90542f0623        4 days ago          5.58MB
ubuntu                  latest              4c108a37151f        5 days ago          64.2MB
nginx                   latest              719cd2e3ed04        13 days ago         109MB
centos                  latest              9f38484d220f        3 months ago        202MB
distribution/registry   master              0a61d6686722        5 months ago        22.9MB
google/cadvisor         latest              eb1210707573        7 months ago        69.6MB

# docker image 삭제
# -f 옵션은 강제 삭제
# 해당 이미지를 통해 현재 컨테이너가 구동중이면, 삭제 에러가 발생한다. docker는 이미지를 레이어 방식으로 사용하기 때문에 처음 배포되는 이미지를 레이어 방식으로 계속 사용함.

[root@cent151 ~]# docker rmi nginx:latest
Untagged: nginx:latest
Untagged: nginx@sha256:bdbf36b7f1f77ffe7bd2a32e59235dff6ecf131e3b6b5b96061c652f30685f3a
Deleted: sha256:719cd2e3ed04781b11ed372ec8d712fac66d5b60a6fb6190bf76b7d18cb50105
Deleted: sha256:e9b6506fb887de50972aefd99d7c5eb56b1a8e757ed953cdfecb86b5359bcb22
Deleted: sha256:55d9d9692a9615a28d183a42bc3881a72a39393feba3664e669e7affb78daa76
Deleted: sha256:cf5b3c6798f77b1f78bf4e297b27cfa5b6caa982f04caeb5de7d13c255fd7a1e
[root@cent151 ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
<none>                  <none>              dedf187fcd07        2 hours ago         307MB
alpine                  latest              4d90542f0623        4 days ago          5.58MB
ubuntu                  latest              4c108a37151f        5 days ago          64.2MB
centos                  latest              9f38484d220f        3 months ago        202MB
distribution/registry   master              0a61d6686722        5 months ago        22.9MB
google/cadvisor         latest              eb1210707573        7 months ago        69.6MB

Docker save / load : Docker 이미지 저장과 로딩
# docker 이미지를 압축하여 저장.
[root@cent151 ~]# docker save alpine -o alpine_test.tar
[root@cent151 ~]# ls
1  alpine_test.tar  anaconda-ks.cfg

# 압축 저장된 이미지를 로딩하여 사용.
[root@cent151 ~]# docker load -i ./alpine_test.tar
Loaded image: alpine:latest

Docker container Export와 Import 
# Docker container export
[root@cent151 ~]# docker export alpine01 -o alpine01.tar
[root@cent151 ~]# ls
1  alpine01.tar  alpine_test.tar  anaconda-ks.cfg

# Docker Container import
[root@cent151 ~]# docker import ./alpine01.tar
sha256:056e3fd8403cb103bb926d58e7fe2ceee56d3ca686b93dc682474fcfc229831b

Docker 이미지 history
# docker centos 이미지에 대한 히스토리 조회
[root@cent151 ~]# docker history centos
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
9f38484d220f        3 months ago        /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B
<missing>           3 months ago        /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B
<missing>           3 months ago        /bin/sh -c #(nop) ADD file:074f2c974463ab38c…   202MB

Docker 컨테이너 로그
# 생성된 컨테이너의 행위에 대한 로그 감시 가능
[root@cent151 ~]# docker logs ubuntu01
root@fdb17624280d:/# root@fdb17624280d:/# exit
root@fdb17624280d:/# exit
root@fdb17624280d:/# exit

# -f 옵션을 사용하면 라이브 로그를 볼 수 있음.


Docker 상태조회 : 컨테이너 자원 사용률에 대한 조회
# container 자원사용률에 대한 조회
# live로 보여주며, linux top과 유사
# 옵션 —-no-stream 울 적용하면 현재 상태만 표시함.
[root@cent151 ~]# docker stats
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
fdb17624280d        ubuntu01            0.00%               812KiB / 7.638GiB   0.01%               740B / 0B           0B / 0B             2
77b4357ed7e8        centos01            0.00%               376KiB / 7.638GiB   0.00%               740B / 0B           0B / 0B             1

Docker top : 특정 컨테이너의 프로세스 사용 정보 조회
# 특정 컨테이너의 프로세스 사용 정보 조회
[root@cent151 ~]# docker top ubuntu01
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                3272                3254                0                   01:14               pts/0               00:00:00            /bin/bash
root                4617                3254                0                   01:27               ?                   00:00:00            bash

기타 유용한 docker command 관련 팁.
docker command complete - Mac OS, Ubuntu, CentOS 에서 docker 명령어 자동완성 구성 방법

# Mac book 에서 구성 방법

#oh-my-zsh 사용 케이스에서 구성

vi ~/.zshrc

plugins=(
   docker
   docker-compose
   )

## plugin autoload
echo "autoload -Uz compinit; compinit" >> ~/.zshrc

# CentOS

yum update
yum -y install bash-completion
curl https://raw.githubusercontent.com/docker/docker-ce/master/components/cli/contrib/completion/bash/docker -o /etc/bash_completion.d/docker.sh

# Ubuntu

apt-get update
apt-get install bash-completion
curl https://raw.githubusercontent.com/docker/docker-ce/master/components/cli/contrib/completion/bash/docker -o /etc/bash_completion.d/docker.sh



공지사항