Dockerfile---指令碼自動化

@牛牛發表於2020-10-08

一:編寫一個Dockerfile指令碼,構造一個具有python3環境的映象

1:將原始Ubuntu映象壓縮包放在Desktop桌面。

python@ubuntu:~/Desktop$ ls
meimei_project  shopping-mall-project  sql指令碼  ubuntu.tar.gz  安裝包檔案

2:映象載入

python@ubuntu:~/Desktop$ docker load < ubuntu.tar.gz 
762d8e1a6054: Loading layer  91.39MB/91.39MB
e45cfbc98a50: Loading layer  15.87kB/15.87kB
d60e01b37e74: Loading layer  12.29kB/12.29kB
b57c79f4a9f3: Loading layer  3.072kB/3.072kB
832ff27e250b: Loading layer  187.7MB/187.7MB
f22b61b25d5c: Loading layer  3.072kB/3.072kB
2c3a13ec4d99: Loading layer    291MB/291MB
65bbf4f56f02: Loading layer  51.71kB/51.71kB
f235a7175832: Loading layer  44.08MB/44.08MB
a1174334046d: Loading layer  133.8MB/133.8MB
a290c0020912: Loading layer  92.14MB/92.14MB
2317a5d4b684: Loading layer   32.4MB/32.4MB
36dce86146ac: Loading layer  4.961MB/4.961MB
Loaded image: ubuntu:18.04

3: 將映象執行成容器:

python@ubuntu:~/Desktop$ docker run -d --network=host ubuntu:18.04 
1caf2d13cdec9d32456ad624dc53f81b14bf98fd4a7d9b8d7451538486898c8c

4:進入容器內部:

python@ubuntu:~/Desktop$ docker ps -a
CONTAINER ID        IMAGE                               COMMAND                  CREATED              STATUS                     PORTS                         NAMES
1caf2d13cdec        ubuntu:18.04                        "nginx '-g daemon of…"   About a minute ago   Up About a minute                                        practical_cerf
66987d8aec15        nginx                               "nginx -g 'daemon of…"   3 hours ago          Up 3 hours                 192.168.203.153:777->80/tcp   nginx-3
56e2e5bd67b7        mysql:5.7.22                        "docker-entrypoint.s…"   3 weeks ago          Exited (0) 4 days ago                                    mysql-slave
b5a7b35611f8        delron/elasticsearch-ik:2.4.6-1.0   "/docker-entrypoint.…"   4 weeks ago          Exited (143) 2 days ago                                  elasticsearch
f90e380d626a        delron/fastdfs                      "/usr/bin/start1.sh …"   4 weeks ago          Exited (137) 3 hours ago                                 storage
f951dc81977c        delron/fastdfs                      "/usr/bin/start1.sh …"   4 weeks ago          Exited (137) 3 hours ago                                 tracker
python@ubuntu:~/Desktop$ docker exec -it 1caf2d13cdec /bin/bash
root@ubuntu:/# 

5:安裝python3 :

root@ubuntu:/# apt-get install python3-pip -y
正在讀取軟體包列表... 完成
正在分析軟體包的依賴關係樹       
正在讀取狀態資訊... 完成       
python3-pip 已經是最新版 (20.0.2-5ubuntu1)。
升級了 0 個軟體包,新安裝了 0 個軟體包,要解除安裝 0 個軟體包,有 259 個軟體包未被升級。

6:生成新的容器:

