클라우드 + DevOps/Docker

Docker 실습 :: 호스트네임 알려주는 컨테이너 생성

gamjadori 2024. 2. 10. 11:45
728x90

<호스트 네임 알려주는 컨테이너 생성>

  • 사용할 이미지: 자바를 일반 파이썬처럼 실행시켜주는 파일로, 호스트네임을 알려줌
  • runapp.js 파일 생성
const http = require('http');

const server = http.createServer().listen(6060);

server.on('request', (req, res) => {
console.log('Your request arrived.');
res.write("HostName: " + process.env.HOSTNAME + "\\n");
res.end();
});

server.on('connection', (socket) => {
console.log("Your Connected.");
});
  • Dockerfile 생성
FROM node:20-alpine3.17

RUN apk add --no-cache tini curl

WORKDIR /app

COPY runapp.js .

EXPOSE 6060

ENTRYPOINT ["/sbin/tini", "--"]

CMD ["node", "runapp.js"]

 

1. 도커 이미지 생성 및 확인

ubuntu@host1:~/Labs/ch03$ docker image build -t noderun:1.0 .
ubuntu@host1:~/Labs/ch03$ docker image ls
REPOSITORY                      TAG       IMAGE ID       CREATED          SIZE
**noderun                         1.0       d77d5ca0759f   10 seconds ago   138MB**
192.168.56.103:5000/phpserver   1.0       c99246de40dc   4 hours ago      410MB
phpserver                       1.0       c99246de40dc   4 hours ago      410MB
192.168.56.103:5000/myweb       1.0       6b60617e4b69   28 hours ago     12MB
192.168.56.103:5000/myweb       1.1       6b60617e4b69   28 hours ago     12MB
choisieun/myweb                 1.0       6b60617e4b69   28 hours ago     12MB
myweb                           1.0       6b60617e4b69   28 hours ago     12MB
alpine                          latest    f8c20f8bbcb6   5 weeks ago      7.38MB

 

2. 컨테이너 생성

ubuntu@host1:~/Labs/ch03$ docker container run -itd -p 6060:6060 --name=noderun -h noderun noderun:1.0
ubuntu@host1:~/Labs/ch03$ docker container ps
CONTAINER ID   IMAGE           COMMAND                   CREATED          STATUS          PORTS                                       NAMES
7ed07c8e4af0   noderun:1.0     "/sbin/tini -- node …"   7 seconds ago    Up 7 seconds    0.0.0.0:6060->6060/tcp, :::6060->6060/tcp   noderun
a40d682d4419   alpine          "sh"                      26 minutes ago   Up 26 minutes                                               mycontainer
af932ec01dc7   phpserver:1.0   "docker-php-entrypoi…"   4 hours ago      Up 4 hours      0.0.0.0:8004->80/tcp, :::8004->80/tcp       phpserver

<결과 확인>

 

1. curl 명령어 확인

ubuntu@host1:~/Labs/ch03$ curl localhost:6060
HostName: noderun

 

2. 브라우저로 웹 서버 접속