Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags
more
Archives
Today
Total
관리 메뉴

클라우드 엔지니어 꿈나무

따배도 도커 컨테이너 만들기 - 실습 본문

container

따배도 도커 컨테이너 만들기 - 실습

새싹싹이 2023. 10. 17. 23:55

1. nodejs 어플리케이션 컨테이너 만들기 : hellojs

2. 우분투 기반의 웹 서버 컨테이너 만들기

3. 만들어 놓은 컨테이너 배포하기 

 

1. nodejs 어플리케이션 컨테이너 만들기 : hellojs

$ vi hello.js

const http = require('http'); #http 서비스 지원
const os = require('os'); #os 서비스 지원
console.log("Test server starting..."); #hello js 실행될 떄 로그메세지 출력값 

var handler = function(request, response) {
    console.log("Received request from " + request.connection.remoteAddress);
    response.writeHead(200);
    response.end("Container Hostname: " + os.hostname() + "\n");
};
#똑똑하면 request, response 를 핸들링
#그러면 콘솔 메세지 출력 + 200번 상태코드 (정삭적인 상태입니다) + container hostname과 os hostname 출력


var www = http.createServer(handler); 
www.listen(8080); #8080 리슨 => 똑똑
#client connection 을 위에 두 줄에서 대기

hello.js 소스코드를 해석해서 실행해주는 운영환경이 필요

 

$ vi Dockerfile => 꼭 Dockerfile이라고 만들어줘야 이미지 생성 가능

FROM node:12 
COPY hello.js /
CMD ["node","/hello.js"]

$ docker build -t hellojs:latest . => . 현재 디렉토리에 있는 파일을 기준으로 작업하고 컨테이너 이미지 이름 및 태그는 hellojs를 만들어라 (hello.js가 현재 디렉토리에 존재)

$ docker images

+컨테이너 생성

$ docker run -d -p 8080:8080 --name web hellojs

$ docker ps

$ curl localhost:8080

컨테이너 이름 출력됨

 

 

2. 우분투 기반의 웹 서버 컨테이너 만들기

$ vim Dockerfile

FROM ubuntu
LABEL maintainer="KANG"
# install apache
RUN apt-get update \
        && apt-get install -y apache2
# && 앞에 것이 실행되면 뒤에 거 실행해! \으로 가독성 높임
RUN echo "TEST WEB" > /var/www/html/index.html
EXPOSE 80
CMD ["/usr/sbin/apache2ctl", "-DFOREGROUND"]
#레이어 6개

$ docker build -t webserver:v1 .

$ docker image ls

$ docker run -d -p 80:80 --name web webserver:v1 (latest는 생략 가능하지만 v1은 생략하면 안됨)

$ docker ps

$ curl localhost:80

 

 

3. 만들어 놓은 컨테이너 이미지 배포하기 

$ docker tag webserver:v1 hiheey/webserver:v1 => 태그 이름에 내 아이디가 앞에 와야 업로드 가능 

$ docker ps

동일한 아이디에 각각 두 개의 repository가 있는 것을 볼 수 있음

$ docker login

$ docker push hiheey/webserver:v1

$ docker push hiheey/hellojs

 

문제 : 주어진 script를 실행하는 컨테이너를 빌드하기

컨테이너 이름 : fortune:20.02

dockerfile의 내용

base image : debian

컨터이너에 아래의 webpage.sh 파일을 복사

#!/bin/bash
mkdir /htdocs
while:
do
	/usr/game/fortune > htdcos/index.html
    sleep 10
done

컨테이너에 fortune 어플리케이션 설치 : apt-get install fortune

컨테이너 실행 시, 저장한 webpage.sh가 실행되도록 구성

 

# vi webpage.sh

위의 코드 블럭 입력

# vim Dockerfile

FROM debian
RUN apt-get update \
        && apt-get install -y fortune
COPY webpage.sh / #copy 되는 경로 꼭 적어주기
CMD ["/webpage.sh"]

# docker build -t fortune:v1 .

#  docker images 

 

 

 

<<참고>>

https://youtu.be/WLjfzwdASbw?si=kv42Mf3ZvIJ086Ig