예배 규칙서 찾다
Compose About versions and upgrading (Compose) ASP.NET Core + SQL Server on Linux (Compose) CLI environment variables (Compose) Command-line completion (Compose) Compose(组成) Compose command-line reference(组合命令行参考) Control startup order (Compose) Django and PostgreSQL (Compose) Docker stacks and distributed application bundles (Compose) docker-compose build(docker-compose构建) docker-compose bundle docker-compose config docker-compose create docker-compose down docker-compose events docker-compose exec docker-compose help docker-compose images docker-compose kill docker-compose logs docker-compose pause docker-compose port docker-compose ps docker-compose pull docker-compose push docker-compose restart docker-compose rm docker-compose run docker-compose scale docker-compose start docker-compose stop docker-compose top docker-compose unpause docker-compose up Environment file (Compose) Environment variables in Compose Extend services in Compose Frequently asked questions (Compose) Getting started (Compose) Install Compose Link environment variables (deprecated) (Compose) Networking in Compose Overview of Docker Compose Overview of docker-compose CLI Quickstart: Compose and WordPress Rails and PostgreSQL (Compose) Sample apps with Compose Using Compose in production Using Compose with Swarm Engine .NET Core application (Engine) About images, containers, and storage drivers (Engine) Add nodes to the swarm (Engine) Apply custom metadata (Engine) Apply rolling updates (Engine) apt-cacher-ng Best practices for writing Dockerfiles (Engine) Binaries (Engine) Bind container ports to the host (Engine) Breaking changes (Engine) Build your own bridge (Engine) Configure container DNS (Engine) Configure container DNS in user-defined networks (Engine) CouchDB (Engine) Create a base image (Engine) Create a swarm (Engine) Customize the docker0 bridge (Engine) Debian (Engine) Default bridge network Delete the service (Engine) Deploy a service (Engine) Deploy services to a swarm (Engine) Deprecated Engine features Docker container networking (Engine) Docker overview (Engine) Docker run reference (Engine) Dockerfile reference (Engine) Dockerize an application Drain a node (Engine) Engine FAQ (Engine) Fedora (Engine) Get started (Engine) Get started with macvlan network driver (Engine) Get started with multi-host networking (Engine) How nodes work (Engine) How services work (Engine) Image management (Engine) Inspect the service (Engine) Install Docker (Engine) IPv6 with Docker (Engine) Join nodes to a swarm (Engine) Legacy container links (Engine) Lock your swarm (Engine) Manage nodes in a swarm (Engine) Manage sensitive data with Docker secrets (Engine) Manage swarm security with PKI (Engine) Manage swarm service networks (Engine) Migrate to Engine 1.10 Optional Linux post-installation steps (Engine) Overview (Engine) PostgreSQL (Engine) Raft consensus in swarm mode (Engine) Riak (Engine) Run Docker Engine in swarm mode Scale the service (Engine) SDKs (Engine) Select a storage driver (Engine) Set up for the tutorial (Engine) SSHd (Engine) Storage driver overview (Engine) Store service configuration data (Engine) Swarm administration guide (Engine) Swarm mode key concepts (Engine) Swarm mode overlay network security model (Engine) Swarm mode overview (Engine) Understand container communication (Engine) Use multi-stage builds (Engine) Use swarm mode routing mesh (Engine) Use the AUFS storage driver (Engine) Use the Btrfs storage driver (Engine) Use the Device mapper storage driver (Engine) Use the OverlayFS storage driver (Engine) Use the VFS storage driver (Engine) Use the ZFS storage driver (Engine) Engine: Admin Guide Amazon CloudWatch logs logging driver (Engine) Bind mounts (Engine) Collect Docker metrics with Prometheus (Engine) Configuring and running Docker (Engine) Configuring logging drivers (Engine) Control and configure Docker with systemd (Engine) ETW logging driver (Engine) Fluentd logging driver (Engine) Format command and log output (Engine) Google Cloud logging driver (Engine) Graylog Extended Format (GELF) logging driver (Engine) Journald logging driver (Engine) JSON File logging driver (Engine) Keep containers alive during daemon downtime (Engine) Limit a container's resources (Engine) Link via an ambassador container (Engine) Log tags for logging driver (Engine) Logentries logging driver (Engine) PowerShell DSC usage (Engine) Prune unused Docker objects (Engine) Run multiple services in a container (Engine) Runtime metrics (Engine) Splunk logging driver (Engine) Start containers automatically (Engine) Storage overview (Engine) Syslog logging driver (Engine) tmpfs mounts Troubleshoot volume problems (Engine) Use a logging driver plugin (Engine) Using Ansible (Engine) Using Chef (Engine) Using Puppet (Engine) View a container's logs (Engine) Volumes (Engine) Engine: CLI Daemon CLI reference (dockerd) (Engine) docker docker attach docker build docker checkpoint docker checkpoint create docker checkpoint ls docker checkpoint rm docker commit docker config docker config create docker config inspect docker config ls docker config rm docker container docker container attach docker container commit docker container cp docker container create docker container diff docker container exec docker container export docker container inspect docker container kill docker container logs docker container ls docker container pause docker container port docker container prune docker container rename docker container restart docker container rm docker container run docker container start docker container stats docker container stop docker container top docker container unpause docker container update docker container wait docker cp docker create docker deploy docker diff docker events docker exec docker export docker history docker image docker image build docker image history docker image import docker image inspect docker image load docker image ls docker image prune docker image pull docker image push docker image rm docker image save docker image tag docker images docker import docker info docker inspect docker kill docker load docker login docker logout docker logs docker network docker network connect docker network create docker network disconnect docker network inspect docker network ls docker network prune docker network rm docker node docker node demote docker node inspect docker node ls docker node promote docker node ps docker node rm docker node update docker pause docker plugin docker plugin create docker plugin disable docker plugin enable docker plugin inspect docker plugin install docker plugin ls docker plugin push docker plugin rm docker plugin set docker plugin upgrade docker port docker ps docker pull docker push docker rename docker restart docker rm docker rmi docker run docker save docker search docker secret docker secret create docker secret inspect docker secret ls docker secret rm docker service docker service create docker service inspect docker service logs docker service ls docker service ps docker service rm docker service scale docker service update docker stack docker stack deploy docker stack ls docker stack ps docker stack rm docker stack services docker start docker stats docker stop docker swarm docker swarm ca docker swarm init docker swarm join docker swarm join-token docker swarm leave docker swarm unlock docker swarm unlock-key docker swarm update docker system docker system df docker system events docker system info docker system prune docker tag docker top docker unpause docker update docker version docker volume docker volume create docker volume inspect docker volume ls docker volume prune docker volume rm docker wait Use the Docker command line (Engine) Engine: Extend Access authorization plugin (Engine) Docker log driver plugins Docker network driver plugins (Engine) Extending Engine with plugins Managed plugin system (Engine) Plugin configuration (Engine) Plugins API (Engine) Volume plugins (Engine) Engine: Security AppArmor security profiles for Docker (Engine) Automation with content trust (Engine) Content trust in Docker (Engine) Delegations for content trust (Engine) Deploying Notary (Engine) Docker security (Engine) Docker security non-events (Engine) Isolate containers with a user namespace (Engine) Manage keys for content trust (Engine) Play in a content trust sandbox (Engine) Protect the Docker daemon socket (Engine) Seccomp security profiles for Docker (Engine) Secure Engine Use trusted images Using certificates for repository client verification (Engine) Engine: Tutorials Engine tutorials Network containers (Engine) Get Started Part 1: Orientation Part 2: Containers Part 3: Services Part 4: Swarms Part 5: Stacks Part 6: Deploy your app Machine Amazon Web Services (Machine) Digital Ocean (Machine) docker-machine active docker-machine config docker-machine create docker-machine env docker-machine help docker-machine inspect docker-machine ip docker-machine kill docker-machine ls docker-machine provision docker-machine regenerate-certs docker-machine restart docker-machine rm docker-machine scp docker-machine ssh docker-machine start docker-machine status docker-machine stop docker-machine upgrade docker-machine url Driver options and operating system defaults (Machine) Drivers overview (Machine) Exoscale (Machine) Generic (Machine) Get started with a local VM (Machine) Google Compute Engine (Machine) IBM Softlayer (Machine) Install Machine Machine Machine CLI overview Machine command-line completion Machine concepts and help Machine overview Microsoft Azure (Machine) Microsoft Hyper-V (Machine) Migrate from Boot2Docker to Machine OpenStack (Machine) Oracle VirtualBox (Machine) Provision AWS EC2 instances (Machine) Provision Digital Ocean Droplets (Machine) Provision hosts in the cloud (Machine) Rackspace (Machine) VMware Fusion (Machine) VMware vCloud Air (Machine) VMware vSphere (Machine) Notary Client configuration (Notary) Common Server and signer configurations (Notary) Getting started with Notary Notary changelog Notary configuration files Running a Notary service Server configuration (Notary) Signer configuration (Notary) Understand the service architecture (Notary) Use the Notary client
문자

