Docker Compose not seeing command? - docker

I am trying to run an exporter this using docker compose. I have created a docker compose file like this :-
version: '2'
services:
prometheus-cloudwatch:
image: cloudposse/prometheus-to-cloudwatch
container_name: prometheus-cloudwatch
command:
- 'AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}'
- 'AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}'
- 'CLOUDWATCH_NAMESPACE=${CLOUDWATCH_NAMESPACE}'
- 'CLOUDWATCH_REGION=${CLOUDWATCH_REGION}'
- 'CLOUDWATCH_PUBLISH_TIMEOUT=15'
- 'PROMETHEUS_SCRAPE_INTERVAL=30'
- 'PROMETHEUS_SCRAPE_URL=http://IP:9399/metrics'
And i have created .env file with the following content :-
AWS_ACCESS_KEY_ID=<ID>
AWS_SECRET_ACCESS_KEY=<ID>
CLOUDWATCH_NAMESPACE=kube-state-metrics
CLOUDWATCH_REGION=us-east-1
When i run the command
docker-compose up -d
and check the running containers, using docker ps -a i get this :-
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cf43bfedef54 cloudposse/prometheus-to-cloudwatch "prometheus-to-cloud…" 7 minutes ago Exited (1) 7 minutes ago prometheus-cloudwatch
And when i further investigate using docker logs prometheus-cloudwatch, I am seeing this :-
2021/07/28 03:15:44 prometheus-to-cloudwatch: Error: -cloudwatch_namespace or CLOUDWATCH_NAMESPACE required
Which does not make sense to me since i have declared the command in the docker-compose file. Any help will be appreciated. Thank you.

See its Dockerfile:
ENTRYPOINT ["prometheus-to-cloudwatch"]
In fact, The command in your docker-compose.yaml will be act as parameters of the entrypoint.
To make the same effect of -e CLOUDWATCH_NAMESPACE mentioned here, you could try next snippet:
version: '2'
services:
prometheus-cloudwatch:
image: cloudposse/prometheus-to-cloudwatch
container_name: prometheus-cloudwatch
env_file: .env
You could refer this for more help.
Another solution is using environment like next, but you need to extract the variable by yourself, detail see this:
environment:
- CLOUDWATCH_NAMESPACE=${CLOUDWATCH_NAMESPACE}

Related

Why container based on ubuntu didn't run from docker-compose file, when that work for similar nginx container? [duplicate]

This question already has answers here:
Ubuntu container keep restarting
(1 answer)
How can I run bash in a new container of a docker image?
(1 answer)
Docker compose detached mode not working
(2 answers)
Closed 1 year ago.
I try to run docker container using docker-compose file instead of a long command line.
I want to run docker-compose file based on ubuntu:latest. Container created but can't run.
version: "3.9"
services:
ubuntu:
image: ubuntu:latest
container_name: nginx_from_scratch3
ports:
- "80:80"
But before I've tried add in my docker-compose file line
command: bash
And noting change. I think what after running container continue to work. But that didn't happend.
But on the other side if I use nginx image all run perfectly.
version: "3.9"
services:
nginx1:
image: nginx
container_name: nginx_from_scratch4
ports:
- "80:80"
Why docker-compose file for nginx image work, and doesn’t work for ubuntu image.
Docker container exits if task inside is done. So when you run nginx, it starts nginx automatically and keep it alive. As for ubuntu, there's no any task to keep running and container ended immediately. So if you want to keep it alive even if it does not have any job: add tail -f, like this:
version: "3.9"
services:
ubuntu:
image: ubuntu
command: tail -F anything
After you do docker container ps you will see it running.
And you can move to it with
docker exec -it container_name bash

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:

Docker container log does not appear anymore on Docker compose log

I'm running my Docker container through my Docker compose, but when my container stops and it restarts again, the log does not appear anymore related to this restarted container.
Would anyone know how to fix it?
I send below the docker compose command and the file for analysis.
Thank you in advance.
Command to start the compose
docker-compose -f docker-compose.dev.yml up
Docker compose
version: '3'
services:
ms3_executive_back:
image: ms3_executive_backend
ports:
- "5001:5001"
volumes:
- ./executive_backend:/app
restart: always
If you want to inspect the logs to determine cause of failure, you can try setting your restart: "no". This will ensure that docker-compose does not automatically restart your container and overwrite the existing logs.

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.

Setting arguments in docker-compose file

Hi I want to use the haproxy exporter provided here https://github.com/prometheus/haproxy_exporter in a docker container.
I am using docker-composefor managing containers and want to recreate this command:
$ docker run -p 9101:9101 prom/haproxy-exporter -haproxy.scrape-uri="http://user:pass#haproxy.example.com/haproxy?stats;csv"
in my docker-compose.yml.
I am not sure how to pass the argument, after viewing the docker-compose documentation I tried it like this:
haproxy-exporter:
image: prom/haproxy-exporter
ports:
- 9101:9101
network_mode: "host"
build:
args:
- haproxy.scrape-uri="http://user:pass#haproxy.example.com/haproxy?stats;csv"
But this gives me an file is invalid message because it requires a context with a build.
Thanks for any help in advance
The image is already built and pulled from the hub (unless you have your own Dockerfile) so you don't need the build option. Instead, pass your arguments as the command since the image appears to use an entrypoint (if their Dockerfile only had a cmd option, then you'd need to also pass /bin/haproxy-exporter in your command):
haproxy-exporter:
image: prom/haproxy-exporter
ports:
- 9101:9101
network_mode: "host"
command: -haproxy.scrape-uri="http://user:pass#haproxy.example.com/haproxy?stats;csv"

Resources