관리 메뉴

bright jazz music

[Docker] 기초 명령어 본문

OS 및 기타/docker & kubernetes

[Docker] 기초 명령어

bright jazz music 2023. 1. 15. 18:52

Docker 기본 CLI명령어

 

https://docs.docker.com/engine/reference/commandline/docker/

 

docker

docker: The base command for the Docker CLI.

docs.docker.com

 

 

우선 Docker hub에서 centOS 이미지를 다운받아 테스트하겠다.

https://hub.docker.com/_/centos

 

centos - Official Image | Docker Hub

DEPRECATION NOTICE This image is no longer supported/maintained (non-EOL tags last updated November 16, 2020, docker-library/official-images#9102; see also https://www.centos.org/centos-linux-eol/ and docker-library/docs#2205). Please adjust your usage acc

hub.docker.com

 

1. docker pull [image]

: 필요한 이미지를 도커 허브에서 다운로드한다.

[root@localhost ~]# docker pull centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
[root@localhost ~]#

7.9 버전으로 받으려고 했는데 실수로 버전을 입력하지 않아 다시 명령함.

docker pull [image] [TAG]

[root@localhost ~]# docker pull centos:7.9.2009
7.9.2009: Pulling from library/centos
Digest: sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4
Status: Downloaded newer image for centos:7.9.2009
docker.io/library/centos:7.9.2009

 

2. docker images

시스템에 존재하는 docker 이미지들을 보여준다. -a 옵션을 붙이면 숨겨진 이미지까지 모두 보여준다.

[root@localhost ~]# docker images -a
REPOSITORY    TAG        IMAGE ID       CREATED         SIZE
hello-world   latest     feb5d9fea6a5   15 months ago   13.3kB
centos        7          eeb6ee3f44bd   16 months ago   204MB
centos        7.9.2009   eeb6ee3f44bd   16 months ago   204MB
[root@localhost ~]#

 

3. docker run 

이미지로부터 컨테이너를 생성하고 그 안의 내용을 실행한다. (구동의 의미에 가깝다)

 

docker run hello-world를 명령하면, hello-world 이미지로부터 컨테이너를 생성하고 그 안의 내용물을 실행한 뒤, 실행이 완료되면 종료된다.

만약 해당 이미지가 존재하지 않는 경우 이미지를 docker hub에서 찾고 있으면 pull 한 뒤 실행한다.

 

현재 centos7과 centos7.9.2009가 모두 존재하는 상황에서,

아래와 같이 "TAG"를 붙여 주지 않고 "REPOSITORY"만 넣고 명령을 실행하면 어떻게 될까?

 

docker run centos

 

도커 허브에서 centos의 latest 버전을 다운받아서 실행한다.

 

따라서 docker run으로 기존의 이미지를 실행할 때는 TAG까지 붙여서 실행하는 것이 좋다.

 

docker run [REPOSITORY] : [TAG]

e.g.) docker run centos:7.9.2009 

 

그런데 만약 이렇게 실행하게 되면 docker ps -a로 현재 실행중인 도커 컨테이너(프로세스)를 조회했을 때, 아무 이름이나 NAMES항목에 들어가게 된다

따라서 --name 옵션을 사용하여 컨테이너의 이름을 정해주자.

 

형식) docker run --name [NAMES] [IMAGE]
예) docker run --name testcentos7.9 centos:7.9.2009  

 

4. docker ps

현재 실행 중인 도커 컨테이너를 보여준다. -a 옵션을 붙여주면 비실행 중인 컨테이너까지 모두 보여준다.

 

5. docker exec

docker exec -it [NAMES 또는 CONTAINER ID] [COMMAND]

 

e.g.) docker exec -it testcentos7.9 /bin/bash

 

6. 도커 컨테이너를 백그라운드로 실행하기

 
docker run -d -it --name [NAMES] [IMAGE]
예시) docker run -d -it --name testcentos7 centos:7.9.2009

