docker build

1 개요[ | ]

docker build
도커 빌드

2 명령[ | ]

docker build [OPTIONS] PATH | URL | -

3 옵션[ | ]

4 실습 1[ | ]

$ ls
Dockerfile  index.html
$ cat Dockerfile
FROM nginx:alpine
COPY . /usr/share/nginx/html
$ cat index.html
<h1>Hello World</h1>
$ docker build -t webserver-image:v1 .
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM nginx:alpine
 ---> aae476eee77d
Step 2/2 : COPY . /usr/share/nginx/html
 ---> b31740ce0dcd
Successfully built b31740ce0dcd
Successfully tagged webserver-image:v1
$ docker images webserver-image
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
webserver-image     v1                  b31740ce0dcd        2 minutes ago       17.7MB
$ docker run -d -p 80:80 webserver-image:v1
2fd2f283639f8af9d3fc8b33d2cad8524bfb03dc66aae2dba05ece892532ccec

5 실습 2[ | ]

$ ls
Dockerfile  index.html
$ cat index.html
<h1>Hello World</h1>
$ cat Dockerfile
FROM nginx:1.11-alpine
COPY index.html /usr/share/nginx/html/index.html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              16508e5c265d        2 months ago        84.1MB
redis               latest              4e8db158f18d        2 months ago        83.4MB
$ docker build -t my-nginx-image:latest .
Sending build context to Docker daemon  3.072kB
Step 1/4 : FROM nginx:1.11-alpine
 ---> bedece1f06cc
Step 2/4 : COPY index.html /usr/share/nginx/html/index.html
 ---> 685f7c1e318f
Step 3/4 : EXPOSE 80
 ---> Running in 6534915eace1
Removing intermediate container 6534915eace1
 ---> 0256d516650d
Step 4/4 : CMD ["nginx", "-g", "daemon off;"]
 ---> Running in ae5f493c0c7b
Removing intermediate container ae5f493c0c7b
 ---> e0844e68be43
Successfully built e0844e68be43
Successfully tagged my-nginx-image:latest
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
my-nginx-image      latest              e0844e68be43        14 seconds ago      54.3MB
ubuntu              latest              16508e5c265d        2 months ago        84.1MB
redis               latest              4e8db158f18d        2 months ago        83.4MB

6 실습 3[ | ]

  • 1. 이미지 빌드에 사용할 파일 준비

본 예시는 참고 사이트와 동일하게 Dockerfile, app.py, requirements.txt 의 세가지 파일을 사용

Dockerfile
# Use an official Python runtime as a parent image
FROM python:2.7-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]
app.py
from flask import Flask
from redis import Redis, RedisError
import os
import socket

# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)

app = Flask(__name__)

@app.route("/")
def hello():
    try:
        visits = redis.incr("counter")
    except RedisError:
        visits = "<i>cannot connect to Redis, counter disabled</i>"

    html = "<h3>Hello {name}!</h3>" \
           "<b>Hostname:</b> {hostname}<br/>" \
           "<b>Visits:</b> {visits}"
    return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80)
requirements.txt
Flask
Redis
  • 2.빌드 실행

빌드 명령으로 빌드를 실행함

john$ docker build -t friendlyhello .
Sending build context to Docker daemon   5.12kB
Step 1/7 : FROM python:2.7-slim
2.7-slim: Pulling from library/python
802b00ed6f79: Pull complete
10b2d5f7ed73: Pull complete
1073a127cf89: Pull complete
90283f3dc1cd: Pull complete
Digest: sha256:0a43a6d7858af4a42427c792b682936d2cd34e183fb026627f53ddb556d4bf62
Status: Downloaded newer image for python:2.7-slim
 ---> c9cde4658340
Step 2/7 : WORKDIR /app
 ---> Running in 8cda2d6fd4df
Removing intermediate container 8cda2d6fd4df
 ---> d2a996cd9199
Step 3/7 : COPY . /app
 ---> fef28e391ee4
Step 4/7 : RUN pip install --trusted-host pypi.python.org -r requirements.txt
 ---> Running in 99cca69bce30
Collecting Flask (from -r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/7f/e7/08578774ed4536d3242b14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl (91kB)
Collecting Redis (from -r requirements.txt (line 2))
  Downloading https://files.pythonhosted.org/packages/3b/f6/7a76333cf0b9251ecf49efff635015171843d9b977e4ffcf59f9c4428052/redis-2.10.6-py2.py3-none-any.whl (64kB)
Collecting itsdangerous>=0.24 (from Flask->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/dc/b4/a60bcdba945c00f6d608d8975131ab3f25b22f2bcfe1dab221165194b2d4/itsdangerous-0.24.tar.gz (46kB)
Collecting Jinja2>=2.10 (from Flask->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl (126kB)
Collecting Werkzeug>=0.14 (from Flask->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/20/c4/12e3e56473e52375aa29c4764e70d1b8f3efa6682bef8d0aae04fe335243/Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)
Collecting click>=5.1 (from Flask->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10->Flask->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/4d/de/32d741db316d8fdb7680822dd37001ef7a448255de9699ab4bfcbdf4172b/MarkupSafe-1.0.tar.gz
Building wheels for collected packages: itsdangerous, MarkupSafe
  Running setup.py bdist_wheel for itsdangerous: started
  Running setup.py bdist_wheel for itsdangerous: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/2c/4a/61/5599631c1554768c6290b08c02c72d7317910374ca602ff1e5
  Running setup.py bdist_wheel for MarkupSafe: started
  Running setup.py bdist_wheel for MarkupSafe: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/33/56/20/ebe49a5c612fffe1c5a632146b16596f9e64676768661e4e46
Successfully built itsdangerous MarkupSafe
Installing collected packages: itsdangerous, MarkupSafe, Jinja2, Werkzeug, click, Flask, Redis
Successfully installed Flask-1.0.2 Jinja2-2.10 MarkupSafe-1.0 Redis-2.10.6 Werkzeug-0.14.1 click-7.0 itsdangerous-0.24
Removing intermediate container 99cca69bce30
 ---> 4b457c3dc813
Step 5/7 : EXPOSE 80
 ---> Running in 26ae3ed01b74
Removing intermediate container 26ae3ed01b74
 ---> 54747aa31e3a
Step 6/7 : ENV NAME World
 ---> Running in ebe99c1eb720
Removing intermediate container ebe99c1eb720
 ---> 3e0b3d88ebce
Step 7/7 : CMD ["python", "app.py"]
 ---> Running in 00ce5971ee2f
Removing intermediate container 00ce5971ee2f
 ---> 2b6f3399daad
Successfully built 2b6f3399daad
Successfully tagged friendlyhello:latest

7 같이 보기[ | ]

8 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}