newbie
newbie

Reputation: 35

Spinning docker image into multiple containers

I am working on building automated CI/CD pipeline for LAMP application using docker.

I want image to be spinned into 5 containers, so that 5 different developers can work on their code. Can this be atained? I tried it using replicas, but it didnt worked out.

version: '3'
services:
  web:
   build: .
   ports:
    - "8080:80"#    
deploy:
      mode: replicated
      replicas: 4

Error which i get:

:#!/bin/bash -eo pipefail docker-compose up ERROR: The Compose file './docker-compose.yml' is invalid because: Additional properties are not allowed ('jobs' was unexpected) You might be seeing this error because you're using the wrong Compose file version. Either specify a supported version (e.g "2.2" or "3.3") and place your service definitions under the services key, or omit the version key and place your service definitions at the root of the file to use version 1. For more on the Compose file format versions, see docs.docker.com/compose/compose-file Exited with code 1 –

Also, from different container, can developers push, pull and commit to git? Will work done in one container will get lost if image is rebuild or run?

What things should i actually take care of while building this pipeline.

Upvotes: 1

Views: 1556

Answers (1)

Saqib Ahmed
Saqib Ahmed

Reputation: 1125

First of all, build your image separately using a Dockerfile with docker build -t <image name>:<version/tag> . then use following compose file with docker stack deploy to deploy your stack.

version: '3'
services:
  web:
   image: <image name>:<version/tag>
   ports:
    - "8080:80"#    
   deploy:
      mode: replicated
      replicas: 4

deploy attribute should be inside a service because it describes the number of replicas a service must have. It is not a global attribute like services. That seems to be the only problem in your compose file and docker compose up is complaining about this when running from the pipeline.

Update

You cannot run multiple replicas with a single docker-compose command. To run multiple replicas from a compose.yml, create a swarm by executing docker swarm init on your machine.

Afterward, simply replace docker-compose up with docker stack deploy <stack name>. docker-compose simply ignores the deploy attribute.

For details on differences between docker-compose up and docker stack deploy <stack name> refer to this article: https://nickjanetakis.com/blog/docker-tip-23-docker-compose-vs-docker-stack

Upvotes: 1

Related Questions