본문 바로가기
컨테이너/도커

03 Hello Docker 실행, 중지, 접속

by ^..^v 2019. 5. 18.
728x90
반응형

docker container run 명령

도커 컨테이너 실행

root@server:~/docker# docker container run -p 9000:8080 -d example/echo:latest ⇐ example/echo:latest 이미지:태그를 백그라운드로 실행. 호스트의 9000번 포트와 컨테이너의 8080 포트를 맵핑(포트 포워드)
ef470b8faf6a0c5576fe8cffd90fc5399aadc31036cecf7777f7e558e3fc4570 ⇐ 컨테이너 아이디

root@server:~/docker# curl http://localhost:9000 ⇐ 호스트에서 컨테이너의 서비스를 실행(접근)

root@server:~/docker# docker container ls
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                    NAMES
ef470b8faf6a        example/echo:latest   "go run /echo/main.go"   15 minutes ago      Up 15 minutes       0.0.0.0:9000->8080/tcp   eager_murdock ⇐ 이름을 지정하지 않을 경우 임의의 이름이 부여되는 것을 확인 (컨테이너 이름으로 실행된 컨테이너 확인이 가능)
db3164ff2997        example/echo:latest   "go run /echo/main.go"   25 minutes ago      Up 25 minutes                                gracious_bell

root@server:~/docker# docker container ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                    NAMES
ef470b8faf6a        example/echo:latest   "go run /echo/main.go"   15 minutes ago      Up 15 minutes       0.0.0.0:9000->8080/tcp   eager_murdock
db3164ff2997        example/echo:latest   "go run /echo/main.go"   25 minutes ago      Up 25 minutes                                gracious_bell

root@serdocker container run -p 8080 -d example/echo:latest ⇐ p 옵션의 값을 하나만 지정하는 경우 호스트의 포트가 임의로 배정됨 (아래에 32768번으로 배정된 것을 확인할 수 있음)
5edc889593a0502e928cad1802a81912779a28d04ed56f5241dd1b8e780c0851

root@server:~/docker# docker container ls
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                     NAMES
5edc889593a0        example/echo:latest   "go run /echo/main.go"   32 seconds ago      Up 32 seconds       0.0.0.0:32768->8080/tcp   sleepy_brahmagupta
ef470b8faf6a        example/echo:latest   "go run /echo/main.go"   19 minutes ago      Up 19 minutes       0.0.0.0:9000->8080/tcp    eager_murdock
db3164ff2997        example/echo:latest   "go run /echo/main.go"   28 minutes ago      Up 28 minutes                                 gracious_bell

root@server:~/docker# curl http://localhost:32768 ⇐ 호스트에서 임의로 배정된 포트로 접속을 확인
Hello Docker!!

docker attach 명령

실행 중인 컨테이너에 표준 입력(stdin)과 표준 출력(stdout)을 연결

root@server:~/docker# docker container run -it -d -p 8888:8080 example/echo:latest /bin/bash ⇐ 컨테이너 실행 후 bash 실행
ecdf91d52bc1861230715c217efbc8560cfe03d0ddde7358287c5891e129503f
root@server:~/docker# docker container ls
CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS              PORTS                    NAMES
ecdf91d52bc1        example/echo:latest   "/bin/bash"         2 minutes ago       Up 2 minutes        0.0.0.0:8888->8080/tcp   priceless_knuth

root@server:~/docker# docker attach echo ⇐ 실행 중인 컨테이너로 연결
root@ecdf91d52bc1:/go# ls     ---+ ⇐ 컨테이너 내부
bin  src                         |  
root@ecdf91d52bc1:/go# cd /      |
root@ecdf91d52bc1:/# cd echo     |
root@ecdf91d52bc1:/echo# ls      |
main.go                       ---+ 

docker container stop 명령

도커 컨테이너 중지

일괄 중지 스크립트 생성 과정 이해
*** STEP1. 컨테이너 리스트 출력
root@server:~/docker# docker container ls
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                     NAMES
5edc889593a0        example/echo:latest   "go run /echo/main.go"   4 minutes ago       Up 4 minutes        0.0.0.0:32768->8080/tcp   sleepy_brahmagupta
ef470b8faf6a        example/echo:latest   "go run /echo/main.go"   23 minutes ago      Up 22 minutes       0.0.0.0:9000->8080/tcp    eager_murdock
db3164ff2997        example/echo:latest   "go run /echo/main.go"   32 minutes ago      Up 32 minutes                                 gracious_bell

*** STEP2. 동일 이미지(example/echo)로 생성된 컨테이너 필터링
root@server:~/docker# docker container ls --filter "ancestor=example/echo" 
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                     NAMES
5edc889593a0        example/echo:latest   "go run /echo/main.go"   6 minutes ago       Up 6 minutes        0.0.0.0:32768->8080/tcp   sleepy_brahmagupta
ef470b8faf6a        example/echo:latest   "go run /echo/main.go"   24 minutes ago      Up 24 minutes       0.0.0.0:9000->8080/tcp    eager_murdock
db3164ff2997        example/echo:latest   "go run /echo/main.go"   34 minutes ago      Up 34 minutes                                 gracious_bell

*** STEP3. 동일 이미지로 생성된 컨테이너의 컨테이너 ID만 출력
root@server:~/docker# docker container ls --filter "ancestor=example/echo" -q
5edc889593a0
ef470b8faf6a
db3164ff2997

*** STEP4. 동일 이미지로 생성된 컨테이너만 중지
root@server:~/docker# docker container stop $(docker container ls --filter "ancestor=example/echo" -q)
5edc889593a0
ef470b8faf6a
db3164ff2997

root@server:~/docker# docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
실행중인 모든 컨테이너를 중지
root@server:~/docker# docker container run -it -d -p 8887:8080 example/echo:latest /bin/bash
08106472d15fe72437a5f05092b2a807b7ec450df973b976f0ee651d788b5f62
root@server:~/docker# docker container run -it -d -p 8888:8080 example/echo:latest /bin/bash
613e3f1588c8595f1d3a1320a6b96f40bce19564c808a15d24b8870118898b79
root@server:~/docker# docker container run -it -d -p 8889:8080 example/echo:latest /bin/bash
23ba3a114780ce529224aae6ce31d25b1067218d6b4cf931635de6d2c5adff3e
root@server:~/docker# docker container ls
CONTAINER ID        IMAGE                 COMMAND             CREATED              STATUS              PORTS                    NAMES
23ba3a114780        example/echo:latest   "/bin/bash"         9 seconds ago        Up 8 seconds        0.0.0.0:8889->8080/tcp   musing_lumiere
613e3f1588c8        example/echo:latest   "/bin/bash"         About a minute ago   Up About a minute   0.0.0.0:8888->8080/tcp   goofy_varahamihira
08106472d15f        example/echo:latest   "/bin/bash"         15 seconds ago       Up 14 seconds       0.0.0.0:8887->8080/tcp   fervent_snyder
root@server:~/docker# docker container stop $(docker container ls -q)
23ba3a114780
08106472d15f
613e3f1588c8
root@server:~/docker# docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
728x90
반응형

댓글