코드/CI-CD-Docker

[Docker] 중지된 컨테이너 자동 제거 하기

Yeah-Panda 2024. 7. 10. 09:51

도커 이미지와 컨테이너를 실행한 이후 소스 코드 수정등으로 이미지와 컨테이너를 새로 생성해야하는 경우가 많다.

이럴 경우 컨테이너 생성시 하나의 옵션을 추가하면 컨테이너가 중지될때 자동으로 제거된다.

 

 

얘를 날려보면

docker run --help

   --rm            Automatically remove the container and its associated

 

--rm 이라는 옵션을 사용할수 있다.

➜  docker build .
[+] Building 3.8s (12/12) FINISHED                                                docker:desktop-linux
 => [internal] load build definition from Dockerfile                                              0.0s
 => => transferring dockerfile: 221B                                                              0.0s
 => [internal] load metadata for docker.io/library/node:20.12.2-alpine3.19                        0.0s
 => [internal] load .dockerignore                                                                 0.0s
 => => transferring context: 2B                                                                   0.0s
 => [1/7] FROM docker.io/library/node:20.12.2-alpine3.19                                          0.0s
 => [internal] load build context                                                                 0.1s
 => => transferring context: 49.37kB                                                              0.1s
 => CACHED [2/7] WORKDIR /app                                                                     0.0s
 => CACHED [3/7] COPY package.json /app                                                           0.0s
 => CACHED [4/7] RUN corepack enable                                                              0.0s
 => CACHED [5/7] RUN pnpm install                                                                 0.0s
 => [6/7] COPY . /app                                                                             0.1s
 => [7/7] RUN pnpm docs:build                                                                     3.6s
 => exporting to image                                                                            0.0s
 => => exporting layers                                                                           0.0s
 => => writing image sha256:2535b6c61759f73e63f52d815c2cb46951e9085eacb1284947ef7af082369b82      0.0s


# 생성된 이미지 확인

➜  docker images
REPOSITORY   TAG                  IMAGE ID       CREATED         SIZE
<none>       <none>               2535b6c61759   5 seconds ago   267MB
<none>       <none>               64393808cb77   5 weeks ago     268MB
<none>       <none>               2ef122f569f5   5 weeks ago     268MB
<none>       <none>               26dcc0d20705   5 weeks ago     268MB
<none>       <none>               49b364a707f7   5 weeks ago     268MB
<none>       <none>               63a51fc658ac   5 weeks ago     1.02GB
node         20.12.2-alpine3.19   3b62bffc0937   2 months ago    133MB

# 컨테이너를 생성한다 -d: detach --rm: 이 부분 추가
➜   docker run -p 4000:4173 -d --rm 2535b6c61759
a36981e1bfadf572cf007a14876842aa6bcc2e65899e7c3044daeda65880102a

# 활성화된 컨테이너 확인
➜   docker ps
CONTAINER ID   IMAGE          COMMAND                   CREATED          STATUS          PORTS                    NAMES
a36981e1bfad   2535b6c61759   "docker-entrypoint.s…"   27 seconds ago   Up 27 seconds   0.0.0.0:4000->4173/tcp   stupefied_fermat

# 해당 컨테이너를 중지.
➜  thoughtofyou-vite-press git:(master) docker stop stupefied_fermat
stupefied_fermat

# 컨테이너 목록 확인
➜  thoughtofyou-vite-press git:(master) docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

# 컨테이너 삭제 확인
➜  docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED       STATUS                    PORTS     NAMES
9c641cc74339   64393808cb77   "docker-entrypoint.s…"   5 weeks ago   Exited (0) 24 hours ago             trusting_ride
6272feac053b   2ef122f569f5   "docker-entrypoint.s…"   5 weeks ago   Exited (1) 5 weeks ago              interesting_feynman
402ca3ac9967   2ef122f569f5   "docker-entrypoint.s…"   5 weeks ago   Exited (0) 5 weeks ago              optimistic_proskuriakova
f4f846c43000   26dcc0d20705   "docker-entrypoint.s…"   5 weeks ago   Exited (0) 5 weeks ago              flamboyant_hofstadter
daf5af8de73a   49b364a707f7   "docker-entrypoint.s…"   5 weeks ago   Exited (0) 5 weeks ago              dazzling_sutherland
9ecbad2dc4bd   49b364a707f7   "docker-entrypoint.s…"   5 weeks ago   Exited (0) 5 weeks ago              silly_lumiere
71ecc7010d01   63a51fc658ac   "python rng.py"           5 weeks ago   Exited (0) 5 weeks ago              magical_euler

# 이미지는 아직 존재
➜  thoughtofyou-vite-press git:(master) docker images
REPOSITORY   TAG                  IMAGE ID       CREATED          SIZE
<none>       <none>               2535b6c61759   31 minutes ago   267MB
<none>       <none>               64393808cb77   5 weeks ago      268MB
<none>       <none>               2ef122f569f5   5 weeks ago      268MB
<none>       <none>               26dcc0d20705   5 weeks ago      268MB
<none>       <none>               49b364a707f7   5 weeks ago      268MB
<none>       <none>               63a51fc658ac   5 weeks ago      1.02GB
node         20.12.2-alpine3.19   3b62bffc0937   2 months ago     133MB

 

매번 컨테이너를 중지시키고 삭제하는 과정을 번거롭게 진행할 필요 없는 옵션이다.