How to `--scale` from within docker-compose file - docker

Let's say I have the following docker-compose.yml file:
version: '3'
services:
load-balancer:
...
...
web-application:
...
...
If I want to run this with 5 replicas of web-application, I have to issue this command:
docker-compose up --scale web-application=5
Is there any way to tell Docker to do the --scale web-application=5 bit from within the docker-compose.yml file?

You can specify the number of replicas in docker-compose only when working in swarm mode.
See the deploy directive.
Example:
services:
redis:
image: redis:latest
deploy:
replicas: 2
From the documentation:
This only takes effect when deploying to a swarm with
docker stack deploy, and is ignored by docker-compose up
and docker-compose run.
Lastly, there is some discussion about this feature (since it used to be possible) in this GitHub issue, and here is the mention in the most recent compose spec.

I tested replicas in docker compose file, the code that worked for me is the following.
You should use docker-compose up -d to execute the configuración
You should see the next results.
The results in web browser should be the following:

Related

Deploying multiple container with same code (Docker)

I have a custom build image (Python + project) which is working fine, I just want to deploy multiple container using docker-compose only environment variables will be changing rest is same can it be done from one service in docker-compose.yml
docker-compose.yml
version: '3'
services:
logsService:
image: logs:latest # logs image is already built and working fine
env_file:
- /home/controller/test/.env # File contains url, api_key, api_secret
by using docker-compose up I can provision one container but I want to provision 4 container with same code but different env variables (i.e. url, api_key, api_secret), any ideas how to achieve this will be appriciated
NOTE: I am new to docker, please pardon me if this is a silly question.
you can do this with the docker-compose command
docker-compose --scale --name="numberofcontainers"
so for if I wanted to launch my "webserver" container 3 times it would be
docker-compse --scale --webserver=3
as you have said you are new I would advise looking into a load balancer aswell. Or your other containers will be usless
So, something like this perhaps:
version: '3'
services:
logsService:
image: logs:latest
env_file:
- /home/controller/test/${ENV:-}.env
ENV=prod docker-compose --project-name prod up
assuming that your production config is in "prod.env"

How to optionally start docker-compose dependent container?

I have a docker-compose project which (simplified) looks like:
version: '3'
services:
main:
image: main-image
depends_on:
- my_service
my_service:
image: very-big-image
The functionality supplied by my-service is also available in the cloud. Running it locally is faster, but demands extreme CPU and disk resources. Therefore, I will sometimes want to run just main, and other times will want to run both containers.
I don't want to duplicate the docker-compose code; the full container definitions are much more complex than the simplified version above.
I thought I could put each service in its own .yml file and then do docker-compose -f main.yml -f my_service.yml up, but that fails on the depends-on line, which seems to require the dependency to be in the same file.
For now, I have an ugly solution. I split into two (with the second files starting with the depends_on line. I then use a shell script that knows which configuration I want to run, and either runs the first file or concatenates the two files together. This works, I guess; but is ugly.
What is the right way to do this?
Try this:
docker-compose start <container_name>
This command only starts a selected container.
What is the right way to do this?
I think you've already identified it: split the service definitions between two files, so that sometimes you can start both:
docker-compose -f main.yml -f service.yml up
And sometimes you can start just the service container:
docker-compose -f service.yml up
This works fine, and the depends_on key does not require that all definitions be in the same file. The dictionaries are merged before docker-compose tries to resolve dependencies.
For example, if I have in main.yml:
---
version: "3"
services:
main:
image: alpine
command: sleep 30
depends_on:
- service
And in service.yml:
---
version: "3"
services:
service:
image: alpine
command: sleep 30
I can start just the service container like this:
$ docker-compose -f service.yml up
Starting compose_service_1 ... done
Attaching to compose_service_1
Or I can start both the main and service containers:
$ docker-compose -f main.yml -f service.yml up
Starting compose_service_1 ... done
Starting compose_main_1 ... done
Attaching to compose_service_1, compose_main_1
If you're seeing behavior that appears to contradict this, it would help if you were to update your question with minimal examples that reproduce the problem, and include any error output from running docker-compose.

Start particular service from docker-compose

I am new to Docker and have docker-compose.yml which is containing many services and iI need to start one particular service. I have docker-compose.yml file with information:
version: '2'
services:
postgres:
image: ${ARTIFACTORY_URL}/datahub/postgres:${BUILD_NUMBER}
restart: "no"
volumes:
- /etc/passwd:/etc/passwd
volumes_from:
- libs
depends_on:
- libs
setup:
image: ${ARTIFACTORY_URL}/setup:${B_N}
restart: "no"
volumes:
- ${HOME}:/usr/local/
I am able to call docker-compose.yml file using command:
docker-compose -f docker-compose.yml up -d --no-build
But I need to start "setup service" in docker-compose file:
How can I do this?
It's very easy:
docker compose up <service-name>
In your case:
docker compose -f docker-compose.yml up setup -d
To stop the service, then you don't need to specify the service name:
docker compose down
will do.
Little side note: if you are in the directory where the docker-compose.yml file is located, then docker-compose will use it implicitly, there's no need to add it as a parameter.
You need to provide it in the following situations:
the file is not in your current directory
the file name is different from the default one, eg. myconfig.yml
As far as I understand your question, you have multiple services in docker-compose but want to deploy only one.
docker-compose should be used for multi-container Docker applications. From official docs :
Compose is a tool for defining and running multi-container Docker
applications.
IMHO, you should run your service image separately with docker run command.
PS: If you are asking about recreating only the container whose image is changed among the multiple services in your docker-compose file, then docker-compose handles that for you.

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.
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

Docker Swarm - Using mongo service only in local enviroment

So i learned that you can use the same docker-compose file for both local development with docker-machine and for production environment with docker swarm, which is great think, you can also share the same docker-compose file and add specific configuration for production/local enviroment.
One think i couldnt find is a way to define a service only on local enviroment, i have compose file locally which contain redis/node/nginx/mongo containers BUT in production i dont want to use docker for mongo since i use external server(Atlas/ MLab), is it possible to do so and keep using the same files for production & development ?
Docker compose has a solution for the different environment compose files Multiple Compose files. You basically extract the differences into a sub compose file and when running you can merge the compose files:
docker-compose.yml
version: '3'
services:
redis:
...
node:
...
nginx:
...
docker-compose.local.yml
...
services:
mongo:
...
When running locally you execute:
docker-compose -f docker-compose.yml -f docker-compose.local.yml up -d
And when deploying to Swarm you just execute:
docker stack deploy --compose-file docker-compose.yml

Resources