簡體   English   中英

從多個容器創建單個 docker 鏡像

[英]Create single docker image from multiple containers

我對容器世界和 docker 很陌生,但由於我正在從事的項目的特定基礎設施限制,我覺得這是我需要利用的技術。

我正在嘗試在我的 Windows 10 機器上構建我的應用程序,然后將其遷移到另一台 Windows 10 計算機,該計算機位於另一個無法訪問互聯網且無法進行通信的網絡上(我只能從我的主網絡推/拉)。 該應用程序是在 uWsgi/nginx 服務器上運行的 Flask 應用程序。

我遵循了一些關於 docker 和 docker compose 的教程,並提出了以下應用程序結構:

Application
    - flask
        - app
        - env
        - Dockerfile
        - app.config
        - run.py
    - nginx
        - Dockerfile
        - nginx.conf
    - docker-compose.yml

docker compose 文件的內容:

version: "3.7"

services:

  nginx:
    build: ./nginx
    image: nginx
    container_name: nginx
    restart: always
    ports:
      - "80:80"
      - "443:443"

  flask:
    build: ./flask
    container_name: flask
    restart: always
    image: trac
    environment:
      - APP_NAME=Trac
    expose:
      - 8080
    depends_on:
      - nginx

Flask Dockerfile 的內容:

# Use python 3.7 container image
FROM python:3.7.2-stretch

# Set the working directory of the app
WORKDIR  /app

# Copy the directory contents into the container at /app
ADD . /app

# Install the dependencies
RUN pip install -r requirements.txt

# run the command to start uWSGI 
CMD ["uwsgi", "app.ini"]

app.ini 的內容

[uwsgi]
wsgi-file = run.py
callable = app
socket = :8080
processes = 4
threads = 2
master = true
chmod-socket = 660
vacuum = true
die-on-term = true

nginx Dockerfile 的內容:FROM nginx

RUN rm /etc/nginx/conf.d/default.conf


COPY crt.crt /etc/nginx/
COPY key.key /etc/nginx/

COPY nginx.conf /etc/nginx/conf.d

nginx.conf 的內容

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    listen 443 ssl http2 default_server;
    ssl_certificate crt.crt;
    ssl_certificate_key key.key;
    location / {
        include uwsgi_params;
        uwsgi_pass flask:8080;
    }
}

使用 docker-compose build 時,我構建了多個圖像,我認為這不是預期的結果? 我想我在想這將是一個單一的圖像,然后可以移動並運行到其他地方。

問題是如何移動圖像並在沒有互聯網訪問的計算機上運行它們。

我能夠在本地構建應用程序並且一切正常。

對此的任何幫助都會很棒。 提前致謝。

對於你的第一個問題,你的假設是錯誤的。 沒有“一張圖片”可以涵蓋撰寫中的所有服務。 每個服務都有自己的形象。

對於沒有互聯網問題的分發......您可以使用本地注冊表,您之前已經在其中加載了您擁有的所有基本依賴項(python、nginx 容器...)。

並不是說我會推薦它,但是您可以使用容器內運行的所有服務創建單個 docker 映像,例如 GitLab docker 發行版也這樣做(它運行所有服務——nginx、app、postgres ……在單個圖像中)。 您只需要在單個容器中自己配置所有服務,這可能很乏味,但可行。

還有一個 docker-in-docker 圖像,您可以使用該圖像來運行您的 docker-compose 文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM