Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker: docker compose file for "docker stack deploy"

I have a docker-compose.yml file which works with docker-compose up --build. My app works and everything is fine.

version: '3'

services:

  myapp:
    container_name: myapp
    restart: always
    build: ./myapp
    ports:
      - "8000:8000"
    command: /usr/local/bin/gunicorn -w 2 -b :8000 flaskplot:app

  nginx:
    container_name: nginx
    restart: always
    build: ./nginx
    ports:
      - "80:80"
    depends_on:
      - myapp

But when I use docker stack deploy -c docker-compose.yml myapp, I get the following error:

Ignoring unsupported options: build, restart

Ignoring deprecated options:

container_name: Setting the container name is not supported.

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

any hints how I should "translate" the docker-compose.yml file to make it compatible with docker stack deploy?

like image 688
xaratustra Avatar asked Jul 21 '18 08:07

xaratustra


People also ask

Which file format is used to deploy docker stacks?

The stack is configured using a docker-compose file. This is a YAML file written in a domain-specific language to specify Docker services, containers and networks. Given a compose file, it is as simple as one command to deploy the stack across an entire swarm of Docker nodes.

Is Docker compose for deployment?

You can use Compose to deploy an app to a remote Docker host by setting the DOCKER_HOST , DOCKER_TLS_VERIFY , and DOCKER_CERT_PATH environment variables appropriately. See also Compose CLI environment variables.

Where do I put my Docker compose file?

The default path for a Compose file is ./docker-compose.yml .


1 Answers

To run containers in swarm mode, you do not build them on each swarm node individually. Instead you build the image once, typically on a CI server, push to a registry server (often locally hosted, or you can use docker hub), and specify the image name inside your compose file with an "image" section for each service.

Doing that will get rid of the hard error. You'll likely remove the build section of the compose file since it no longer applies.

Specifying "container_name" is unsupported because it would break the ability to scale or perform updates (a container name must be unique within the docker engine). Let swarm name the containers and reference your app on the docker network by it's service name.

Specifying "depends_on" is not supported because containers may be started on different nodes, and rolling updates/failure recovery may remove some containers providing a service after the app started. Docker can retry the failing app until the other service starts up, or preferably you configure an entrypoint that waits for the dependencies to become available with some kind of ping for a minute or two.

Without seeing your Dockerfile, I'd also recommend setting up a healthcheck on each image. Swarm mode uses this to control rolling updates and recover from application failures.

Lastly, consider adding a "deploy" section to your compose file. This tells swarm mode how to deploy and update your service, including how many replicas, constraints on where to run, memory and CPU limits and requirements, and how fast to update the service. You can define a restart policy here as well but I recommend against it since I've seen docker engines restarting containers that conflict with swarm mode deploying containers on other nodes, or even a new container on the same node.

You can see the full compose file documentation with all of these options here: https://docs.docker.com/compose/compose-file/

like image 182
BMitch Avatar answered Sep 26 '22 17:09

BMitch