2018年5月31日 星期四

Dockerfile 指令筆記

本篇文章參考來源:
https://docs.docker.com/get-started/part2/
https://docs.docker.com/engine/reference/builder/

使用者可以透過Dockerfile來定義Docker container的開發環境。

Dockerfile的基本格式:
# comment
INSTRUCTION arguments

引用官方範例如下:
# 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"]


FROM


FROM <image>[:<tag>]

引入container所需要的Base Image。


WORKDIR


WORKDIR /path/to/workdir

指定docker執行指令的工作目錄


ADD


ADD <src>... <dst>

將<src>的檔案及目錄複製到image的<dst>中


RUN


RUN <command>
RUN ['executable', 'param1, 'param2']

在當前的image上會再建立新的一層image,並在新的image執行RUN的command


EXPOSE


EXPOSE <port> [<port>/<protocol>...]

指定container對外部開放的port


ENV


ENV <key> <value>
ENV <key>=<value> ...

建立build stage時的environment variable


CMD


CMD ["executable","param1","param2"]
CMD ["param1","param2"]
CMD command param1 param2

image被啟動時所執行的command,在Dockerfile中只有最後一個CMD會被執行。


2018年5月28日 星期一

在 Ubuntu下安裝 Docker CE

本篇文章參考來源:https://docs.docker.com/install/linux/docker-ce/ubuntu

  • 移除舊版的 Docker

在Docker較新的版本中,將Docker區分為CE(Community Edition)與EE(Enterprise Edition)的版本, 所以在安裝前,最好先移除舊版的Docker。

$ sudo apt-get remove docker docker-engine docker-io

  • 透過 Docker的 repository來進行安裝


設定 Docker的 repository


1. 更新 apt的套件資訊

apt會使用 /etc/apt/sources.list來更新套件資訊,讓套件與相關 repository同步。

$ sudo apt-get update

2. 安裝相關套件讓 apt能透過 HTTPS使用 repository

$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

3. 加入Docker的 GPG key

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
接下來,確認剛加入的key是否含有以下fingerprint
9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88

搜尋fingerprint最後8個字元為0EBFCD88 $ sudo apt-get fingerprint 0EBFCD88 pub   4096R/0EBFCD88 2017-02-22
      Key fingerprint = 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
uid                  Docker Release (CE deb) <docker@docker.com>
sub   4096R/F273FCD8 2017-0222

4. 使用Docker Stable 的 repository

$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"


安裝Docker CE


1. 再次更新 apt的套件資訊

$ sudo apt-get update

2. 安裝Docker CE

$ sudo apt-get install docker-ce

3. 運行 hello-world image來確認Docker是否安裝成功

$ sudo docker run hello-world

2018年5月14日 星期一

使用 Virtualenv 開發 Python



  • 安裝 Virtualenv

使用pip指令安裝Virtualenv

Windows:
> python -m pip install virtualenv


  • 使用 Virtualenv 建立虛擬環境

將路徑切換至Python需要建立虛擬環境的目錄下,並執行

Windows:
> python -m virtualenv [env_name]
env_name為所要建立虛擬環境的名稱,之後就會在目錄下產生名為[env_name]的資料夾


  • 運行虛擬環境

執行env_name/Scripts中的activate檔案

Windows:
> .\venv\Scripts\activate.bat

執行成功後,命令視窗就會出現目前正運行在虛擬環境中的提示字樣
(env_name)


  • 離開虛擬環境

執行env_name/Scripts中的deactivate檔案

Windows:
> .\venv\Scripts\deactivate.bat

若命令視窗中的提示字樣消失,就代表已經成功離開虛擬環境