[root@localhost ~]# docker run -d -it --name testcentos7 centos:7.9.2009
5b501d781655eaa4e8bb987ff7d367bd39cfa23dfed644b23464d2f7a3e32704
[root@localhost ~]# docker exec -it testcentos7 /bin/bash
[root@5b501d781655 /]#

 

7. docker commit

docker commit -p [CONTAINER ID] [NAMES]

 

7.1 구동 중인 컨테이너 복제

순서

  1. docker commit 명령어 사용하여 구동 중인 컨테이너 커밋
  2. docker images 명령어로 생성된 이미지 확인
  3. docker run -d -it 사용하여 구동
[root@localhost ~]#

//현재 실행중인 컨테이너 확인
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE             COMMAND       CREATED             STATUS             PORTS     NAMES
5b501d781655   centos:7.9.2009   "/bin/bash"   About an hour ago   Up About an hour             testcentos7
[root@localhost ~]#

//실행중인 컨테이너 커밋
//sudo docker commit [CONTAINER_ID] [new_image_name = 저장할 이름]
[root@localhost ~]# sudo docker commit 5b501d781655 idn2
sha256:1136a0d7672cc10e68b4c2689bb59e3c8a8fa10e54a1c167cad46835c60ef6bb
[root@localhost ~]#

//도커 이미지 확인
[root@localhost ~]# sudo docker images
REPOSITORY    TAG        IMAGE ID       CREATED          SIZE
idn2          latest     1136a0d7672c   34 seconds ago   204MB //이번에 commit한 이미지
clear         latest     2d492b13f164   16 minutes ago   204MB
testcentos7   latest     c131f7ace8bb   32 minutes ago   204MB
hello-world   latest     feb5d9fea6a5   15 months ago    13.3kB
centos        7          eeb6ee3f44bd   16 months ago    204MB
centos        7.9.2009   eeb6ee3f44bd   16 months ago    204MB
[root@localhost ~]#

//commit한 것을 백그라운드에서 실행하기
// docker run -d -it --name [NAMES] [IMAGE]
[root@localhost ~]# docker run -d -it --name idn_second idn2
3e7b6288ff77fa7112f883bb39c02d949aba936f43fb37e4ce73a97fe0d48014
[root@localhost ~]#
[root@localhost ~]#

//구동중인 도커 컨테이너 재확인. 실행되고 있는 것을 볼 수 있음.
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE             COMMAND       CREATED             STATUS             PORTS     NAMES
3e7b6288ff77   idn2              "/bin/bash"   26 seconds ago      Up 25 seconds                idn_second
5b501d781655   centos:7.9.2009   "/bin/bash"   About an hour ago   Up About an hour             testcentos7
[root@localhost ~]#

//복제한 컨테이너에 접근
[root@localhost ~]# docker exec -it idn_second /bin/bash
[root@3e7b6288ff77 /]#

//testcentos7 컨테이너에서 생성한 test.txt 파일 존재하는지 확인
[root@3e7b6288ff77 /]# ls
anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  test.txt  tmp  usr  var
[root@3e7b6288ff77 /]#
[root@3e7b6288ff77 /]#

idn2는 testcentos7을 commit하여 만든 이미지이다.

이걸 docker run -d -it 명령어를 통해 구동한다.

그리고 docker exec -it 명령어를 통해 접근하여testcentos7에서 생성해 놓은 test.txt가 존재하는지 확인한다.

 

idn_second에서 test.txt 파일을 수정한다고 해도 testcentos7컨테이너에 존재하는 test.txt 파일에는 영향이 없다.

 

 

 

7.2 구동 중인 컨테이너 백업 후 복원하기

 

[root@localhost ~]# docker commit -p 5b501d781655 testcentos7
sha256:c131f7ace8bb4961be6bc92c84dff588774dcdd3df15509abbb5d6b74d579d30
[root@localhost ~]#

 

저장(back up)

