Calling redis-cli in docker-compose setup - docker

I run the official Redis image https://hub.docker.com/_/redis/ in a docker-compose setup.
myredis:
image: redis
How can run redis-cli with docker-compose on that image?
I tried the following, but it didn't connect:
docker-compose run myredis redis-cli
> Could not connect to Redis at 127.0.0.1:6379: Connection refuse
The docs of the image says that I should run:
docker run -it --rm \
--link some-redis:redis \
redis \
sh -c 'exec redis-cli -h "$REDIS_PORT_6379_TCP_ADDR" -p "$REDIS_PORT_6379_TCP_PORT"'
How does this translate to docker-compose run?

That would override the default CMD [ "redis-server" ]: you are trying to run redis-cli on a container where the redis-server was never executed.
As mentioned here, you can also test with:
docker exec -it myredis redis-cli
From docker-compose, as mentioned in this docker/compose issue 2123:
rcli:
image: redis:latest
links:
- redis
command: >
sh -c 'redis-cli -h redis '
This should also works:
rcli:
image: redis:latest
links:
- redis
command: redis-cli -h redis
As the OP ivoba confirms (in the comments), the last form works.
Then:
docker-compose run rcli
ivoba also adds:
docker-compose run redis redis-cli -h redis works also when the containers are running.
This way its not necessary to declare a separate rcli container.

You can also use this command:
docker-compose run myredis redis-cli -h myredis

I followed as #VonC suggest, but in my case I run redis on predefined network so it did not worked.
So in the case redis container run in specific network, network field should be specified in docker-compose.yaml file
rcli:
image: redis:latest
links:
- redis
command: redis-cli -h redis
networks:
- <network redis run on>

Related

Starting docker containers