python@ubuntu:~/Desktop$ docker ps -a
CONTAINER ID        IMAGE                               COMMAND                  CREATED             STATUS                     PORTS                         NAMES
1caf2d13cdec        ubuntu:18.04                        "nginx '-g daemon of…"   7 minutes ago       Up 7 minutes                                             practical_cerf
66987d8aec15        nginx                               "nginx -g 'daemon of…"   3 hours ago         Up 3 hours                 192.168.203.153:777->80/tcp   nginx-3
56e2e5bd67b7        mysql:5.7.22                        "docker-entrypoint.s…"   3 weeks ago         Exited (0) 4 days ago                                    mysql-slave
b5a7b35611f8        delron/elasticsearch-ik:2.4.6-1.0   "/docker-entrypoint.…"   4 weeks ago         Exited (143) 2 days ago                                  elasticsearch
f90e380d626a        delron/fastdfs                      "/usr/bin/start1.sh …"   4 weeks ago         Exited (137) 3 hours ago                                 storage
f951dc81977c        delron/fastdfs                      "/usr/bin/start1.sh …"   4 weeks ago         Exited (137) 3 hours ago                                 tracker
python@ubuntu:~/Desktop$ docker commit -m "安裝python3環境" -a "renshanwen" 1caf2d13cdec python3:V1.0
sha256:49c1e54dce5a0d82bd55a4de621476a9b717988e168393335401501727677d7c

此時發現多了一個python3的映象。

python@ubuntu:~/Desktop$ docker images
REPOSITORY                TAG                 IMAGE ID            CREATED              SIZE
python3                   V1.0                49c1e54dce5a        About a minute ago   852MB
ubuntu                    18.04               1975b4bd017d        6 hours ago          852MB
nginx                     latest              f949e7d76d63        12 months ago        126MB
mysql                     5.7.22              0d16d0a97dd1        2 years ago          372MB
delron/fastdfs            latest              8487e86fc6ee        2 years ago          464MB
delron/elasticsearch-ik   2.4.6-1.0           095b6487fb77        2 years ago          689MB

7:總結安裝的步驟:
選取一個基礎映象----> 執行成容器—> 進入容器—> 安裝需要的環境和部署—>將新的容器提交成新的映象。

專案部署—手動搭建映象

<一>: 前序操作:

1:進入meiduo_mall/uwsgi.ini中設定虛擬環境程式碼註釋掉(因為映象中使用的是全域性python,無需指定虛擬環境)

root@ubuntu:/data/meiduo_mall# vim uwsgi.ini

2:將meiduo_mall meiduo_mall_admin front_end_pc 打包

root@ubuntu:/data# ls
front_end_pc  front_end_pc.tar.gz  meiduo_mall  meiduo_mall_admin  meiduo_mall_admin.tar.gz  meiduo_mall.tar.gz

3:新建目錄,拷貝打包檔案和配置檔案:

root@ubuntu:/data# ls
front_end_pc  front_end_pc.tar.gz  meiduo_mall  meiduo_mall_admin  meiduo_mall_admin.tar.gz  meiduo_mall.tar.gz
root@ubuntu:/data# mkdir base
root@ubuntu:/data# mv *.tar.gz base
root@ubuntu:/data# ls
base  front_end_pc  meiduo_mall  meiduo_mall_admin
root@ubuntu:/data# cd base
root@ubuntu:/data/base# ls
front_end_pc.tar.gz  meiduo_mall_admin.tar.gz  meiduo_mall.tar.gz
root@ubuntu:/data/base# cp /etc/nginx/conf.d/*.conf ./
root@ubuntu:/data/base# ls
8000.conf  8080.conf  8081.conf  front_end_pc.tar.gz  meiduo_mall_admin.tar.gz  meiduo_mall.tar.gz

4:把ubuntu壓縮包恢復映象,並且執行成容器。

python@ubuntu:~/Desktop$ docker load < ubuntu.tar.gz 
python@ubuntu:~/Desktop$ docker container run -d --name meiduo ubuntu:18.04
74e33bfc58944dca7f266de109e7ace72f2c42cc03e0e1679ed0fc2f8d245c59

進入容器:

python@ubuntu:~/Desktop$ docker container exec -it meiduo /bin/bash
root@74e33bfc5894:/# 

<二>搭建容器—手動構建

1、安裝依賴

  • 4.1、Nginx安裝(韋偉老師給的映象已經安裝好啦)
  apt-get install nginx -y --allow-unauthenticated 
  • 4.2、安裝python3環境(韋偉老師給的映象已經安裝好啦)
  apt-get install python3-pip -y

2:上傳工程檔案並解壓:

