Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker swarm: 'build' configuration in docker compose file ignored during stack deployment

We have created a docker compose file with multiple services. The images for these services are built in runtime using 'build' configuration option. The corresponding Dockerfile(s) are given in the respective directories.

Sample docker compose file...

version: '3'
services:
  db2server:
    build: ./db2server
    ports:
      - "50005:50000"
    command: ["db2start"]
  appruntime:
    build: ./appruntime
    depends_on:
     - db2server

This docker compose file works with docker-compose command.

  • The images are built in runtime from the Dockerfile(s) present in db2server & appruntime directories
  • These images get deployed in the host machine

But when we try to deploy this in a docker swarm, the following error is thrown...

docker stack deploy -c /home/docker/docker-compose.yml app

Ignoring unsupported options: build

Creating network app_default
Creating service app_db2server
failed to create service app_db2server: Error response from daemon: rpc error: code = InvalidArgument desc = ContainerSpec: image reference must be provided

Looks like the 'build' configuration option is ignored during stack deployment in docker swarm.

How can we deploy these services (with build option) defined in docker compose file in a docker swarm.

like image 637
dinup24 Avatar asked Jan 23 '18 07:01

dinup24


People also ask

Can you use Docker compose with Docker Swarm?

Docker 1.13 introduced a new version of Docker Compose. The main feature of this release is that it allow services defined using Docker Compose files to be directly deployed to Docker Engine enabled with Swarm mode. This enables simplified deployment of multi-container application on multi-host.

What is used to deploy stacks on Docker Swarm?

When running Docker Engine in swarm mode, you can use docker stack deploy to deploy a complete application stack to the swarm. The deploy command accepts a stack description in the form of a Compose file. The docker stack deploy command supports any Compose file of version “3.0” or above.

What is the difference between Docker compose and Docker stack?

Docker Compose is an official tool that helps you manage your Docker containers by letting you define everything through a docker-compose. yml file. docker stack is a command that's embedded into the Docker CLI. It lets you manage a cluster of Docker containers through Docker Swarm.

Which is the parent configuration file that is called by Docker first to load the configuration?

The default location of the configuration file on Windows is %programdata%\docker\config\daemon. json . The --config-file flag can be used to specify a non-default location.


3 Answers

Short answer is, you can not use the build command with docker stack deploy.

From the docs:

Note: The docker stack command build option is ignored when deploying a stack in swarm mode with a (version 3) Compose file. The docker stack command accepts only pre-built images.

An alternative is to build the docker image before deploying the your swarm cluster.

Use the docker build command to create the docker image; Push the created image to a (public or private) docker registry; and reference it in your docker compose file.

like image 199
François Maturel Avatar answered Oct 18 '22 21:10

François Maturel


If anyone is still on this you can tag the built image in compose by setting the image parameter along with the build parameter, as you can see in the build section of the docs. So the file should look like:

version: '3'
services:
  db2server:
    image: <your registry here>/db2server
    build: ./db2server
    ports:
      - "50005:50000"
    command: ["db2start"]
  appruntime:
    image: <your registry here>/appruntime
    build: ./appruntime
    depends_on:
     - db2server

then you can do:

docker-compose build
docker-compose push
docker stack deploy -c /home/docker/docker-compose.yml app
like image 27
vlizana Avatar answered Oct 18 '22 20:10

vlizana


The compose file serves both tools: docker-compose cli, and docker stack cli. the "build" options work in docker-compose but are ignored by stack commands, and the "deploy" options work in stack commands but are ignored by docker-compose.

Swarm is not designed to build your images for you. It assumes you're images are available in a image registry. Multiple nodes in a Swarm can't share images with each other, so a registry (either remote or running on the Swarm itself) is the only way they can all ensure they can pull the same exact image.

So the typical example is to either have Docker Hub auto-build your images based on code commits, or have your CI/CD platform build the images and push to a registry. Then your stack deploy commands will pull the proper image from that registry.

like image 11
Bret Fisher Avatar answered Oct 18 '22 20:10

Bret Fisher