Docker CLI(2)

Siwoo Pak·2021년 9월 17일
0

Docker

목록 보기
4/9

도커 컨테이너에 파일을 복사하기

  • 앞선 내용에선 사용할 모든 파일이 하나의 이미지에 구성되어 있고, 그 이미지를 사용하는 방법을 학습했다
  • 그러나 게임 서버, 웹서버와 같이 사용할 도구가 도커 이미지에 모두 구성되어 있지 않은 경우도 있다
    • 웹서버는 도커 컨테이너로 실행
    • 웹서버를 구성하는 파일은 직접 만들거나 가져온 파일 구성
    • 장점
      • 서버에 문제가 생기는 것은 호스트와 별개로 파악할 수 있음
      • 문제가 생긴 서버를 끄고, 도커 이미지로 서버를 재구동할 수 있음
  • 로컬에 있는 파일과 도커 이미지를 연결하는 방법
    • CP를 이용하는 방법: 호스트와 컨테이너 사이에 파일을 복사
    • Docker Volume기능을 이용하는 방법: 호스트와 컨테이너 사이에 공간을 마운트

to do

  • codestates/pacman-canvas 레포지토리 클론
$ git clone https://github.com/codestates/pacman-canvas 
  • docker container run 명령어로 httpd 실행
$ docker container run --name [container name] -p 818:80 httpd
  • docker desktop app의 경우 localhost:818이나 127.0.0.1:818로 브라우저 연결 확인 가능하나 맥OS 10.13버전 이하인 맥북에선 docker toolbox로 설치한 경우 virtual machine 위에서 컨테이너를 실행하기에 ip주소가 다름
$ docker-machine ip default// 해당 가상머신의 ip주소 나옴
192.168.99.100
  • 브라우저에서 해당 ip와 포트로 접속

  • 로컬에 있는 파일을 컨테이너에 복사(cp)
Last login: Sat Sep 18 16:47:12 on ttys000
$ docker cp ~/Documents/codestates/week15/pacman-canvas/ httpd:/usr/local/apache2/htdocs
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? // 새로운 터미널에서 가상 머신 선택안해서 발생한 오류
$ docker-machine env dev // dev 가상머신 쉘 생성
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/siwoo/.docker/machine/machines/dev"
export DOCKER_MACHINE_NAME="dev"
# Run this command to configure your shell: 
# eval $(docker-machine env dev)
$ eval $(docker-machine env dev) // 쉘 실행
$ docker cp ~/Documents/codestates/week15/pacman-canvas/ httpd:/usr/local/apache2/htdocs
Error: No such container:path: httpd:/usr/local/apache2 
// 컨테이너명 오입력
$ docker cp ~/Documents/codestates/week15/pacman-canvas/ httpdserver:/usr/local/apache2/htdocs 
//전체 경로명으로 입력했을시 적용이 안됨
$ docker container cp ./ httpdserver:/usr/local/apache2/htdocs
// 파일 경로로 이동 후 복사 시도 성공

  • httpdserver 컨테이너의 내부 터미널 접속
$ docker exec -it httpdserver bash
root@c14c5721068f:/usr/local/apache2# cd htdocs
root@c14c5721068f:/usr/local/apache2/htdocs# ls
Dockerfile		     img		pacman-canvas.js
README.md		     index.html		pacman-canvas.test.js
ads.txt			     js			pacman-canvas.webapp
bump_version.sh		     manifest.json	server.js
cache.manifest		     mp3		style.css
data			     package-lock.json	wav
fonts			     package.json	web-app-manifest.json
googlee6aee5a894225e60.html  pacman-canvas	webpack.config.js
httpd.conf		     pacman-canvas.css

