반응형
반응형
Dockerfile을 생성하여 자신만의 고유한 docker image를 생성해 보자
Dockerfile 생성 위치
[root@linux-1 docker_images]# pwd
/usr/local/docker_images
Dockerfile을 생성.
FROM : 원본 이미지 이름
MAINTAINER : 작성자 이름
RUN : Linux command
EXPOSE : 호스트와 연결할 포트 번호
CMD : container가 시작되었을 때 실행할 실행 파일 또는 Shell script
[root@linux-1 docker_images]# vi Dockerfile
###
FROM centos:7
MAINTAINER hoggil Dong <gdhon@naver.com>
RUN yum -y update && \
yum -y install httpd && \
yum clean all
EXPOSE 80
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
Docker 이미지 생성.
Dockerfile을 기반으로 이미지를 생성합니다.
이미지 이름 뒷부분에 태그 번호를 붙여줍니다.
끝에 점을 찍어 주는것도 잊지 마세요.
(현재 경로에 Dockerfile 있다는 의미입니다)
[root@linux-1 docker_images]# docker build -t centos_httpd:0.1 .
Sending build context to Docker daemon 2.048kB
Step 1/5 : FROM centos:7
7: Pulling from library/centos
524b0c1e57f8: Pull complete
Digest: sha256:e9ce0b76f29f942502facd849f3e468232492b259b9d9f076f71b392293f1582
Status: Downloaded newer image for centos:7
---> b5b4d78bc90c
Step 2/5 : MAINTAINER hoggil Dong <gdhon@naver.com>
---> Running in feebd12bf15a
Removing intermediate container feebd12bf15a
---> 2d1b19dfe358
Step 3/5 : RUN yum -y update && yum -y install httpd && yum clean all
---> Running in 4b878a5e221c
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirror.kakao.com
* extras: mirror.kakao.com
* updates: mirror.kakao.com
Resolving Dependencies
--> Running transaction check
---> Package bind-license.noarch 32:9.11.4-16.P2.el7_8.2 will be updated
... 생략 ...
---> Package yum-utils.noarch 0:1.1.31-54.el7_8 will be an update
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository
Size
================================================================================
Updating:
bind-license noarch 32:9.11.4-16.P2.el7_8.3 updates 89 k
binutils x86_64 2.27-43.base.el7_8.1 updates 5.9 M
device-mapper x86_64 7:1.02.164-7.el7_8.2 updates 295 k
device-mapper-libs x86_64 7:1.02.164-7.el7_8.2 updates 324 k
systemd x86_64 219-73.el7_8.6 updates 5.1 M
systemd-libs x86_64 219-73.el7_8.6 updates 416 k
yum-plugin-fastestmirror noarch 1.1.31-54.el7_8 updates 34 k
yum-plugin-ovl noarch 1.1.31-54.el7_8 updates 28 k
yum-utils noarch 1.1.31-54.el7_8 updates 122 k
Transaction Summary
================================================================================
Upgrade 9 Packages
Total download size: 12 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
warning: /var/cache/yum/x86_64/7/updates/packages/bind-license-9.11.4-16.P2.el7_8.3.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for bind-license-9.11.4-16.P2.el7_8.3.noarch.rpm is not installed
--------------------------------------------------------------------------------
Total 11 MB/s | 12 MB 00:01
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
Package : centos-release-7-8.2003.0.el7.centos.x86_64 (@CentOS)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Updating : systemd-libs-219-73.el7_8.6.x86_64 1/18
... 생략 ...
Verifying : 7:device-mapper-1.02.164-7.el7_8.1.x86_64 18/18
Updated:
bind-license.noarch 32:9.11.4-16.P2.el7_8.3
binutils.x86_64 0:2.27-43.base.el7_8.1
device-mapper.x86_64 7:1.02.164-7.el7_8.2
device-mapper-libs.x86_64 7:1.02.164-7.el7_8.2
systemd.x86_64 0:219-73.el7_8.6
systemd-libs.x86_64 0:219-73.el7_8.6
yum-plugin-fastestmirror.noarch 0:1.1.31-54.el7_8
yum-plugin-ovl.noarch 0:1.1.31-54.el7_8
yum-utils.noarch 0:1.1.31-54.el7_8
Complete!
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
* base: mirror.kakao.com
* extras: mirror.kakao.com
* updates: mirror.kakao.com
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-93.el7.centos will be installed
... 생략 ...
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
httpd x86_64 2.4.6-93.el7.centos base 2.7 M
Installing for dependencies:
apr x86_64 1.4.8-5.el7 base 103 k
apr-util x86_64 1.5.2-6.el7 base 92 k
centos-logos noarch 70.0.6-3.el7.centos base 21 M
httpd-tools x86_64 2.4.6-93.el7.centos base 92 k
mailcap noarch 2.1.41-2.el7 base 31 k
Transaction Summary
================================================================================
Install 1 Package (+5 Dependent packages)
Total download size: 24 M
Installed size: 32 M
Downloading packages:
--------------------------------------------------------------------------------
Total 22 MB/s | 24 MB 00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : apr-1.4.8-5.el7.x86_64 1/6
... 생략 ...
Verifying : centos-logos-70.0.6-3.el7.centos.noarch 6/6
Installed:
httpd.x86_64 0:2.4.6-93.el7.centos
Dependency Installed:
apr.x86_64 0:1.4.8-5.el7
apr-util.x86_64 0:1.5.2-6.el7
centos-logos.noarch 0:70.0.6-3.el7.centos
httpd-tools.x86_64 0:2.4.6-93.el7.centos
mailcap.noarch 0:2.1.41-2.el7
Complete!
Loaded plugins: fastestmirror, ovl
Cleaning repos: base extras updates
Cleaning up list of fastest mirrors
Removing intermediate container 4b878a5e221c
---> bce41f4fdee9
Step 4/5 : EXPOSE 80
---> Running in 546196bf07a0
Removing intermediate container 546196bf07a0
---> 948ee5437b87
Step 5/5 : CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
---> Running in 62dc1a75acf4
Removing intermediate container 62dc1a75acf4
---> 9dbff881038f
Successfully built 9dbff881038f
Successfully tagged centos_httpd:0.1
이미지 생성 확인
[root@linux-1 docker_images]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_httpd 0.1 9dbff881038f About a minute ago 308MB
컨테이너 생성
그럼, 이제 생성한 이미지로 컨테이너를 생성해보겠습니다
[root@linux-1 ~]# docker run -d --name httpd_1 -p 8080:80 centos_httpd:0.1
f414691b56437ca0076d2663f81fd2e23b17b2910a2d72259551423e2644fbdf
httd_1 : 컨테이너 이름
-d : 백그라운드로 실행합니다.
-p 8080:80 : 포트 포워딩 설정
-> 컨테이너는 외부에 포트가 노출이 안되기 때문에
외부 요청에 대해서는 호스트에서 컨테이너로 포트 포워딩을 해줘야합니다.
즉, 호스트의 8080 포트로 들어온 요청을 컨테이너의 80 포트로 포트 포워딩하는 설정입니다.
컨테이너 생성 확인!!!
"PORTS" 부분에서 포트 포워딩 설정을 확인할 수 있습니다.
[root@linux-1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f414691b5643 centos_httpd:0.1 "/usr/sbin/httpd -D …" 3 seconds ago Up 2 seconds 0.0.0.0:8080->80/tcp httpd_1
웹서버 접근 테스트
"192.168.56.101:8080" 호스트 ip 8080 포트로 접근하면 정상적으로 페이지가 뜨네요
반응형
'IT > Docker & Kubernetes' 카테고리의 다른 글
[k8s] kubernetes(쿠버네티스) cluster에 nginx 설치하기 (0) | 2020.07.10 |
---|---|
[k8s] kubespray로 kubernetes(쿠버네티스) 설치하기 (2) | 2020.07.01 |
[Docker] 도커 학습 - 3. 이미지 & 컨테이너 삭제 명령어 (0) | 2020.05.12 |
[Docker] 도커 학습 - 2. 이미지 다운로드 및 컨테이너 실행 (1) | 2020.05.09 |
[Docker] 도커 학습 - 1. 설치 및 정보 확인 (0) | 2020.05.09 |