Docker 學習筆記(2)極簡入門

創建鏡像
基於已有鏡像的容器創建
基於本地模板的導入
基於Dockerfile 創建

Dockerfile 是一個文本文件,利用給定的指令,基於父鏡像創建新的鏡像。

1.創建新文件夾及文件

mkdir py2.7Docker                              //創建文件夾

ubuntu@VM-0-13-ubuntu:~$ cd py2.7Docker
ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ ls
ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ touch Dockerfile         //創建文件
ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ ls
Dockerfile
ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ vim Dockerfile
# 編輯Dockerfile

cat 查看編輯內容:

ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ cat 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"]

同樣的方法,創建 requirements.txt and app.py
requirements.txt

Flask
Redis

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)

2. build 建立docker

$ ls
Dockerfile		app.py			requirements.txt

出現錯誤 ““docker build” requires exactly 1 argument…”

ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ docker build --tag=friendlyhello
"docker build" requires exactly 1 argument.
See 'docker build --help'.

Usage:  docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile

使用pwd獲取當前路徑,之後再加入路徑build

pwd 

ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ docker build --tag=friendlyhello  /home/ubuntu/py2.7Docker
。。。
。。。
Successfully built f6afc80826ac
Successfully tagged friendlyhello:latest         //成功

查看

ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
friendlyhello       latest              f6afc80826ac        43 seconds ago      131MB
ubuntu              18.04               94e814e2efa8        2 days ago          88.9MB
python              2.7-slim            8559620b5b0d        9 days ago          120MB

運行

docker run -p 4000:80 friendlyhello

停止

docker container stop 1fa4ab2cf395

3. 上傳到DockerHub

登陸DockerHub賬號:

ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ 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: lizengquan
Password:***

修改標籤,並push

ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ docker tag friendlyhello lizengquan/py2.7:001
ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
friendlyhello       latest              f6afc80826ac        26 minutes ago      131MB
lizengquan/py2.7    001                 f6afc80826ac        26 minutes ago      131MB

ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ docker push lizengquan/py2.7:001

查看
此時網頁上也會顯示

ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
friendlyhello       latest              f6afc80826ac        31 minutes ago      131MB
lizengquan/py2.7    001                 f6afc80826ac        31 minutes ago      131MB
ubuntu              18.04               94e814e2efa8        2 days ago          88.9MB

從遠程倉庫運行

docker run -p 4000:80 username/repository:tag

4. 運行docker


ubuntu@VM-0-13-ubuntu:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
friendlyhello       latest              f6afc80826ac        About an hour ago   131MB
lizengquan/py2.7    001                 f6afc80826ac        About an hour ago   131MB
ubuntu              18.04               94e814e2efa8        2 days ago          88.9MB
python              2.7-slim            8559620b5b0d        9 days ago          120MB

運行python

ubuntu@VM-0-13-ubuntu:~$ docker run -it friendlyhello:latest python2.7            //遠程運行類似
Python 2.7.16 (default, Mar  5 2019, 00:20:32)
[GCC 6.3.0 20170516] on linux2               
Type "help", "copyright", "credits" or "license" for more information.
>>> print "afd"
afd
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章