[Docker] Docker 기본 명령어

소개

  • 오늘은 Docker 3번째 시간으로써, 간단한 도커 기본 명령어들을 사용하는 방법에 대해서 알아 보려고 합니다.
  • 해당 포스팅에서는 Docker가 기본적으로 설치가 되어 있다고 가정 하에 진행하도록 하겠습니다.
  • 만약, Docker가 설치되어 있지 않다면 Docker를 우선 설치 하고 따라하시길 추천 드립니다.
  • 오늘은 Docker의 가장 기본이 되는 몇가지 명령어를 DockerHub에 올라와 있는 "hello-world" 이미지를 이용하여 명령어를 알아보도록 하겠습니다.

version 명령어

  • Docker가 본인 PC에 정상적으로 설치가 되어 있다면, cmd 혹은 powershell 을 실행하여 아래와 같이 명령어를 작성해 주세요.
docker version

Client:
 Cloud integration: 1.0.17
 Version:           20.10.7
 API version:       1.41
 Go version:        go1.16.4
 Git commit:        f0df350
 Built:             Wed Jun  2 12:00:56 2021
 OS/Arch:           windows/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.7
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       b0f5bc3
  Built:            Wed Jun  2 11:54:58 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.6
  GitCommit:        d71fcd7d8303cbf684402823e425e9dd2e99285d
 runc:
  Version:          1.0.0-rc95
  GitCommit:        b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
  • docker version 명령어를 통해서 docker의 client, server 버전 정보를 확인할 수 있습니다.

search 명령어

  • 다음은 search 명령어에 대해서 알아보겠습니다.
  • search 명령어는 사용자가 DockerHub 사이트에 가지 않고도, 해당 원격 저장소에 image를 검색할 수 있는 명령어 입니다.
docker search hello-world

