How to build docker image from dockerfile
Updated:Categories: Docker
Tags: #Docker #Dockerfile #Setup
OverviewPermalink
Docker는 Dockerfile로부터 명령어들을 읽어 image들을 자동적으로 빌드할 수 있다. Play framework v2.5.18 for scala 프로젝트의 배포된 bin파일이 JRE 8u151이 설치된 Oracle linux v7.2 docker image 환경에서 돌아가는지 확인하기 위하여 Dockerfile을 이용하여 docker image를 만들어봄.
PrerequisitesPermalink
이전 포스트 How to setup docker on macOS를 참고하여 docker-machine 내에서 작업.
ImplementationPermalink
docker로 테스트한 과정은 다음과 같다.
1. Build oracle linux 7.2 docker image by using DockerfilePermalink
docker-machine에 접속docker-machine 관련 명령어# macOS 내에서 실행되고 있는 docker Machine에 SSH 접속 $ docker-machine ssh dev
NOTES: Docker for Mac 업데이트로 인해 Docker machine에 따로 접속하지 않고 터미널에서 docker 명령어 실행 가능. (2018.08 확인)
- docker-file을 만들고 이미지 빌드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Dockerfile을 만들 directory 생성 | |
$ mkdir OracleLinux7.2 | |
# 생성한 directory 내, Dockefile 파일 생성 | |
$ cd OracleLinux7.2/ | |
$ vi Dockerfile | |
##### Example Dockerfile ##### | |
# Set up oracle linux 7.2 | |
# Pull base image | |
FROM oraclelinux:7.2 | |
##### | |
# Dockerfile을 이용한 image 빌드 | |
# docker build [-t{Image 명} [:{Tag 명}]]{Dockerfile이 존재하는 directory 아래에서는 dockerfile이 존재하는 diretory에서 실행하여 '.'임 } | |
$ docker build -t jb/oraclelinux:7.2 . | |
Sending build context to Docker daemon 2.048 kB | |
Step 1 : FROM oraclelinux:7.2 | |
... | |
# 빌드된 image 확인 | |
$ docker images | |
# 빌드된 image로 container run | |
$ docker run --rm -it jb/oraclelinux:7.2 /bin/bash |
- Dockerfile for oracle linux 7.2 with JRE 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Set up oracle linux 7.2 with JRE 8 | |
# Pull base image | |
FROM oraclelinux:7.2 | |
MAINTAINER JB <jungbin.kim@letsee.io> | |
ENV JRE_VERSION 8u151 | |
ENV JRE_BUILD_NUM b12 | |
ENV JRE_DOWNLOAD_HASH e758a0de34e24606bca991d704f6dcbf | |
RUN \ | |
echo "Update packages" && \ | |
yum update -y && \ | |
yum install -y wget && \ | |
echo "Install JRE" && \ | |
wget --quiet --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/$JRE_VERSION-$JRE_BUILD_NUM/$JRE_DOWNLOAD_HASH/jre-$JRE_VERSION-linux-x64.rpm" && \ | |
rpm -ivh jre-$JRE_VERSION-linux-x64.rpm && \ | |
java -version |
2. Mount host directory to docker containerPermalink
Host의 디렉토리를 docker container에 mount 해주어, 배포 파일을 docker container에서 접근 가능하도록 함. play server의 port를 docker container의 port와 연결해야 서비스가 작동하는지 확인할 수 있음.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -p docker-machine port matching | |
# -p {외부 포트}:{내부 포트} | |
# -v: mount host directory | |
# -v {host dir}:{container dir} | |
$ docker run --rm -it -p 9000:9000 -v ~/DockerMount:/mnt jb/oraclelinux:7.2 /bin/bash |
3. Deploy a projectPermalink
- Download test play2.5 project at https://www.playframework.com/download
- Apply native-packager
and distribute a project by using
sbt universal:packageZipTarball
4. Test deployed filePermalink
- Move a distributed project file to mounted directory and unzip
docker container 접속 > mount된 폴더 > 압축 해제한 폴더
에서 bin 파일 실행./bin/{project name}
Comments