天天看點

Docker初體驗,建立并導出第一個本地鏡像

準備工作,建立一個目錄docker-test,用來存放建立鏡像所需的檔案,同僚完成相關檔案的建立。

[root@ChatDevOps ~]# mkdir docker-test
[root@ChatDevOps ~]# cd docker-test/
[root@ChatDevOps docker-test]# touch Dockerfile
[root@ChatDevOps docker-test]# touch app.py
[root@ChatDevOps docker-test]# touch 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
ADD . /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           

安裝pip,并使用pip安裝Flask和Redis:

[root@ChatDevOps docker-test]#  curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
[root@ChatDevOps docker-test]# python get-pip.py 
[root@ChatDevOps docker-test]# pip install -r requirements.txt
[root@ChatDevOps docker-test]# rm -rf get-pip.py 
[root@ChatDevOps docker-test]# ls
app.py  Dockerfile  requirements.txt           

執行建構指令,使用-t選項指定鏡像的倉庫、标簽。注意,鏡像名稱必須小寫。

[root@ChatDevOps docker-test]# docker build -t local/chatdevops .
Sending build context to Docker daemon 4.608 kB
Step 1/7 : FROM python:2.7-slim
Trying to pull repository docker.io/library/python ... 
2.7-slim: Pulling from docker.io/library/python
... ...
[root@ChatDevOps docker-test]# docker image ls 
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
local/chatdevops        latest              fc26e265213a        21 minutes ago      151 MB
docker.io/fedora        latest              cc510acfcd70        2 weeks ago         253 MB           

使用剛剛建立的鏡像建構一個容器,并将容器的80端口映射到本機的4000端口。

[root@ChatDevOps docker-test]# docker run -p 4000:80 local/chatdevops
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
172.17.0.1 - - [23/May/2018 04:21:04] "GET / HTTP/1.1" 200 -
172.17.0.1 - - [23/May/2018 04:21:04] "GET /favicon.ico HTTP/1.1" 404 -
172.17.0.1 - - [23/May/2018 04:22:24] "GET / HTTP/1.1" 200 -           

在浏覽器看一下效果:

Docker初體驗,建立并導出第一個本地鏡像

鏡像制作完成,導出鏡像,選項-o表示将導出内容寫入一個檔案,替代了标準輸出。

[root@ChatDevOps docker-test]# docker save -o chatdevops.tar local/chatdevops
[root@ChatDevOps docker-test]# ll
總用量 153716
-rw-r--r--. 1 root root       666 5月  23 09:52 app.py
-rw-------. 1 root root 157392896 5月  23 11:47 chatdevops.tar
-rw-r--r--. 1 root root       509 5月  23 11:12 Dockerfile
-rw-r--r--. 1 root root        12 5月  23 09:51 requirements.txt           

在異機上導入剛剛建立的鏡像。

[root@ChatDevOps docker-test]# scp chatdevops.tar [email protected]:/root/
[root@ChatDevOps ~]# ll
總用量 153704
-rw-------. 1 root root 157392896 5月  23 14:30 chatdevops.tar
[root@ChatDevOps ~]# docker load --input chatdevops.tar 
ba291263b085: Loading layer [==================================================>] 82.94 MB/82.94 MB
10dd6271862c: Loading layer [==================================================>] 7.487 MB/7.487 MB
4e1a46391216: Loading layer [==================================================>] 46.96 MB/46.96 MB
a40d037570f2: Loading layer [==================================================>] 7.649 MB/7.649 MB
d9bad830e350: Loading layer [==================================================>] 1.536 kB/1.536 kB
16b29278858d: Loading layer [==================================================>]  5.12 kB/5.12 kB
b6e1c4419841: Loading layer [==================================================>] 12.32 MB/12.32 MB
Loaded image: local/chatdevops:latest
[root@ChatDevOps ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
local/chatdevops    latest              dea564c3cb05        3 hours ago         151 MB           

本文是Docker官方文檔的執行個體的實踐與拓展,剛剛學習Docker,諸多問題還望大家海涵和指教。