在 Docker-Compose 中执行多条命令
Docker 通过将应用程序包装在称为容器的标准化单元中,使开发人员可以更轻松地构建、测试和部署应用程序,而无需担心依赖性。 Docker-compose 是管理多容器应用程序的高级必备工具。
您可以使用 docker-compose 定义指令来管理依赖于多个 Docker 容器的服务。
除了能够管理多个容器之外,docker-compose 还提供了其他好处,例如应用程序服务的隔离环境以及在创建容器时保留卷数据。
在 Docker-Compose 中执行多条命令
Docker-compose 在 Docker 引擎上运行,与 Docker 一起安装在 Mac 和 Windows 机器上。 下面的命令可让您验证 docker-compose 是否已在您的机器中正确设置。
$ docker-compose version
Docker Compose version v2.2.3
在下面的示例中,我们将探讨如何使用 docker-compose 设置 Django、PostgreSQL 应用程序。 我们将探讨如何在 docker-compose 中执行多个命令。
首先,我们将创建一个包含简单条目的简单 Dockerfile,如下所示。 该文件概述了将执行以创建我们的 Docker 映像的命令。
FROM python:3
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /tonyloi
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /tonyloi/
Dockerfile 将 Python3 定义为基础镜像; 但是,我们还定义了一个 requirements.txt 文件来说明项目运行所需的其他要求。
Django>=3.0,<4.0
psycopg2>=2.8
现在您已准备好这两个文件,您可以在该项目的根目录中创建 docker-compose.yml 文件。 该文件描述了我们打算构建的应用程序服务:数据库和网络服务器。
除此之外,我们还将定义这些服务使用的 Docker 映像以及您可能希望挂载的任何其他 Docker 卷。 最后,我们还需要指定服务将公开的端口。
由于这是一个 Django 应用程序,我们还必须指定允许我们运行迁移和启动开发服务器的命令。 有几种方法可以做到这一点,使用 sh 或 bash。
在这种情况下,我们将使用 sh,因为它在基于 Unix 的系统中可用。
docker-compose.yml:
version: "3.9"
services:
DB:
image: Postgres
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
build: .
command: >
sh -c "
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8080"
volumes:
- .:/tonyloi
ports:
- "8080:8080"
environment:
- POSTGRES_NAME=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
depends_on:
- DB
我们将运行下面的 docker-compose run
命令来创建这个 Django 项目。 在执行此命令之前,您必须确保您位于根目录中。
如果没有,请使用 cd 命令导航到根目录。
~/django_app$ sudo docker-compose run web Django-admin startproject new_app .
输出:
Successfully built 00a4c8d65c91
Successfully tagged django_app_web:latest
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating django_app_db_1 ... done
Creating django_app_web_run ... done
此命令使用 docker-compose 和 Docker 文件来创建 Django 项目。 在本例中,我们将项目命名为 new_app。
您现在可以导航到项目并列出项目文件。
isaac@DESKTOP-HV44HT6:~/django_app$ cd new_app
isaac@DESKTOP-HV44HT6:~/django_app/new_app$ ls
__init__.py asgi.py settings.py urls.py wsgi.py
isaac@DESKTOP-HV44HT6:~/django_app/new_app$
本文重点介绍了如何使用 sh 在 docker-compose 文件中执行多个命令。 或者,您也可以使用 bash -c
,如下所示。
command: bash -c "
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8080
"
我们可以通过在 settings.py 文件中配置数据库设置来完成 Django 应用程序其余部分的创建。
Django 在开发时默认使用 SQLite; 因此,由于我们使用的是 PostgreSQL,因此需要进行以下必要的配置。 请务必将数据库和用户名替换为您自己的。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('postgres'),
'USER': os.environ.get('postgres'),
'PASSWORD': os.environ.get('enter_your database'),
'HOST': 'db',
'PORT': 5432,
}
}
最后,为了启动,应用程序在项目的主目录中执行 sudo docker-compose up
命令。 它在 http://0.0.0.0:8080/ 启动开发服务器,正如我们在 docker-compose 文件中指定的那样。
~/django_app$ sudo docker-compose up
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
相关文章
Get the IP address of the Docker container from the host using docker inspect
发布时间:2025/03/26 浏览次数:102 分类:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
Solution to incorrect access log time when deploying Nginx in Docker
发布时间:2025/03/26 浏览次数:165 分类:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
Docker deploys nginx php application
发布时间:2025/03/26 浏览次数:131 分类:Docker
-
I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam
How to use Docker to image a Node.js web application
发布时间:2025/03/26 浏览次数:107 分类:Docker
-
Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at
Start a Bash terminal in a new Docker container
发布时间:2025/03/26 浏览次数:97 分类:Docker
-
Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers
Passing environment variables to containers in Docker
发布时间:2025/03/26 浏览次数:124 分类:Docker
-
This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t
Install Docker using Homebrew
发布时间:2025/03/26 浏览次数:202 分类:Docker
-
There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide
Enforce clean build of images in Docker
发布时间:2025/03/26 浏览次数:87 分类:Docker
-
This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho
Running a Docker instance from a Dockerfile
发布时间:2025/03/26 浏览次数:140 分类:Docker
-
Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application