NAME                                       DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
hello-world                                Hello World! (an example of minimal Dockeriz…   1508      [OK]
kitematic/hello-world-nginx                A light-weight nginx container that demonstr…   150
tutum/hello-world                          Image to test docker deployments. Has Apache…   84                   [OK]
dockercloud/hello-world                    Hello World!                                    19                   [OK]
crccheck/hello-world                       Hello World web server in under 2.5 MB          15                   [OK]
vad1mo/hello-world-rest                    A simple REST Service that echoes back all t…   5                    [OK]
arm32v7/hello-world                        Hello World! (an example of minimal Dockeriz…   3
ppc64le/hello-world                        Hello World! (an example of minimal Dockeriz…   2
souravpatnaik/hello-world-go               hello-world in Golang                           1
markmnei/hello-world-java-docker           Hello-World-Java-docker                         1                    [OK]
thomaspoignant/hello-world-rest-json       This project is a REST hello-world API to bu…   1
ansibleplaybookbundle/hello-world-db-apb   An APB which deploys a sample Hello World! a…   1                    [OK]
ansibleplaybookbundle/hello-world-apb      An APB which deploys a sample Hello World! a…   1                    [OK]
datawire/hello-world                       Hello World! Simple Hello World implementati…   1                    [OK]
freddiedevops/hello-world-spring-boot                                                      0
strimzi/hello-world-producer                                                               0
strimzi/hello-world-streams                                                                0
koudaiii/hello-world                                                                       0
burdz/hello-world-k8s                      To provide a simple webserver that can have …   0                    [OK]
nirmata/hello-world                                                                        0                    [OK]
businessgeeks00/hello-world-nodejs                                                         0
strimzi/hello-world-consumer                                                               0
infrastructureascode/hello-world           A tiny "Hello World" web server with a healt…   0                    [OK]
garystafford/hello-world                   Simple hello-world Spring Boot service for t…   0                    [OK]
dandando/hello-world-dotnet                                                                0

pull 명령어

  • pull 명령어를 사용하면 DockerHub 즉, 원격 저장소에 있는 이미지를 로컬 저장소로 저장할 수 있습니다.
  • DockerHub에 hello-world 라는 이미지가 있는데, 해당 이미지를 한번 제 로컬 저장소로 가져오도록 하겠습니다.
docker pull hello-world

Using default tag: latest
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:0fe98d7debd9049c50b597ef1f85b7c1e8cc81f59c8d623fcb2250e8bec85b38
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
  • 위와 같이 tag가 latest(최신) 버전의 hello-world 이미지를 로컬로 가져온 것을 확인할 수 있습니다.
  • 만약, 최신 버전이 아닌 이전의 버전을 저장하고 싶다면, docker pull [이미지이름] [태그번호] 이렇게 명령어를 쓰면 해당 태그 정보에 맞는 버전의 이미지를 로컬로 가져올 수 있습니다.

images 명령어

  • 다음은 images 명령어 입니다.
  • 앞서, hello-world 이미지를 로컬로 저장하였습니다.
  • 정상적으로 저장 되었는지 imgaes 명령어를 통해 이미지 목록을 확인할 수 있습니다.
docker images

REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   5 months ago   13.3kB
  • 위와 같이 hello-world 이미지가 목록에 조회되는 것을 확인할 수 있습니다.

run 명령어

  • 다음은 run 명령어 입니다.
  • run 명령은 도커 이미지를 컨테이너로 생성하고 실행하는 명령어 입니다.
  • 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.
    (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/
  • 위와 같이 run 명령어를 통해 hello-world 이미지 컨테이너를 생성하고 실행시켜 보았습니다.
  • 해당 컨테이너가 제대로 생성되었는지 확인할 수 있는 명령어가 있습니다.
  • 바로 아래에서 설명 드리도록 하겠습니다.

컨테이너 목록 확인 명령어

  • 앞에서 hello-world 컨테이너를 하나 생성하였습니다.
  • ps 명령어를 통해 현재 컨테이너 목록을 확인할 수 있습니다.
docker ps -a

CONTAINER ID   IMAGE         COMMAND                  CREATED              STATUS                          PORTS                                   NAMES
a455944bcd80   hello-world   "/hello"                 About a minute ago   Exited (0) About a minute ago                                           busy_mirzakhani
  • 위와 같이 ContainerID, Image, Command, Created, Status, Names 들의 정보를 확인할 수 있습니다.
  • ps -a 라고 하면 STATUS 가 UP 또는 Exited 모든 상태의 목록을 볼 수 있습니다.
  • ps 명령어만 입력하면 STATUS가 UP인 상태만 목록에 표시됩니다.

name 명령어

  • 다음은 name 명령어를 통해 컨테이너 이름을 지정할 수 있습니다.
docker run --name myHelloWorld 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 ps -a

CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS                      PORTS                                   NAMES
3e8634c8d9cd   hello-world   "/hello"                 22 seconds ago   Exited (0) 20 seconds ago                                           myHelloWorld
a455944bcd80   hello-world   "/hello"                 4 minutes ago    Exited (0) 4 minutes ago                                            busy_mirzakhani
  • 위와 같이 컨테이너 이름이 "myHelloWorld" 라는 컨테이너가 새롭게 생성된 것을 확인할 수 있습니다.

컨테이너 삭제 명령어

  • 이제는 생성한 컨테이너를 삭제해 보도록 하겠습니다.
  • 컨테이너 삭제는 rm 명령어를 통해서 컨테이너를 삭제할 수 있습니다.
  • docker rm [컨테이너이름] or [컨테이너ID]
docker rm myHelloWorld
myHelloWorld

docker ps -a

CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS                      PORTS                                   NAMES
a455944bcd80   hello-world   "/hello"                 5 minutes ago   Exited (0) 5 minutes ago                                            busy_mirzakhani
  • 위와 같이 컨테이너가 삭제된 것을 확인할 수 있습니다.

이미지 삭제 명령어

  • 이제 컨테이너를 삭제 했으니, Image도 삭제해 보도록 하겠습니다.
  • 이미지 삭제는 rmi 명령어를 통해서 삭제할 수 있습니다.
docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   5 months ago   13.3kB

docker rmi hello-world
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container a455944bcd80 is using its referenced image d1165f221234

docker ps -a
CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS                      PORTS                                   NAMES
a455944bcd80   hello-world   "/hello"                 6 minutes ago   Exited (0) 6 minutes ago                                            busy_mirzakhani

docker rm a455
a455

docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED        STATUS                      PORTS                                   NAMES

docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   5 months ago   13.3kB

docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:0fe98d7debd9049c50b597ef1f85b7c1e8cc81f59c8d623fcb2250e8bec85b38
Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726
Deleted: sha256:f22b99068db93900abe17f7f5e09ec775c2826ecfe9db961fea68293744144bd

docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
  • 여기서 핵심은 Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container a455944bcd80 is using its referenced image d1165f221234 이 에러 코드입니다.
  • 해당 에러가 발생한 이유는, hello-world 라는 컨테이너가 아직 존재하기 때문에 이미지를 삭제할 수 없는 것이고 먼저 hello-world로 생성된 모든 컨테이너를 삭제하고나서 그 다음으로 이미지를 삭제하라는 에러 메시지 입니다.
  • 때문에, ContainerID가 a455인 컨테이너를 삭제하고 최종적으로 이미지까지 삭제된 것을 확인할 수 있습니다.
  • 이렇게 오늘은 간단한 Docker 기본 명령어를 살펴 보았습니다.
728x90

이 글을 공유하기

댓글

Designed by JB FACTORY