Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

:)

docker CLI 본문

Docker

docker CLI

mihee 2022. 4. 20. 21:18
  • docker daemon과 통신하여 기능을 수행
    • binary : docker
      • docker group을 supplementary group에 포함시켜야 사용이 가능

id - runtime , id mihee - configure

  • 그룹 추가 후에는 session을 재생성해야 groups 정보를 다시 읽는다 -> 재로그인
    1. X window에서 로그아웃
    2. CTRL+ALT+F4 tty4 이동 후 console에 root로 로그인
    3. systemctl restart gdm
    4. X window 로그인

  • docker : run ( name)

10개의 hello-world container 만들기 (이름은 hello-world_1 hello-world_2..)

$ for ii in {1..10}; do docker run --name hello-world_${ii} hello-world; done
$ docker ps -a
  • docker : filter
$ docker ps -a -f 'name=hello'
$ docker ps -af 'name=hello_[1-3]'   -> glob pattern을 지원

$ docker ps -af 'name=hel' --format "{{.ID}} {{.Image}} {{.Status}} {{.Names}}"
  • rm : remove containers
$ docker ps -af 'name=hello' --format "{{.Names}}"
$ docker ps -af 'name=hello' --format "{{.Names}}" | xargs docker rm
  • rmi : remove images
$ docker rmi hello-world  -> container가 존재하기 때문에 image삭제 실패. container 삭제 후 image를 만든다
$ docker ps -a -f 'ancestor=hello-world' --format {{.Names}} | xargs docker rm
$ docker rmi hello-world

docker : image

  • image : ls
    • REPOSITORY : docker image 저장소 이름
    • TAG : 태그 이름(버전)
$ docker image ls

  • images : pull
    • 당장 run 목적이 아니라, 나중에 Run 하거나 build or save 목적인 경우에 pull 한다.
$ docker image pull [repository]
$ docker pull [repository]
  • images : inspect
    • docker image 안에 있는 설정, 환경을 보기 위해 사용
$ docker image inspect nginx
$ docker image inspect -f '{{.Config.Env}}' nginx  -> format을 이용해 특정 정보만 볼 수 있음
  • images : save/load
    • save images to a tar archive
    • 여러 서버에 복사하거나 종종 사용하는 이미지는 network를 절약하거나 여러 문제를 일으키지 않도록 save해서 백업해두고 load로 사용하는 것이 좋다.
    • load image from a tar archive or STDIN
$ docker image save nginx > docker_nginx_1.19.10.tar
$ docker image load < docker_nginx_1.19.10.tar
  • exec
    • run은 새롭게 container를 실행하는 명령
    • exec는 기존에 존재하는 Container에서 실행하는 명령
$ docker exec -it ubuntu_top top "-d 0.2"

  • binding
    • container의 자원을 외부와 연결
      • 일반적으로 I/O와 storage관련을 연결
        • network
          • port binding : host OS의 port와 container의 port를 바인딩
          • network : docker network 사용
        • directory, file, block-device
          • mount binding : host OS의 directory를 바인딩
          • volumn : docker volumn 저장소 사용
          • device : host OS의 device, GPU를 바인딩
        • environment
          • shell environment variables를 지정
  • binding : net
    • 80/tcp 포트를 host OS의 포트에 바인딩시키면 외부에 연결 가능
    • 1번 터미널 : nginx container 실행
$ docker run --rm -p 8080:80/tcp --name nginx_8080 nginx
--rm : automatically remove the container when it exits
-p : port binding
8080 : host port number
80 : container port number
  • 2번 터미널 : 8080 포트 LISTEN 확인
$ ss -nlt 'sport = :8080'
-n : numeric
-l : listen
-t : tcp
  • 1번 터미널 : Stopping
    • container가 foreground로 작동하고 있기 때문에 해당 프로세스를 ^C로 죽이면 docker가 닫히게 되고, --rm 옵션으로 인해 자동 삭제됨.
  • binding : net : detach
    • 1번 터미널 : detach mode (background mode)

  • 2번 터미널 : logs (following mode를 사용하려면 -f 옵션 사용)
$ docker logs nginx_8080
  • attach, detach
    • docker run -it ...
      • interactive mode & terminal을 사용하는 경우
        • container를 running 상태로 두고 잠시 빠져나올때는 detach를 할 수 있다.(escape key : ^P^Q)
      • 처음 실행할 때 -d 옵션을 추가하면 detach mode로 실행 가능
$ docker run --rm -itd --name ubuntu_bash ubuntu bash
  • 다시 들어가기 위해 (attach)
    • ^P^Q를 누르면 'read escape sequence' 출려되며 host로 나올 수 있다.
    • 다시 attach한 뒤에 exit로 나오면 container가 종료되면서 rm된다.
$ docker attach ubuntu_bash
  • binding : mount
    • 1번 터미널 : container에 binding할 dir의 준비 및 실행
      • binding directory : $HOME/nginx_doc_root(host os) -> /usr/share/nginx/html (container directory)

-v host file:container file container 안에서&nbsp; container&nbsp; file을 읽어들이면 hostfile을 읽어들이는 것임.

  • 2번 터미널 : curl로 nginx에 접속

  • -v (--volume) 대신 --mount 사용
    • type=bind : mount의 bind 기능
    • type=volume : volume을 사용
    • docker에서는 -v 보다 --mount 사용 권장
$ docker run --rm -d -p 8080:80/tcp \
--mount type=bind,src=/home/mihee/nginx_doc_root,dst=/usr/share/nginx/html \
--name nginx_8080 nginx
  • binding : Env
    • environment variables
      • --env KEY=value
      • --env-file env_file

  • docker : stop, start
    • docker stop
      • detached mode로 실행중이면 외부에서 docker stop으로 정지
      • -it를 사용하지 않는 시스템은 signal 혹은 docker stop으로 정지
    • docker start
      • docker run --rm을 쓰지 않는 경우 exited시 container는 남는다
      • stop 후 재시작할 때 start로 가능

 

file copy ( local <-> container)

container -> local

$ docker cp dl_lecture:/mihee/CNN/test.py ~/home/

local -> container

$ docker cp ~/data/test.py dl_lecture:/mihee/CNN/

'Docker' 카테고리의 다른 글

docker 안에서 GUI 실행하기(ex.. plt.show())  (0) 2022.04.27
docker-compose  (0) 2022.04.21
Docker history, install  (0) 2022.04.18
Comments