I have a docker-compose.yml file that starts two services: amazon/dynamodb-local on 8000 port and django-service. django-service runs tests that are dependent on dynamodb-local.
Here is working docker-compose.yml:
version: '3.8'
services:
dynamodb-local:
image: "amazon/dynamodb-local:latest"
container_name: dynamodb-local
ports:
- "8000:8000"
django-service:
depends_on:
- dynamodb-local
image: django-service
build:
dockerfile: Dockerfile
context: .
env_file:
- envs/tests.env
volumes:
- ./:/app
command: sh -c 'cd /app && pytest tests/integration/ -vv'
Now I need to run this without docker-compose, only using docker itself. I try to do following:
docker network create -d bridge net // create a network for dynamodb-local and django-service
docker run --network=net --rm -p 8000:8000 -d amazon/dynamodb-local:latest // run cont. att. to network
docker run --network=net --rm --env-file ./envs/tests.env -v `pwd`:/app django-service /bin/sh -c 'env && cd /app && pytest tests/integration -vv'
I can see that both services start, but I can't connect to the dynamo-db.
Where is the problem? Any comment or help is appreciated!
Through the docker-compose.yml, the amazon/dynamodb-local container has a name defined (container_name: dynamodb-local, If we do not set this property, docker-compose will use the service's name as container name). This enables other containers in the same network to address the container through its name.
In the docker-run command, we do not set an explicit container name. We can set an explicit container name by executing docker run ... --name dynamodb-local .... More details can be found in the corresponding docker run documentation.

How to convert a docker run -it bash command into a docker-compose?

Given the following command:
docker run -dit -p 9080:9080 -p 9443:9443 -p 2809:2809 -p 9043:9043 --name container_name --net=host myimage:latest bash
How to convert it into an equivalent docker-compose.yml file?
In docker-compose in -it flags are being reflected by following:
tty: true
stdin_open: true
Equivalent to docker run --net=host is this:
services:
web:
...
networks:
hostnet: {}
networks:
hostnet:
external: true
name: host
So your final docker-compose should look like this:
version: '3'
services:
my_name:
image: myimage:latest
container_name: my_name
ports:
- "9080:9080"
- "9443:9443"
- "2809:2809"
- "9043:9043"
command: bash
tty: true
stdin_open: true
networks:
hostnet: {}
networks:
hostnet:
external: true
name: host
Compose file version 3 reference
Last but not least if you want to run it in the detached mode just add -d flag to docker-compose command:
docker-compose up -d
You can’t directly. Docker Compose will start up some number of containers that are expected to run more or less autonomously, and there’s no way to start typing commands into one of them. (What would you do if you had multiple containers that you wanted to start that were all just trying to launch interactive bash sessions?)
A better design would be to set up your Docker image so that its default CMD launched the actual command you were trying to run.
FROM some_base_image:x.y
COPY ...
CMD myapp.sh
Then you should be able to run
docker run -d \
-p 9080:9080 \
-p 9443:9443 \
-p 2809:2809 \
-p 9043:9043 \
--name container_name \
myimage:latest
and your application should start up on its own, successfully, with no user intervention. That’s something you can translate directly into Docker Compose syntax and it will work as expected.

Executing logstash command outside of docker container

I want to execute a Logstash command to start importing to Elasticsearch without entering the ELK Docker container.
This doesn't work:
docker exec -it docker_elk_1 opt/logstash/bin/logstash -f /home/configs/logstash-logs.config
Although it would show
Successfully started Logstash API endpoint {:port=>9600} but it would just exit after.
However, this would work, but I have to enter docker container first
docker exec -it docker_elk_1 bin/bash
Then
opt/logstash/bin/logstash -f /home/configs/logstash-logs.config
Thanks
docker-compose.yml
elk:
image: sebp/elk
volumes:
- ${PWD}:/home/configs
ports:
- "5601:5601"
- "9200:9200"
- "5044:5044"
I do not know if I understand...you can try with:
docker exec -it docker_elk_1 /bin/bash -c 'opt/logstash/bin/logstash -f /home/configs/logstash-logs.config'
or
docker-compose exec /bin/bash -c 'opt/logstash/bin/logstash -f /home/configs/logstash-logs.config'

docker-compose environment not the same as Docker -e

I am using docker-compose file and want to add some ENV variables to it, which are not related to redis itself.
redis-master:
environment:
- REDIS_REPLICATION_MODE=master
- ALLOW_EMPTY_PASSWORD=yes
# Domains
- VIRTUAL_HOST=redis-master.xxx.com
- VIRTUAL_PORT=6379
ports:
- '6379:6379'
expose:
- '6379'
image: bitnami/redis:latest
But the problem is that this two ENV were not added to Docker:
VIRTUAL_HOST and VIRTUAL_PORT
If I am doing like
docker run -d -p 6379:6379 --name redis-master -e VIRTUAL_PORT=6379 --expose 6379 -e VIRTUAL_HOST=redis-master.xxx.com bitnami/redis:latest
then I can see this two ENV. Why? What is the difference?
I used your Compose file and I can see the ENVs:
➜ ~ docker-compose up -d
prometherion_redis-master_1 is up-to-date
➜ ~ docker-compose exec redis-master sh
$ env | grep -i virtual
VIRTUAL_HOST=redis-master.xxx.com
VIRTUAL_PORT=6379
If you want to be sure that ENVs are injected: docker inspect <container_id> | jq '.[0].Config.Env' (you need jq installed)

Execute command in linked docker container

Is there any way posible to exec command from inside one docker container in the linked docker container?
I don't want to exec command from the host.
As long as you have access to something like the docker socket within your container, you can run any command inside any docker container, doesn't matter whether or not it is linked. For example:
# run a container and link it to `other`
docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock \
--link other:other myimage bash -l
bash$ docker exec --it other echo hello
This works even if the link was not specified.
With docker-compose:
version: '2.1'
services:
site:
image: ubuntu
container_name: test-site
command: sleep 999999
dkr:
image: docker
privileged: true
working_dir: "/dkr"
volumes:
- ".:/dkr"
- "/var/run/docker.sock:/var/run/docker.sock"
command: docker ps -a
Then try:
docker-compose up -d site
docker-compose up dkr
result:
Attaching to tmp_dkr_1
dkr_1 | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dkr_1 | 25e382142b2e docker "docker-entrypoint..." Less than a second ago Up Less than a second tmp_dkr_1
Example Project
https://github.com/reduardo7/docker-container-access
As "Abdullah Jibaly" said you can do that but there is some security issues you have to consider, also there is sdk docker to use, and for python applications can use Docker SDK for Python

Resources