root@74e33bfc5894:/# scp root@192.168.203.153:/data/base/front_end_pc.tar.gz /data/
root@192.168.203.153's password: 
front_end_pc.tar.gz                                                                                                                                   100% 1246KB 121.5MB/s   00:00    
root@74e33bfc5894:/# scp root@192.168.203.153:/data/base/meiduo_mall.tar.gz /data/
root@192.168.203.153's password: 
meiduo_mall.tar.gz                                                                                                                                    100%  453KB  84.8MB/s   00:00    
root@74e33bfc5894:/# scp root@192.168.203.153:/data/base/meiduo_mall_admin.tar.gz /data/
root@192.168.203.153's password: 
meiduo_mall_admin.tar.gz  
tar -z xvf front_end_pc.tar.gz 
tar -z xvf meiduo_mall.gz 
tar -z xvf meiduo_mall_admin.tar.gz 

3:安裝美多工程依賴環境(映象已經安裝好啦)

cd /data/meiduo_mall

pip3 install -r requirements.txt
4:上傳nginx部署檔案:

root@74e33bfc5894:/# scp root@192.168.203.153:/data/base/8080.conf  /etc/nginx/conf.d/
root@192.168.203.153's password: 
8080.conf                                                                                                                                             100%  193   507.0KB/s   00:00    
root@74e33bfc5894:/# scp root@192.168.203.153:/data/base/8081.conf  /etc/nginx/conf.d/
root@192.168.203.153's password: 
8081.conf                                                                                                                                             100%  185   438.9KB/s   00:00    
root@74e33bfc5894:/# scp root@192.168.203.153:/data/base/8000.conf  /etc/nginx/conf.d/
root@192.168.203.153's password: 
8000.conf      

5:退出重啟容器:(目的是重新啟動nginx服務)

python@ubuntu:~/Desktop$ docker restart meiduo
meiduo

6: 進入容器並啟動美多工程

docker container exec -it meiduo /bin/bash
cd /data/meiduo_mall
uwsgi --ini uwsgi.ini

<二>搭建容器—指令碼構建映象

1、編寫/data/base/Dockerfile指令碼,內容如下:

# 選擇基礎映象
FROM ubuntu:18.04
# 作者
MAINTAINER weiwei

# 更新源(韋偉老師給的映象已經安裝好故不用執行)
# RUN apt-get update -y
# 安裝nginx(韋偉老師給的映象已經安裝好故不用執行)
# RUN apt-get install nginx -y --allow-unauthenticated
# 安裝python3(韋偉老師給的映象已經安裝好故不用執行)
# RUN apt-get install python3-pip -y

# 把工程檔案上傳容器
ADD front_end_pc.tar.gz /data/
ADD meiduo_mall_admin.tar.gz /data/
ADD meiduo_mall.tar.gz /data/

# 切換到美多工程目錄,並且安裝依賴(韋偉老師給的映象已經安裝好故不用執行)
# WORKDIR /data/meiduo_mall/
# RUN pip3 install -r requirements.txt

COPY 8080.conf /etc/nginx/conf.d/
COPY 8081.conf /etc/nginx/conf.d/
COPY 8000.conf /etc/nginx/conf.d/

# 設定環境變數;作用:指定系統語言為UTF-8;如果不設定,uwsgi執行無法處理中文語言
ENV LC_ALL="zh_CN.utf8"

# 容器開放埠為8080和8081
EXPOSE 8080 8081
# 傳送容器啟動指令碼
COPY command.sh /data
# 切換容器目錄
WORKDIR /data
# 指定容器執行的啟動指令
ENTRYPOINT ["/bin/bash", "command.sh"]

新建command.sh指令碼檔案,啟動指令碼command.sh內容如下:

cd /data/meiduo_mall/
uwsgi --ini uwsgi.ini
nginx -g "daemon off;"

2、使用Dockerfile指令碼生成映象

docker image build -t meiduo ./

3、執行映象

docker container run -d --name meiduo --network=host meiduo:latest

4、使用瀏覽器訪問

相關文章