Products & Services/Docker

[Docker] docker image - Docker 이미지 관리 명령어

TechNote.kr 2021. 2. 16. 16:16
728x90

 

docker image 명령어 목록

technote@TechNote:~$ docker image --help

Usage:  docker image COMMAND

Manage images

Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Run 'docker image COMMAND --help' for more information on a command.


Docker 이미지 빌드 :
Dockerfile을 기반으로 docker 이미지를 빌드한다. 

$ docker image build -t "Image 이름" "Dockerfile 경로"

technote@TechNote:~/docker$ docker image ls
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
technote@TechNote:~/docker$ docker image build -t my-test-app .
Sending build context to Docker daemon  3.072kB
Step 1/3 : FROM python:3
3: Pulling from library/python
0ecb575e629c: Pull complete 
7467d1831b69: Pull complete 
feab2c490a3c: Pull complete 
f15a0f46f8c3: Pull complete 
937782447ff6: Pull complete 
e78b7aaaab2c: Pull complete 
06c4d8634a1a: Pull complete 
42b6aa65d161: Pull complete 
f7fc0748308d: Pull complete 
Digest: sha256:ca8bd3c91af8b12c2d042ade99f7c8f578a9f80a0dbbd12ed261eeba96dd632f
Status: Downloaded newer image for python:3
 ---> 2a93c239d591
Step 2/3 : COPY main.py ./
 ---> 77da3887c4b1
Step 3/3 : CMD ["python3", "./main.py"]
 ---> Running in d3cc2e0c5522
Removing intermediate container d3cc2e0c5522
 ---> ca344f358767
Successfully built ca344f358767
Successfully tagged my-test-app:latest
technote@TechNote:~/docker$ docker image ls
REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
my-test-app   latest    ca344f358767   54 seconds ago   885MB
python        3         2a93c239d591   6 days ago       885MB

새로운 docker image 가 생성된 것을 확인할 수 있다.

[Docker 이미지 빌드시 사용된 Dockerfile]

FROM python:3

COPY main.py ./

CMD ["python3", "./main.py"]

새로 생성된 이미지가 공식 python 3 이미지를 기반으로 하기에 python 이미지 또한 다운로드되어 있는 것을 알 수 있다. 


Docker 이미지 이력 확인 :
Docker 이미지의 생성 이력을 확인한다. 

$ docker image build -t "Image 이름" "Dockerfile 경로"

technote@TechNote:~/docker$ docker image ls
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
my-test-app   latest    ca344f358767   2 minutes ago   885MB
python        3         2a93c239d591   6 days ago      885MB
technote@TechNote:~/docker$ docker image history my-test-app
IMAGE          CREATED         CREATED BY                                      SIZE      COMMENT
ca344f358767   2 minutes ago   /bin/sh -c #(nop)  CMD ["python3" "./main.py…   0B        
77da3887c4b1   2 minutes ago   /bin/sh -c #(nop) COPY file:05e1e101e1853515…   21B       
2a93c239d591   6 days ago      /bin/sh -c #(nop)  CMD ["python3"]              0B        
<missing>      6 days ago      /bin/sh -c set -ex;   wget -O get-pip.py "$P…   8.01MB    
<missing>      6 days ago      /bin/sh -c #(nop)  ENV PYTHON_GET_PIP_SHA256…   0B        
<missing>      6 days ago      /bin/sh -c #(nop)  ENV PYTHON_GET_PIP_URL=ht…   0B        
<missing>      6 days ago      /bin/sh -c #(nop)  ENV PYTHON_PIP_VERSION=21…   0B        
<missing>      6 days ago      /bin/sh -c cd /usr/local/bin  && ln -s idle3…   32B       
<missing>      6 days ago      /bin/sh -c set -ex   && wget -O python.tar.x…   55.7MB    
<missing>      6 days ago      /bin/sh -c #(nop)  ENV PYTHON_VERSION=3.9.1     0B        
<missing>      6 days ago      /bin/sh -c #(nop)  ENV GPG_KEY=E3FF2839C048B…   0B        
<missing>      6 days ago      /bin/sh -c apt-get update && apt-get install…   18MB      
<missing>      6 days ago      /bin/sh -c #(nop)  ENV LANG=C.UTF-8             0B        
<missing>      6 days ago      /bin/sh -c #(nop)  ENV PATH=/usr/local/bin:/…   0B        
<missing>      7 days ago      /bin/sh -c set -ex;  apt-get update;  apt-ge…   510MB     
<missing>      7 days ago      /bin/sh -c apt-get update && apt-get install…   146MB     
<missing>      7 days ago      /bin/sh -c set -ex;  if ! command -v gpg > /…   17.5MB    
<missing>      7 days ago      /bin/sh -c set -eux;  apt-get update;  apt-g…   16.5MB    
<missing>      7 days ago      /bin/sh -c #(nop)  CMD ["bash"]                 0B        
<missing>      7 days ago      /bin/sh -c #(nop) ADD file:8f75f11b2bd2d50e5…   114MB     


Docker 이미지 목록 확인 :
Docker 이미지 목록을 확인한다. 

$ docker image ls
$ docker image ls -a

technote@TechNote:~/docker$ docker image ls
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
my-test-app   latest    ca344f358767   4 minutes ago   885MB
python        3         2a93c239d591   6 days ago      885MB
technote@TechNote:~/docker$ docker image ls -a
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
my-test-app   latest    ca344f358767   4 minutes ago   885MB
<none>        <none>    77da3887c4b1   4 minutes ago   885MB
python        3         2a93c239d591   6 days ago      885MB

-a 옵션 : Show all images (default hides intermediate images)

Docker images have intermediate layers that increase reusability, decrease disk usage, and speed up docker build by allowing each step to be cached. These intermediate layers are not shown by default.

Docker image는 cache 와 같은 역할을 하는 intermediate layer 를 가지고 있다. (intermediate image)
기본적으로 ls 시에는 보이지 않고, -a 옵션을 했을 때만 보인다. 

docker image history 시 최종 image 생성전 중간에 intermediate image 가 생기는 것을 Image ID를 통해 확인할 수 있다. 


Docker dangling 이미지 전체 삭제:
Docker 사용 중 발생할 수 있는 dangling 이미지 전체를 삭제한다.  

$ docker image prune

technote@TechNote:~/docker$ docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B


Docker 이미지 삭제 :
Docker 이미지를 삭제한다. 

$ docker image rm "Image 이름:[태그]"

technote@TechNote:~/docker$ docker image ls
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
my-test-app   latest    ca344f358767   7 minutes ago   885MB
python        3         2a93c239d591   6 days ago      885MB
technote@TechNote:~/docker$ docker image rm my-test-app:latest 
Untagged: my-test-app:latest
Deleted: sha256:ca344f358767bed3419c9b9c8ce2f8bb5b115d13b10b536bef8222561c6614cf
Deleted: sha256:77da3887c4b19340a4601cd55d46c74f00611036e18968eddcad176feabc4b1f
Deleted: sha256:3d07b1cb1ba54ae2785be9298208ddcd013358fd3fc1cee403e5ece168aaf0dc
technote@TechNote:~/docker$ docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
python       3         2a93c239d591   6 days ago   885MB

 

 

 

728x90
1 2 3 4 5