Docker 이미지 만들기

  • 도커 컨테이너를 이미지파일로 변환했을 때 장점
    • 이전에 작업했던 내용을 다시 한번 수행하지 않아도 됨
    • 배포 및 관리가 유용
  • 구동한 도커 컨테이너 이미지로 만들기
    • docker container commit 명령어
      $ docker container commit httpdserver my_pacman:1.0    
      sha256:dbd547c679bc290e02b2b7c7677357729a853d38e1ca6fe1b302be7cbff3b4be
      $ docker images
      REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
      my_pacman           1.0                 dbd547c679bc        28 minutes ago      172MB
      httpd               latest              f34528d8e714        2 weeks ago         138MB
    • 생성된 이미지를 900포트에서 웹서버로 구동
      $ docker run --name my_web2 -p 900:80 my_pacman:1.0
      AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
      AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
      [Sat Sep 18 10:23:01.138645 2021] [mpm_event:notice] [pid 1:tid 140573721932928] AH00489: Apache/2.4.48 (Unix) configured -- resuming normal operations
      [Sat Sep 18 10:23:01.145361 2021] [core:notice] [pid 1:tid 140573721932928] AH00094: Command line: 'httpd -D FOREGROUND'
      192.168.99.1 - - [18/Sep/2021:10:23:10 +0000] "GET / HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:10 +0000] "GET /style.css HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:10 +0000] "GET /pacman-canvas.css HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:10 +0000] "GET /js/jquery.hammer.min.js HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:10 +0000] "GET /js/jquery-1.10.2.min.js HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:11 +0000] "GET /img/inky.svg HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:11 +0000] "GET /pacman-canvas.js HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:11 +0000] "GET /img/instructions/instructions_scatter.PNG HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:11 +0000] "GET /img/bg-pattern-black.png HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:11 +0000] "GET /img/audio-icon.png HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:11 +0000] "GET /img/clyde.svg HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:11 +0000] "GET /img/pinky.svg HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:11 +0000] "GET /img/blinky.svg HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:11 +0000] "GET /fonts/PressStart2Play.woff HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:12 +0000] "GET /img/instructions/instructions_chase.PNG HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:12 +0000] "GET /img/instructions/instructions_powerpill.PNG HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:12 +0000] "GET /img/audio-icon-mute.png HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:12 +0000] "GET /img/platzh1rsch-logo.png HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:12 +0000] "GET /wav/theme.wav HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:13 +0000] "GET /data/map.json HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:13 +0000] "GET /wav/waka.wav HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:13 +0000] "GET /wav/powerpill.wav HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:13 +0000] "GET /wav/die.wav HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:15 +0000] "GET /img/heart.png HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:15 +0000] "GET /img/dazzled.svg HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:15 +0000] "GET /img/dazzled2.svg HTTP/1.1" 304 -
      192.168.99.1 - - [18/Sep/2021:10:23:16 +0000] "GET /img/dead.svg HTTP/1.1" 304 -
  • 도커이미지 빌드를 위한 파일인 Dockerfile로 만드는 방법
  • 공식문서(https://docs.docker.com/engine/reference/builder/)
  • docker build 명령어 사용(Dockerfile로 이미지 파일 생성)
    $ docker build . --tag my_pacman:2.0
    #. 현재 경로의 있는 Dockerfile을 찾아서 빌드
    # --tag: name:tag 형식으로 이미지 생성
    Sending build context to Docker daemon  17.29MB
    Step 1/3 : FROM httpd:2.4 # 베이스 이미지를 httpd:2.4로 사용
    ---> f34528d8e714
    Step 2/3 : WORKDIR /usr/local/apache2
    ---> Running in 8d73821600bd
    Removing intermediate container 8d73821600bd
    ---> be18cfb9f7c4
    Step 3/3 : COPY ./ /usr/local/apache2/htdocs/ #호스트의 현재 경로에 있는 파일 해당경로에 복사
    ---> 48620c6c43b0
    Successfully built 48620c6c43b0
    Successfully tagged my_pacman:2.0
    (base) bagsiuui-MacBook-Pro:pacman-canvas siwoo$ docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    my_pacman           2.0                 48620c6c43b0        About an hour ago   155MB
    my_pacman           1.0                 dbd547c679bc        3 hours ago         172MB
    httpd               2.4                 f34528d8e714        2 weeks ago         138MB
    httpd               latest              f34528d8e714        2 weeks ago         138MB
    # 이미지에 my_pacman:2.0이 생성된 걸 확인
  • 생성된 이미지를 이용해 901포트에 웹서버 구동
    $ docker run --name my_web3 -p 901:80 my_packman:2.0
    AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
    AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
    [Sat Sep 18 11:58:24.634424 2021] [mpm_event:notice] [pid 1:tid 140486022722688] AH00489: Apache/2.4.48 (Unix) configured -- resuming normal operations
    [Sat Sep 18 11:58:24.654023 2021] [core:notice] [pid 1:tid 140486022722688] AH00094: Command line: 'httpd -D FOREGROUND'
    # 터미널 상황
    192.168.99.1 - - [18/Sep/2021:11:58:49 +0000] "GET / HTTP/1.1" 200 10268
    192.168.99.1 - - [18/Sep/2021:11:58:50 +0000] "GET /style.css HTTP/1.1" 200 9997
    192.168.99.1 - - [18/Sep/2021:11:58:50 +0000] "GET /pacman-canvas.css HTTP/1.1" 200 4594
    192.168.99.1 - - [18/Sep/2021:11:58:50 +0000] "GET /js/jquery.hammer.min.js HTTP/1.1" 200 13005
    192.168.99.1 - - [18/Sep/2021:11:58:50 +0000] "GET /js/jquery-1.10.2.min.js HTTP/1.1" 200 93107
    192.168.99.1 - - [18/Sep/2021:11:58:51 +0000] "GET /pacman-canvas.js HTTP/1.1" 200 46430
    192.168.99.1 - - [18/Sep/2021:11:58:51 +0000] "GET /img/inky.svg HTTP/1.1" 200 5845
    192.168.99.1 - - [18/Sep/2021:11:58:51 +0000] "GET /img/pinky.svg HTTP/1.1" 200 5852
    192.168.99.1 - - [18/Sep/2021:11:58:51 +0000] "GET /img/blinky.svg HTTP/1.1" 200 5862
    192.168.99.1 - - [18/Sep/2021:11:58:51 +0000] "GET /img/clyde.svg HTTP/1.1" 200 5850
    192.168.99.1 - - [18/Sep/2021:11:58:51 +0000] "GET /img/instructions/instructions_scatter.PNG HTTP/1.1" 200 14970
    192.168.99.1 - - [18/Sep/2021:11:58:51 +0000] "GET /img/audio-icon.png HTTP/1.1" 200 917
    192.168.99.1 - - [18/Sep/2021:11:58:51 +0000] "GET /fonts/PressStart2Play.woff HTTP/1.1" 200 6688
    192.168.99.1 - - [18/Sep/2021:11:58:51 +0000] "GET /img/bg-pattern-black.png HTTP/1.1" 200 24091
    192.168.99.1 - - [18/Sep/2021:11:58:51 +0000] "GET /img/instructions/instructions_chase.PNG HTTP/1.1" 200 16104
    192.168.99.1 - - [18/Sep/2021:11:58:51 +0000] "GET /img/instructions/instructions_powerpill.PNG HTTP/1.1" 200 15557
    192.168.99.1 - - [18/Sep/2021:11:58:51 +0000] "GET /img/platzh1rsch-logo.png HTTP/1.1" 200 1325
    192.168.99.1 - - [18/Sep/2021:11:58:51 +0000] "GET /data/map.json HTTP/1.1" 200 10874
    192.168.99.1 - - [18/Sep/2021:11:58:51 +0000] "GET /img/audio-icon-mute.png HTTP/1.1" 200 1201
    192.168.99.1 - - [18/Sep/2021:11:58:51 +0000] "GET /wav/theme.wav HTTP/1.1" 206 382510
    192.168.99.1 - - [18/Sep/2021:11:58:51 +0000] "GET /wav/waka.wav HTTP/1.1" 206 34606
    192.168.99.1 - - [18/Sep/2021:11:58:52 +0000] "GET /wav/die.wav HTTP/1.1" 206 281134
    192.168.99.1 - - [18/Sep/2021:11:58:52 +0000] "GET /wav/powerpill.wav HTTP/1.1" 206 110638
    192.168.99.1 - - [18/Sep/2021:11:58:52 +0000] "GET /img/heart.png HTTP/1.1" 200 181
    192.168.99.1 - - [18/Sep/2021:11:58:52 +0000] "GET /img/dazzled2.svg HTTP/1.1" 200 6588
    192.168.99.1 - - [18/Sep/2021:11:58:52 +0000] "GET /img/dazzled.svg HTTP/1.1" 200 6586
    192.168.99.1 - - [18/Sep/2021:11:58:53 +0000] "GET /img/dead.svg HTTP/1.1" 200 3381
    192.168.99.1 - - [18/Sep/2021:11:59:02 +0000] "GET /img/Icon-130x130.png HTTP/1.1" 200 4545
profile
'하루를 참고 인내하면 열흘을 벌 수 있고 사흘을 참고 견디면 30일을, 30일을 견디면 3년을 벌 수 있다.'

0개의 댓글