本快速入门指南演示了如何使用Docker Compose来设置和运行简单的Django / PostgreSQL应用程序。在开始之前,您需要安装Compose。

定义项目组件

对于这个项目,你需要创建一个Dockerfile,一个Python依赖关系文件和一个docker-compose.yml文件。(您可以使用此文件的扩展名.yml.yaml扩展名。)

  1. 创建一个空的项目目录。您可以为该目录命名一些容易记忆的内容。该目录是应用程序映像的上下文。该目录应该只包含构建该映像的资源。

  2. Dockerfile在您的项目目录中创建一个新文件。Dockerfile通过一个或多个配置该映像的构建命令来定义应用程序的映像内容。一旦建成,你可以在一个容器中运行图像。有关更多信息Dockerfiles,请参阅Docker用户指南和Dockerfile参考。

  3. 将以下内容添加到Dockerfile。FROM python:3 ENV PYTHONUNBUFFERED 1 RUN mkdir / code WORKDIR / code ADD requirements.txt / code / RUN pip install -r requirements.txt ADD。/ code /这Dockerfile从一个Python 3父图像开始。父图像通过添加新code目录进行修改。通过安装requirements.txt文件中定义的Python需求进一步修改父映像。

  4. 保存并关闭Dockerfile

  5. requirements.txt在您的项目目录中创建一个。该文件由RUN pip install -r requirements.txt您的命令使用Dockerfile

  6. 在文件中添加所需的软件。Django> = 1.8,<2.0 psycopg2

  7. 保存并关闭requirements.txt文件。

  8. docker-compose.yml在您的项目目录中创建一个名为的文件。该docker-compose.yml文件描述了制作您的应用的服务。在这个例子中,这些服务是一个Web服务器和数据库。撰写文件还描述了这些服务使用哪些Docker映像,它们如何链接在一起,以及它们可能需要装入容器内的任何卷。最后,该docker-compose.yml文件描述了这些服务公开哪些端口。有关docker-compose.yml此文件如何工作的更多信息,请参阅参考资料。

  9. 将以下配置添加到文件中。版本:'3'服务:db:image:postgres web:build:。命令:python3 manage.py runserver 0.0.0.0:8000卷: - 。:/ code ports: - “8000:8000”depends_on: -  db这个文件定义了两个服务:db服务和web服务。

  10. 保存并关闭docker-compose.yml文件。

