ChatGPT解决这个技术问题 Extra ChatGPT

What is the difference between docker and docker-compose

docker and docker-compose seem to be interacting with the same dockerFile, what is the difference between the two tools?


B
BMitch

The docker cli is used when managing individual containers on a docker engine. It is the client command line to access the docker daemon api.

The docker-compose cli can be used to manage a multi-container application. It also moves many of the options you would enter on the docker run cli into the docker-compose.yml file for easier reuse. It works as a front end "script" on top of the same docker api used by docker, so you can do everything docker-compose does with docker commands and a lot of shell scripting. See this documentation on docker-compose for more details.

Update for Swarm Mode

Since this answer was posted, docker has added a second use of docker-compose.yml files. Starting with the version 3 yml format and docker 1.13, you can use the yml with docker-compose and also to define a stack in docker's swarm mode. To do the latter you need to use docker stack deploy -c docker-compose.yml $stack_name instead of docker-compose up and then manage the stack with docker commands instead of docker-compose commands. The mapping is a one for one between the two uses:

Compose Project -> Swarm Stack: A group of services for a specific purpose

Compose Service -> Swarm Service: One image and it's configuration, possibly scaled up.

Compose Container -> Swarm Task: A single container in a service

For more details on swarm mode, see docker's swarm mode documentation.


J
JSON C11

docker manages single containers

docker-compose manages multiple container applications

Usage of docker-compose requires 3 steps:

Define the app environment with a Dockerfile Define the app services in docker-compose.yml Run docker-compose up to start and run app

https://i.stack.imgur.com/KQMXU.png

Below is a docker-compose.yml example taken from the docker docs:

services:
  web:
    build: .
    ports:
    - "5000:5000"
    volumes:
    - .:/code
    - logvolume01:/var/log
    links:
    - redis
  redis:
    image: redis
volumes:
  logvolume01: {}

For clarity's sake, the line build: . in the web section points to the location of the Dockerfile created in step 1.
So how many containers will be created with the yml above? Do they share something, like FS?
T
Thorbjørn Ravn Andersen

A Dockerfile is a text document that contains all the commands/Instruction a user could call on the command line to assemble an image.

Docker 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. By default, docker-compose expects the name of the Compose file as docker-compose.yml or docker-compose.yaml. If the compose file has a different name we can specify it with -f flag.

Check here for more details


D
Dev

docker or more specifically docker engine is used when we want to handle only one container whereas the docker-compose is used when we have multiple containers to handle. We would need multiple containers when we have more than one service to be taken care of, like we have an application that has a client server model. We need a container for the server model and one more container for the client model. Docker compose usually requires each container to have its own dockerfile and then a yml file that incorporates all the containers.