본문 바로가기
IT/Docker

Docker Container tutorial (도커 컨테이너 매뉴얼) 2-1.명령어 (Container)

by halizy 2018. 7. 10.

이번 포스팅은 기본적으로 docker 운영에 있어서 사용되는 명령어에 대해 다뤄보았습니다. 

문의 사항은 댓글로 남겨주시면 성심성의껏 답변드리도록 하겠습니다. 


Docker command (Container)






 컨테이너 생성 (run)


$ docker run -i -t --name hello ubuntu /bin/bash 

   // # -i : interactive, -t : Pseudo-tty => 마지막줄의 /bin/bash에 입출력할수있게끔 한다

      # --name hello : 컨테이너의 이름 (뒤에 나오는 docker ps  NAME에 해당하는 값)# ubuntu : 실행시킬 이미지이름

      # /bin/bash : 이미지안의 /bin/bash를 실행함

      # 더 자세한 내용은 $ man docker-run


    root@c55525a94b7a:/# lsb_release -a 

 

    No LSB modules are available.

 

    Distributor ID: Ubuntu

 

    Description:  Ubuntu 14.04.3 LTS

 

    Release: 14.04

 

    Codename:  trusty




컨테이너 목록 확인 (ps)


컨테이너는 아래처럼 확인 가능 


docker ps // 실행중인 컨테이너 확인


CONTAINER ID    IMAGE    COMMAND    CREATED     STATUS    PORTS     NAMES


c55525a94b7a     ubuntu    "/bin/bash"   16 minutes ago   Up 9 seconds             hello

docker ps -a // 모든 컨테이너 확인


CONTAINER ID    IMAGE    COMMAND    CREATED     STATUS    PORTS     NAMES


c55525a94b7a     ubuntu    "/bin/bash"   16 minutes ago   Up 9 seconds             hello

f9036dbb42fe    ubuntu:12.04    "/bin/bash"   10 minutes ago      Exited (0) 58 seconds ago         hello1204





컨테이너 시작 (start)


# docker start '컨테이너명'


$ docker start hello1204 // hello1204 컨테이너 실행


 hello1204


docker ps // 실행중인 컨테이너 확인


CONTAINER ID    IMAGE    COMMAND    CREATED     STATUS    PORTS     NAMES


f9036dbb42fe    ubuntu:12.04    "/bin/bash"   18 minutes ago       Up 8 seconds                 hello1204

c55525a94b7a     ubuntu    "/bin/bash"   16 minutes ago   Up 9 seconds              hello





컨테이너 재 시작 (restart)


# docker restart '컨테이너명'


docker restart hello1204 // hello1204 컨테이너 실행


 hello1204


docker ps // 실행중인 컨테이너 확인


CONTAINER ID    IMAGE    COMMAND    CREATED     STATUS    PORTS     NAMES


f9036dbb42fe    ubuntu:12.04    "/bin/bash"   18 minutes ago       Up 2 seconds                 hello1204

c55525a94b7a     ubuntu    "/bin/bash"   16 minutes ago   Up 9 seconds              hello




컨테이너에 접속 (attach)


# attach docker run으로 만들어진 컨테이너에 다시 접속하기위한 명령


# docker attach '컨테이너명'


$ docker attach hello  // hello 컨테이너에 접속


  root@c55525a94b7a:/#


  root@c55525a94b7a:/# ls


  bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var


  root@c55525a94b7a:/# exit


$ docker ps // 실행중인 컨테이너 확인


CONTAINER ID    IMAGE    COMMAND    CREATED     STATUS    PORTS     NAMES


c55525a94b7a     ubuntu    "/bin/bash"   16 minutes ago   Up 9 seconds              hello





외부에서 컨테이너 안의 명령 실행 (exec)


# hello 컨테이너에 접속해서 echo "Hello World" 출력하기


# docker exec 컨테이너명 커맨드 인자


$ docker exec hello echo "Hello World!"



  Hello World!




컨테이너 정지 (stop)


실행중인 컨테이너 정지


# docker stop 컨테이너명


docker ps // 실행중인 컨테이너 확인


CONTAINER ID    IMAGE    COMMAND    CREATED     STATUS    PORTS     NAMES


c55525a94b7a     ubuntu    "/bin/bash"   16 minutes ago   Up 7 seconds             hello


docker stop hello // 실행중인 컨테이너 정지


hello


docker ps // 실행중인 컨테이너 확인


CONTAINER ID    IMAGE    COMMAND    CREATED     STATUS    PORTS     NAMES




컨테이너 삭제(rm)


# docker rm '컨테이너명' // ps, ps -a로 확인 할 수 있는 컨테이너 삭제 (이미지 삭제는 아님)


# docker rm -f '컨테이너명' (실행중인컨테이너의 강제삭제)


docker ps -a // 모든 컨테이너 확인


CONTAINER ID    IMAGE    COMMAND    CREATED     STATUS    PORTS     NAMES


c55525a94b7a     ubuntu    "/bin/bash"   16 minutes ago   Up 9 seconds             hello

f9036dbb42fe    ubuntu:12.04    "/bin/bash"   10 minutes ago      Exited (0) 58 seconds ago         hello1204


$ docker rm hello1204 // hello 컨테이너 삭제


hello1204


$ docker rm hello // hello 컨테이너 삭제


Error response from daemon: Cannot destroy container hello: Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f


Error: failed to remove containers: [hello]


docker rm -f hello // hello 컨테이너 강제 삭제


hello1204



docker ps // 실행중인 컨테이너 확인


CONTAINER ID    IMAGE    COMMAND    CREATED     STATUS    PORTS     NAMES



댓글