创建一个Django项目

在这一步中,您将通过构建上一过程中定义的构建上下文中的图像来创建Django初学者项目。

  1. 切换到您的项目目录的根目录。

  2. 通过运行docker-compose run命令来创建Django项目,如下所示。docker-compose运行web django-admin.py startproject composeexample。这指示Compose django-admin.py startproject composeexample使用web服务的映像和配置在容器中运行。由于该web图像尚不存在,因此Compose会从当前目录中构建它,如build: .线条所示docker-compose.yml。一旦web构建了服务映像,Compose就会运行它并django-admin.py startproject在容器中执行该命令。该命令指示Django创建一组代表Django项目的文件和目录。

  3. 在后docker-compose命令完成后,列出项目的内容。$ ls -l drwxr-xr-x 2 root root composeexample -rw-rw-r-- 1用户用户docker-compose.yml -rw-rw-r-- 1个用户用户Dockerfile -rwxr-xr-x 1 root root manage.py -rw-rw-r-- 1个用户用户requirements.txt如果您在Linux上运行Docker,则django-admin创建的文件将由root用户拥有。发生这种情况是因为容器以root用户身份运行。更改新文件的所有权。sudo chown -R $ USER:$ USER。如果您在Mac或Windows上运行Docker,则您应拥有所有文件的所有权,包括由所生成的文件django-admin。列出文件只是为了验证这一点。$ ls -l total 32 -rw -r -r-- 1位用户员工145 Feb 13 23:00 Dockerfile drwxr-xr-x 6用户人员204 Feb 13 23:07 composeexample -rw-r  -  r-- 1用户工作人员159 Feb 13 23:02 docker-compose.yml -rwxr-xr-x 1用户人员257 Feb 13 23:07 manage.py -rw -r  -  r-- 1个用户人员16 Feb 13 23:01需求。文本

