관리 메뉴

피터의 개발이야기

[Docker] docker image 생성 및 배포, docker build and push 본문

DevOps/Docker

[Docker] docker image 생성 및 배포, docker build and push

기록하는 백앤드개발자 2022. 8. 7. 14:34
반응형

 

 ㅁ 개요

 ㅇ Dockerfile로 Image를 생성하고 Docker Hub에 배포하기

 

 

ㅁ Flask-hello 데모 app

 ㅇ run.py 작성

from flask import Flask
from flask import request, jsonify

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

if __name__=='__main__':
    app.run(host="0.0.0.0", port=8080)

 

ㅁ Dockerfile 작성

 ㅇ flask-hello 폴더를 생성한 뒤에,  아래 내용으로 Dockerfile 생성

FROM python:3.8-slim
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 8080
ENTRYPOINT ["python"]
CMD ["run.py"]

 

 

ㅁ Docker Image Build

 ㅇ flask-hello 폴더에 생성된 Dockerfile로 ilovefran/flask-hello:1.0 이란 image를 build한다.

 ㅇ ilovefran은 Dockerhub의 계정이다.

$ docker build -t ilovefran/flask-hello:1.0 -f Dockerfile .
[+] Building 3.3s (10/10) FINISHED
 => [internal] load build definition from Dockerfile
 => => transferring dockerfile: 37B
 => [internal] load .dockerignore
 => => transferring context: 2B
 => [internal] load metadata for docker.io/library/python:3.8-slim
 => [auth] library/python:pull token for registry-1.docker.io
 => [internal] load build context
 => => transferring context: 121B
 => [1/4] FROM docker.io/library/python:3.8-slim@sha256:275ac8020b9b2be9978aae095015d872207d2f40d3857f34b04fbea485a3e
 => => resolve docker.io/library/python:3.8-slim@sha256:275ac8020b9b2be9978aae095015d872207d2f40d3857f34b04fbea485a3e
 => CACHED [2/4] COPY . /app
 => CACHED [3/4] WORKDIR /app
 => CACHED [4/4] RUN pip install -r requirements.txt
 => exporting to image
 => => exporting layers
 => => writing image sha256:db8a654d80df8ae1af76eb19b01c4bb0cb70f5e512cfc4bc4a31104ebdc391b2
 => => naming to docker.io/ilovefran/flask-hello:1.0

 ㅇ Image Build 시 Local에 python:3.8-slim이 없기 때문에 Docker Hub에서 Pull을 받는다.

 ㅇ Dockerfile의 명령어에 따라 새로운 Layer가 생성되었고, ilovefranflask-hello:1.0 image가 build되었다.

 

 

 ㅇ docker images 명령어로 생성된 이미지를 확인 할 수 있다.

 

 

 

ㅁ Image를 Container로 Run

 ~/study/aws/EFK-minikube/flask-hello  docker run -dit -p 8080:8080 --name flask-hello  ilovefran/flask-hello:1.0
60dade4b8f0fe2c38ebc7d6830cb3ceabbcd9d762830c39e2e8c454f2536f31b

 ~/study/aws/EFK-minikube/flask-hello  docker logs flask-hello
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Serving Flask app 'run'
 * Debug mode: off
 * Running on all addresses (0.0.0.0)
   WARNING: This is a development server. Do not use it in a production deployment.
 * Running on http://127.0.0.1:8080
 * Running on http://172.17.0.2:8080 (Press CTRL+C to quit)

 

  ㅇ 접속 확인하여 Container가 정상확인하였다.

 

 

ㅁ Image를 Docker Hub로 Push하기

$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: ilovefran
Password:
Login Succeeded

 ㅇ Image를 push하기 위해 docker Hub에 접속을 한다.

 

 

$ docker push ilovefran/flask-hello:1.0
The push refers to repository [docker.io/ilovefran/flask-hello]
1b464482c54e: Pushed
71711ed585a0: Mounted from library/python
c3de06802875: Mounted from library/python
5d6969c21ec9: Mounted from library/python
e7a38ce55a0d: Mounted from library/python
92a4e8a3140f: Mounted from library/python
1.0: digest: sha256:60b98e22724436de5b2727dcf7d1d4e132317dfa6d1065fa396bd08bbb44a249 size: 1994

 ㅇ Python image는 이미 library로  존재하는 Layer이기 때문에 Mount만 되고, 생성된 Layer만 추가로 Push가 되었다.

 

 

 ㅇ Docker Hub에서 정상 배포된 것을 확인할 수 있다.

 

반응형
Comments