[root@localhost ~]# sudo docker save -o idnuser2.tar testcentos7
[root@localhost ~]#
//명령어를 실행시킨 위치에 tar 파일이 만들어짐
[root@localhost ~]#
[root@localhost ~]# ll
total 206748
-rw-------. 1 root root      1262 Feb  5  2022 anaconda-ks.cfg
-rw-------. 1 root root 211702784 Jan 15 19:26 idnuser2.tar
[root@localhost ~]#

 

복원(restore)

[root@localhost ~]# docker load < idnuser2.tar
Loaded image: testcentos7:latest

 

8. docker stop [NAMES]

 

[root@localhost ~]#
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE             COMMAND       CREATED          STATUS          PORTS     NAMES
b055712d56b7   idn               "/bin/bash"   53 minutes ago   Up 53 minutes             idn_first
3e7b6288ff77   idn2              "/bin/bash"   53 minutes ago   Up 53 minutes             idn_second
5b501d781655   centos:7.9.2009   "/bin/bash"   2 hours ago      Up 2 hours                testcentos7
[root@localhost ~]# docker stop idn_first
idn_first
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE             COMMAND       CREATED          STATUS          PORTS     NAMES
3e7b6288ff77   idn2              "/bin/bash"   54 minutes ago   Up 54 minutes             idn_second
5b501d781655   centos:7.9.2009   "/bin/bash"   2 hours ago      Up 2 hours                testcentos7
[root@localhost ~]#

 

중지된 컨테이너 재시작

docker restart [container_id] 또는 [container_name]

중지 이전 처음 구동했을 때의 설정으로 재시작 된다.

 

 

컨테이너 삭제

docker rm [container_id]  or  docker container rm [container_id]

docker rm -f  강제 삭제

 

이미지 삭제

 

docker rmi [container_ID] or [container_name]

 

강제 삭제

docker rmi -f  

 

여러 개 삭제

docker rmi [image1] [image2] [image3]

 

9. 호스트 -컨테이너 간 파일 전송

 

1. 호스트 -> 컨테이너

명령어 : docker cp /path입력/text.txt  컨테이너명:/path입력/text.txt

ex : docker cp /tmp/text.txt dockertest:/tmp/text.txt

2. 컨테이너 -> 호스트

명령어 : docker cp 컨테이너명:/path입력/text.txt  /path입력/text.txt 

ex : docker cp dockertest:/tmp/text.txt /tmp/text.txt 

 

 

 

10. 포트포워딩

 

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE                COMMAND       CREATED       STATUS                   PORTS     NAMES
5b501d781655   centos:7.9.2009      "/bin/bash"   2 hours ago   Up 2 hours                         testcentos7
4b5ca78267dd   centos:7.9.2009      "/bin/bash"   3 hours ago   Exited (0) 3 hours ago             testcentos7.9
9defb22e138d   centos:7.9.2009      "/bin/bash"   3 hours ago   Exited (0) 3 hours ago             infallible_blackburn
e13c3f78d804   centos:7             "/bin/bash"   3 hours ago   Exited (0) 3 hours ago             blissful_turing
e224df77a7d1   hello-world:latest   "/hello"      3 hours ago   Exited (0) 3 hours ago             ecstatic_pascal
5400b56f7540   centos:7.9.2009      "/bin/bash"   3 hours ago   Exited (0) 3 hours ago             interesting_yalow
77c4501b54f3   hello-world          "/hello"      3 hours ago   Exited (0) 3 hours ago             sweet_moore
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# docker run  -d -it -p 8080:8080 --name idn_second idn2
13521e7c32510f9fbfaf551236dbe4fd258681180c8d256a9880d987209b1573
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE             COMMAND       CREATED         STATUS         PORTS                                       NAMES
13521e7c3251   idn2              "/bin/bash"   4 seconds ago   Up 3 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   idn_second
5b501d781655   centos:7.9.2009   "/bin/bash"   2 hours ago     Up 2 hours                                                 testcentos7
[root@localhost ~]#
Comments