连接数据库

在本节中,您将为Django设置数据库连接。

  1. 在您的项目目录中,编辑该composeexample/settings.py文件。

  2. 替换为DATABASES = ...:DATABASES = {'default':{'ENGINE':'django.db.backends.postgresql','NAME':'postgres','USER':'postgres','HOST':'db ','PORT':5432,}}这些设置由中指定的postgres Docker镜像决定docker-compose.yml

  3. 保存并关闭文件。

  4. 从项目的顶级目录运行docker-compose up命令。$ docker-compose up djangosample_db_1是最新版本创建djangosample_web_1 ...创建djangosample_web_1 ... done附加到djangosample_db_1,djangosample_web_1 db_1 | 属于这个数据库系统的文件将由用户“postgres”拥有。db_1 | 该用户还必须拥有服务器进程。db_1 | db_1 | 数据库集群将使用语言环境“en_US.utf8”进行初始化。db_1 | 默认的数据库编码相应地被设置为“UTF8”。db_1 | 默认的文本搜索配置将被设置为“english”。。。。web_1 | 2017年5月30日 -  21:44:49 web_1 | Django版本1.11.1,使用设置'composeexample.settings'web_1 | 从http://0.0.0.0启动开发服务器:8000 / web_1 | 使用CONTROL-C退出服务器。此时,您的Django应用程序应该在端口上运行8000在您的Docker主机上。在Docker for Mac和Docker for Windows上,转到http://localhost:8000Web浏览器以查看Django欢迎页面。如果您使用Docker Machine,则docker-machine ip MACHINE_VM返回可添加端口(<Docker-Host-IP>:8000)的Docker主机IP地址。

注意:

在某些平台(Windows 10)上,您可能需要编辑ALLOWED_HOSTS内部settings.py并将Docker主机名或IP地址添加到列表中。为了演示目的,您可以将该值设置为:

ALLOWED_HOSTS = '*'

请注意,这个值是不是对生产使用是安全的。有关更多信息,请参阅Django文档。

  1. 列出运行的容器。在另一个终端窗口中,使用该docker ps命令列出正在运行的Docker进程。$ docker ps容器ID映像命令创建状态端口名称def85eff5f51 django_web“python3 manage.py ...”10分钟前上传9分钟0.0.0.0:8000->8000/tcp django_web_1 678ce61c79cc postgres“docker-entrypoint ...”20分钟前上升9分钟5432 / tcp django_db_1

  2. 关闭服务并使用以下任一方法进行清理:

-  Stop the application by typing `Ctrl-C` in the same shell in where you started it: Gracefully stopping... (press Ctrl+C again to force) Killing test_web_1 ... done Killing test_db_1 ... done
-  Or, for a more elegant shutdown, switch to a different shell, and run [docker-compose down](../reference/down/index) from the top level of your Django sample project directory. vmb at mymachine in ~/sandbox/django $ docker-compose down Stopping django_web_1 ... done Stopping django_db_1 ... done Removing django_web_1 ... done Removing django_web_run_1 ... done Removing django_db_1 ... done Removing network django_default

关闭应用程序后,您可以安全地删除Django项目目录(例如,rm -rf django)。

이전 기사: 다음 기사: