본문 바로가기
Docker

도커(Docker) 기본 실습 nginx 이미지에 html 생성 후 컨테이너 실행

by 바닐라이코노미 2025. 2. 3.
반응형

 

도커 기본 실습 내용 정리

인프런 강의(초보를 위한 도커 안내서, subicura)를 들으면서 개인실습으로 나온 부분을 따라 하며 정리한 내용을 작성

실습정보
 - 이미지: nginx:latest
 - 포트: 80
 - HTML 경로: /usr/share/nginx/html

문제 > 다음조건을 만족하는 컨테이너를 실행해주세요.
nginx 컨테이너를 50000 포트로 연결하여 실행
임의의 index.html 파일을 만들고 이 파일 내용을 제공하는 nginx 컨테이너 실행 (docker run -v 옵션 활용)

 

간단해 보이는 실습이었는데 명령어가 익숙지 않아 헤매다 실패의 과정을 정리

 

1. 내 로컬 PC C:/workspace/index.html 파일을 생성 후 hello world를 작성 후 저장

2. 도커 명령어를 통해 nginx컨테이너 실행하여 index.html 파일을 실행

docker run -d -p 50000:80 -v ~/index.html:/usr/share/nginx/html/index.html nginx:latest

-d: 컨테이너를 백그라운드에서 실행.
-p 50000:80: 호스트의 50000 포트를 컨테이너의 80 포트에 매핑.
-v ~/index.html:/usr/share/nginx/html/index.html: 생성한 index.html 파일을 NGINX의 기본 HTML 경로에 마운트.
nginx:latest: 최신 버전의 NGINX 이미지 사용.
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
af302e5c37e9: Download complete
207b812743af: Download complete
4de87b37f4ad: Download complete
841e383b441e: Download complete
38e992d287c5: Download complete
9e9aab598f58: Download complete
0256c04a8d84: Download complete
Digest: sha256:0a399eb16751829e1af26fea27b20c3ec28d7ab1fb72182879dcae1cca21206a
Status: Downloaded newer image for nginx:latest
9a35308d13c5e82ce794d622334b535791e486dcf23dcb0bfa62f4216ada8604
docker: Error response from daemon: driver failed programming external connectivity on endpoint agitated_leavitt (2b81af0fcd99ad5474b143ed799ac7f2b79360bad9375166fe9210d70d4d78dc): Bind for 0.0.0.0:50000 failed: port is already allocated.

 

→ 로컬에서 nginx 최신 이미지를 찾을 수 없어 pull로 가져와서 다운로드하고, 끝에 보면 0.0.0.0:50000 failed: port is already allocated 50000 포트가 이미 할당된 것을 볼 수가 있다

 

3. 포트를 50001로 수정해서 재 시도

docker run -d -p 50001:80 -v ~/index.html:/usr/share/nginx/html/index.html nginx:latest
b568eeab98a698c6afd22e0cdded490ad32ad321a7ae7b9382fe758b57f0faec
docker: Error response from daemon: 
failed to create task for container: 
failed to create shim task: OCI runtime create 
failed: runc create 
failed: unable to start container process: error during container init: error mounting "/index.html" to rootfs at "/usr/share/nginx/html/index.html": mount /index.html:/usr/share/nginx/html/index.html (via /proc/self/fd/6), flags: 0x5000: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.

 

 → 로컬 설정된 index.html을 바라보는 경로가 잘못되었음 확인하고 해당 부분을 절대경로로 수정하여 변경

 

4. index.html 경로 수정 후 재시도

#절대 경로로 수정
docker run -d -p 50001:80 -v C:/workspace/index.html:/usr/share/nginx/html/index.html nginx:latest

#Git Bash 또는 WSL 사용
docker run -d -p 50001:80 -v /c/workspace/index.html:/usr/share/nginx/html/index.html nginx:latest

 

→ 나의 경우는 Windows 환경에서 WSL로 진행하는 case였으므로 /c/workspace/index.html 형태로 수정해서 진행

 

5. 수정 후 정상적으로 결과 확인

docker run -d -p 50001:80 -v /c/workspace/index.html:/usr/share/nginx/html/index.html nginx:latest
c3d93a2c0a36461661199aa7e983db696e288a6c33f2f3c71277264ba78655a9

 

<localhost:50001 실행>

 

6. 컨테이너를 중단했을 경우

#container 아이디로 중단
docker stop c3d93a2c0a36

 

아직 도커 개념이 잘 잡히지 않은 상태라 용어의 혼선이 있을 수 있습니다

공부 차원의 정리로 일단은 봐주시면 좋을 거 같습니다 :)

반응형