定製帶 Python3 的 Ubuntu 基礎 Docker 鏡像

1. 製做 Dockerfile

FROM daocloud.io/ubuntu:trusty
MAINTAINER water-law <dockerwaterlaw@daocloud.io>
RUN apt-get update && \
    apt-get install -y python3 \
                        python3-dev \
                        python3-pip \
    && apt-get clean \
    && apt-get autoclean \
    && rm -rf /var/lib/apt/lists/*
    
RUN mkdir -p /app
WORKDIR /app
EXPOSE 80
CMD ["bash"]

2. 生成鏡像

建立空文件夾, 將 Dockerfile 文件放在此目錄html

運行 docker build -t [tagname] .python

3. 導出鏡像

docker imagesdocker

docker save -o [output_name] [image]ubuntu

其中 output_name 通常是 tar, tar.xz 格式bash

4. 利用鏡像生成容器

docker run -it -v E:Projectswaterlawblog:/home/code [image] /bin/bashapp

5. 退出容器

exitide

6. 再次啓動容器

docker start [OPTIONS] CONTAINER [CONTAINER...]ui

說明: 對容器的修改是永久保存到文件系統的。code

可發現 E:Projectswaterlawblog 是掛載到 docker 容器的 /home/code 目錄的htm

7. 修改容器

好比 安裝 virtualenv 且在 /home 目錄使用 virtualenv -p python3 pysp

建立虛擬環境, 進入 /home/code, 運行 pip3 install -r requirements.txt

8. 提交容器修改

先退出容器 exit

基於舊的 container 提交新的 docker 鏡像

docker commit -m "commit message" -a "author info" [container_id] image_name:tag

示例

docker commit -m "earth" -a "zjp" 846edc550555 kuaiyun:earth

提示: 以上命令會打了一個 REPOSITORY 爲 kuaiyun、 TAG 爲 earth 的鏡像, 可經過

docker images 查看。

9. 導出容器

docker ps -a

| CONTAINER I D| IMAGE | COMMAND | CREATED STATUS | PORTS | NAMES |

| caa1f263a254 | myubuntu-base | "/bin/bash" | 32 minutes ago Exited (0) 54 seconds ago | | relaxed_lumiere |

docker export -o [outputfile] [container_id]

10. 導入容器 tar 文件

docker import [container_file] [command]

我這裏 command 是 /bin/bash, 因此 docker import [container_file] bash

11. 導入鏡像 tar 文件

docker load < [tar file]

注意 save 和 load 是配對的, import 和 export 是配對的。 import 還要加上 command。

參考文檔

如何製做一個定製的 Python 基礎 Docker 鏡像

相關文章
相關標籤/搜索