Docker

[Docker] Dockerfile로 이미지 생성하기 (3)

달의요정루나 2022. 1. 31. 23:29

이 게시물은 이전 게시물과 비슷합니다.

https://julian5383.tistory.com/50

 

[Docker] Dockerfile로 이미지 생성하기

1. 메모장을 열고 다음 내용을 담은 Dockerfile파일을 만든다. 2. Dockerfile과 비슷한 방식으로 default.conf와 index.html을 만든다. 3. Thymeleaf에 해당경로로 위에 만든 파일을 옮긴다. 필자는 이전에 만든..

julian5383.tistory.com

1. c드라이브 Temp에 nginx_docker 폴더를 만든다.

2. 다음 처럼 폴더와 파일을 만든다.

┬ Dockerfile

└ nginx ┬ default.conf

            └ html ─ index.html

default.conf

server {
	listen 80;
	server_name localhost;

	location / {
		root /usr/share/nginx/html;
		index index.html index.htm;
	}
}

index.html

<!DOCTYPE html>
<html>
	<head>
		<title>Hello git-sync demo</title>
	</head>
	<body>
		<h1>Hello git-sync demo v1.0</h1>
	</body>
</html>

3. Dockerfile을 만든다.

FROM nginx:1.21
COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf
COPY ./nginx/html/index.html /usr/share/nginx/html/index.html
RUN echo "<html><body><h1>Hello Docker!!!</h1></body></html>" > /usr/share/nginx/html/index.html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

4. 명령 프롬프트를 관리자 권한으로 실행한다.

5. cd 명령어를 이용해 nginx_docker 폴더로 이동한다.

6. docker image build -t my-nginx . 구문으로 my-nginx라는 이미지를 빌드한다.

- 그렇게 되면 도커에 my-nginx 이미지가 이렇게 나타날 것이다.

7. docker run -it --name test-nginx -p 80:80 my-nginx:latest 구문으로 Dockerfile로 생성된 이미지로 컨테이너를 생성한다.

혹은 도커 내에서 이미지를 직접 실행시켜서 컨테이너를 생성할 수 있다.

8. 그렇게 되면 컨테이너에 7. 언급한 구문 그대로 test-nginx라는 컨테이너가 만들어진다. 초록색으로 되어 있으면 실행되어 있는 것이고 나중에 도커에 들어가서 직접 재생버튼으로 실행시킬수 있다.

9. 컨테이너에서 test-nginx가 초록색 빛을 띄고 있으면 실행이 되고 있는 것이다. 브라우저에 접속해서 주소창에 localhost라고 치면 된다.