Docker Python Django 初始配置設定

王林
發布: 2024-09-01 21:09:38
原創
781 人瀏覽過

Docker 工作流程

1.確保您的 Dockerfile 正確

確保您的 Dockerfile 已準備好並位於專案的根目錄中。根據前面的討論,您的 Dockerfile 可能如下所示:

雷雷

.
.
.

2.建置 Docker 映像

開啟終端並導覽至 Dockerfile 所在的目錄。然後執行以下命令建置 Docker 映像並將其命名為 my-docker-image:

雷雷

此命令運行容器並將容器的連接埠 8000 對應到本機上的連接埠 8000,讓您可以透過 http://localhost:8000 存取 Django 應用程式。

如果你想在背景執行容器,請新增 -d 選項:

雷雷

這將以分離模式啟動容器。

**docker 映像**

要檢查系統上可用的 Docker 映像,您可以使用以下命令:

雷雷

此指令將顯示所有 Docker 映像的列表,以及它們的儲存庫、標籤、映像 ID、建立的和大小。

範例輸出:

雷雷

解釋:

  • REPOSITORY:鏡像的名稱,例如 my-docker-image。
  • TAG:鏡像的標籤,常用於指定版本。
  • 影像 ID:影像的唯一識別碼。
  • CREATED:建立映像的時間。
  • SIZE:影像的大小。

.
.
.

3.建置 Docker 容器

您提供的命令將以分離模式執行名為 my-docker-container 的 Docker 容器,將本機電腦上的連接埠 8001 對應到容器內的連接埠 8000。此指令的作用如下:

命令:

雷雷

解釋:

  • -d:以分離模式運行容器,這意味著它在後台運行。
  • --name my-docker-container:將名稱 my-docker-container 指派給容器。
  • -p 8001:8000:將本機上的連接埠 8001 對應到容器內的連接埠 8000。這允許您透過 http://localhost:8001 存取 Django 應用程式。
  • my-docker-image:指定用於容器的 Docker 映像。

驗證容器正在運行

執行此命令後,您可以使用以下命令檢查容器是否正在執行:

雷雷

這將列出所有正在運行的容器及其名稱、狀態和連接埠對映。

訪問應用程式

您現在可以透過在 Web 瀏覽器中導航至 http://localhost:8001 來存取您的 Django 應用程式。

.
.
.

4.Docker 卷

docker run -d --name my-docker-container -p 8001:8000 -v .:/app my-docker-image

您提供的 docker run 指令用於從 Docker 映像啟動 Docker 容器。以下是該指令的詳細說明:

  • -d:以分離模式運作容器(在背景)。
  • --name my-docker-container:為容器分配名稱(my-docker-container)。
  • -p 8001:8000:將容器內的連接埠 8000 對應到主機上的連接埠 8001。這表示您可以在主機上的 localhost:8001 處存取容器中執行的服務。
  • -v .:/app:將目前目錄 (.) 從主機掛載到容器內的 /app 目錄。當您想要即時查看更改而不重建映像時,這對於開發非常有用。
  • my-docker-image:指定用於容器的 Docker 映像。

因此,此命令將在後台啟動一個容器,容器內的連接埠 8000 可透過主機的連接埠 8001 訪問,並將當前目錄掛載到容器中的 /app 。如果您需要任何調整或進一步解釋,請隨時詢問!

.
.
.

5.Docker-compose.yml

docker-compose.yml 檔案用於定義和執行多容器 Docker 應用程式。以下是基於 docker run 指令的 docker-compose.yml 檔案的基本範例:

version: '3.8' # Specify the version of Docker Compose services: my-service: image: my-docker-image # The Docker image to use container_name: my-docker-container # The name of the container ports: - "8001:8000" # Map port 8000 in the container to port 8001 on the host volumes: - .:/app # Mount the current directory to /app in the container # Optional: Add environment variables if needed # environment: # - ENV_VAR_NAME=value # Optional: Specify any commands to run # command: python app.py # Optional: Define networks or other configurations here # networks: # default: # driver: bridge
登入後複製

Explanation:

  • version: Defines the version of Docker Compose file format. 3.8 is a common choice.
  • services: Lists all the containers you want to run.
    • my-service: The name of the service. You can use any name here.
    • image: Specifies the Docker image to use.
    • container_name: Assigns a name to the container.
    • ports: Maps container ports to host ports.
    • volumes: Mounts directories or files from the host to the container.
    • environment: (Optional) Defines environment variables inside the container.
    • command: (Optional) Overrides the default command specified in the Docker image.

To use this docker-compose.yml file, save it in your project directory and run:

docker-compose up
登入後複製

This command will start the container based on the configuration in the docker-compose.yml file.

Docker Python Django initial configuration setup

[Source - Mayank Ahuja ]

Let's understand the?????? ????????-

[1.] Develop
◾ Write your application code.

[2.] Dockerfile
◾ Create a Dockerfile that defines the environment and dependencies for your application.

[3.] Build Image
◾ Use docker build to create a Docker image from your Dockerfile.

[4.] Run Container
◾ Use docker run to launch a container from your image.
◾ The container is an isolated instance of your application.

[5.] Test
◾ Test your application within the container.
◾ If you make changes, rebuild the image and recreate the container.

[6.] Push => This is Optional
◾ Use docker push to share your image on a registry (e.g. Docker Hub).

[7.] Pull => This is Optional
◾ Others can use docker pull to download your image and run your application in their own environments.

以上是Docker Python Django 初始配置設定的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!