Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between service and container in 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

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 

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?

like image 265
Muhammad Raihan Muhaimin Avatar asked Feb 22 '16 23:02

Muhammad Raihan Muhaimin


People also ask

What is the difference between container and service in docker?

Docker Container is a runtime instance of Docker Image. Docker Service can run one type of Docker Images on various containers locating on different nodes to perform the same functionality.

What is a service in Docker compose?

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. Then, with a single command, you create and start all the services from your configuration.

Is a service a container docker?

docker service create is used to create instances (called tasks) of that service running in a cluster (called swarm) of computers (called nodes). Those tasks are containers of cource, but not standalone containers. In a sense a service acts as a template when instantiating tasks.

What is the difference between docker and Docker compose?

The key difference between docker run versus docker-compose is that docker run is entirely command line based, while docker-compose reads configuration data from a YAML file. The second major difference is that docker run can only start one container at a time, while docker-compose will configure and run multiple.


2 Answers

Services and container are related but both are different things.

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

For example:

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 

This compose file defines two services, web and db.

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 

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

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 

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 

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

$docker-compose stop web 
like image 68
Hemerson Varela Avatar answered Oct 07 '22 12:10

Hemerson Varela


i cant fully answer your question as i dont understand about services myself. However, I do understand volumes_from in relation to containers and so this will answer half your question :) A partial answer is better than none?

CONTAINER A

volumes: /my/shared/directory container_name:ca 

CONTAINER B

volumes_from:ca 

Now container B will have a new directory in it /my/shared/directory which is mounted from container A - the contents of the directory in container B will be exactly the same as the contents of the directory in container A

if you discover about services plz let me know

like image 33
danday74 Avatar answered Oct 07 '22 10:10

danday74