Deploying multiple container with same code (Docker) - 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"

Related

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

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:

couchbase command while docker setup

I am running couchbase using docker[on windows]. When I start it ask for doing cluster setup.
I want to customize docker-compose file to do below setup
1. set a cluster name
2. set up a admin account
3. create a empty bucket.
My docker compose file
version: '3'
services:
couchbase:
image: couchbase/server
ports:
- 11210:11210
- "8091-8094:8091-8094"
volumes:
- /opt/couchbase/data:/opt/couchbase/var
env_file: .env
You can read about a solution here using Couchbase with Docker Compose - behind the scenes it uses REST API calls to initialize the database and create buckets.
His docker file includes a configuration script to this:
FROM couchbase/server:enterprise-4.5.0-DP1
COPY configure-node.sh /opt/couchbase
CMD ["/opt/couchbase/configure-node.sh"]
You can find contents of the script here on Github as a good starting point.

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.

How to set environment variable into docker container using docker-compose

I want to set credentials to use Google Translate Api Client so I have to set environment variable GOOGLE_APPLICATION_CREDENTIALS that value is path to credential file (from Google Cloud).
When I have been used docker build and docker run it was pretty easy.
I have been used docker run
--env GOOGLE_APPLICATION_CREDENTIALS=/usr/src/app/CryptoTraderBot-901d31d199ce.json and environment variable has been set.
More difficult things come when I tried to set it in docker-compose. I have to use docker-compose because I need few containers so it is only way to achieve this.
Based on Docker compose environment variables documentation I created my docker-compose.yml file that looks like this:
version: "3"
services:
redis:
image: redis:4-alpine
crypto-bot:
build: .
depends_on:
- redis
environment:
- GOOGLE_APPLICATION_CREDENTIALS = /usr/src/app/CryptoTraderBot-901d31d199ce.json
I also have been tried multiple combination of path to .json file but none of this has been worked properly.
Have you got any idea how can I set it properly ?
While creating this question I have been resolve this problem in a funny and easy way but I have been thought that I post answer to help someone in the future with similiar problem.
All you have to do is remove " " (space) next = sign so two last lines of docker-compose.yml should looks like this:
environment:
- GOOGLE_APPLICATION_CREDENTIALS=/usr/src/app/CryptoTraderBot-901d31d199ce.json
Docker Compose has a newer feature called secrets. You can bind the credentials like this:
services:
secret-service:
build:
context: secret-service
environment:
- GOOGLE_APPLICATION_CREDENTIALS=/run/secrets/gcp-credentials
secrets:
- gcp-credentials
secrets:
gcp-credentials:
file: ./gcp-credentials.json
Reference: https://docs.docker.com/compose/compose-file/#secrets

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