Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between `docker-compose build` and `docker build`?

What is the difference between docker-compose build and docker build?

Suppose in a dockerized project path there is a docker-compose.yml file:

docker-compose build 

And

docker build 
like image 279
Benyamin Jafari Avatar asked May 08 '18 09:05

Benyamin Jafari


People also ask

What is the difference between Docker compose and docker?

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.

What is Docker compose build used for?

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.

Does docker build use Docker compose yml?

How Docker Compose works. To put it simply: Docker Compose builds a stack of applications to run a complete service. The docker-compose. yml file is broken into sections, each section represents a single container which, when combined with the other containers, create the service.

What is the difference between Docker compose up and Docker compose start?

docker-compose up : Builds, (re)creates, and starts containers. It also attaches to containers for a service. docker-compose start : Start the stopped containers, can't create new ones.


1 Answers

docker-compose can be considered a wrapper around the docker CLI (in fact it is another implementation in python as said in the comments) in order to gain time and avoid 500 characters-long lines (and also start multiple containers at the same time). It uses a file called docker-compose.yml in order to retrieve parameters.

You can find the reference for the docker-compose file format here.

So basically docker-compose build will read your docker-compose.yml, look for all services containing the build: statement and run a docker build for each one.

Each build: can specify a Dockerfile, a context and args to pass to docker.

To conclude with an example docker-compose.yml file :

version: '3.2'  services:   database:     image: mariadb     restart: always     volumes:       - ./.data/sql:/var/lib/mysql    web:     build:       dockerfile: Dockerfile-alpine       context: ./web     ports:       - 8099:80     depends_on:       - database  

When calling docker-compose build, only the web target will need an image to be built. The docker build command would look like :

docker build -t web_myproject -f Dockerfile-alpine ./web 
like image 156
hugoShaka Avatar answered Oct 09 '22 09:10

hugoShaka