docker compose中服务和容器的区别 [英] Difference between service and container in docker compose

查看:31
本文介绍了docker compose中服务和容器的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 docker compose 中的 volumes_from 选项.显然,您可以从容器或服务导入卷.来自 docker compose 文档是:

I was going through volumes_from option in docker compose. Apparently you can import a volumes from either a container or a service. From the docker compose documentation it is:

volumes_from

从另一个服务或容器安装所有卷,可选指定只读访问(ro)或读写(rw).

Mount all of the volumes from another service or container, optionally specifying read-only access(ro) or read-write(rw).

volumes_from:
 - service_name
 - service_name:ro
 - container:container_name
 - container:container_name:rw

注意:container:... 格式仅在版本 2 中受支持文件格式.在版本 1 中,您可以使用容器名称而无需标记他们是这样的:

Note: The container:... formats are only supported in the version 2 file format. In version 1, you can use container names without marking them as such:

- service_name
- service_name:ro
- container_name
- container_name:rw

我在这里很困惑,这里的容器和服务有什么区别?

I am confused here what is the difference between containers and services here?

推荐答案

服务和容器是相关的,但两者是不同的.

Services and container are related but both are different things.

一项服务可以由一个或多个容器运行.使用 docker 可以处理容器,使用 docker-compose 可以处理服务.

A service can be run by one or multiple containers. With docker you can handle containers and with docker-compose you can handle services.

例如:

假设我们有这个 docker-compose.yml 文件:

Let's say that we have this docker-compose.yml file:

web:
  image: example/my_web_app:latest
  expose:
    - 80
  links:
    - db 

db:
  image: postgres:latest

这个撰写文件定义了两个服务,webdb.

This compose file defines two services, web and db.

当你运行 docker-compose up 时,假设项目目录是 test1 那么 compose 会启动 2 个名为 test1_db_1 的容器>test1_web_1.

When you run docker-compose up, Assuming that the project directory is test1 then compose will start 2 containers named test1_db_1 and test1_web_1.

$ docker ps -a
CONTAINER ID   IMAGE        COMMAND          ...      NAMES
1c1683e871dc   test1_web    "nginx -g"       ...      test1_web_1
a41360558f96   test1_db     "postgres -d"    ...      test1_db_1

因此,此时您有 2 个服务和 1 个容器.

So, in this point you have 2 services and 1 container for each.

但是您可以扩展名为 web 的服务以使用 5 个容器.

But you could scale the service named web to use 5 containers.

$ docker-compose scale web=5
Creating and starting 2 ... done
Creating and starting 3 ... done
Creating and starting 4 ... done
Creating and starting 5 ... done

此时你有 2 个服务和 6 个容器

In this point you have 2 services and 6 containers

$ docker ps -a  
CONTAINER ID   IMAGE        COMMAND         ...      NAMES
1bf4c939263f   test1_web    "nginx -g"      ...      test1_web_3
d3033964a44b   test1_web    "nginx -g"      ...      test1_web_4
649bbda4d0b0   test1_web    "nginx -g"      ...      test1_web_5
a265ea406727   test1_web    "nginx -g"      ...      test1_web_2
1c1683e871dc   test1_web    "nginx -g"      ...      test1_web_1
a41360558f96   test1_db     "postgres -d'   ...      test1_db_1

此外,使用 docker-compose,您可以针对一个或多个服务运行子命令.

Additionally, with docker-compose you can run subcommand against one or more services.

$docker-compose stop web

这篇关于docker compose中服务和容器的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