Docker 快速入門

Docker 入門

Docker是.......html

快速上手

1. 安裝

以MacOS爲例, 其餘操做系統的docker安裝與運行,請繞道至 Docker官網python

下載地址:

MacOS的Docker安裝包地址web

而後dmg文件安裝便可.redis

運行

和其餘軟件同樣, 運行Docker便可docker

2. 登入帳號

docker.io 上面註冊你我的的帳號, 用於建立同步你我的的容器鏡像flask

回到本機, 咱們使用命令行登入便可ubuntu

docker login
## 後續根據提示,輸入你的帳號密碼登入

3. 簡單命令

官方給出了一個示例命令vim

docker run hello-world

運行結果以下:瀏覽器

控制打印了一段說明文字bash

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

大體介紹了 docker run xxxx 命令後,docker內部幫我完成的後續工做

4. 建立容器 (Container)

官網給出了一個docker運行python的web應用的demo. 詳細步驟以下:

1. 建立一個工做目錄

# 我根據我的代碼習慣, 建立了以下目錄, (其實咱們只須要一個新的有操做權限的目錄空間)
mkdir -p ~/code/docker/demo-python
cd ~/code/docker/demo-python

2. 建立Dockerfile文件

# 切換到工做目錄
cd ~/code/docker/demo-python

# 建立 Dockerfile
touch Dockerfile

vim Dockerfile
# 編輯文件 Dockerfile 內容以下
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"]

3. 增長python棧的Web服務代碼

在工做目錄下建立 requirements.txtapp.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)

4. 構建

# -t 參數爲 指定tagname
docker build -t demopython .

# 查看剛構建的鏡像
docker images

5. 運行

# -p 參數: 使用本機4000端口映射容器中80端口服務
docker run -p 4000:80 demopython

6. 訪問服務

打開瀏覽器訪問服務 http://localhost:4000

相關文章
相關標籤/搜索