docker-compose environment not the same as Docker -e - docker

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)

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.

Running Echoip Docker Image

I'm new to Docker and having trouble running the docker image https://github.com/mpolden/echoip#docker-image. What am I doing wrong? Any help would be greatly appreciated!
$ docker run mpolden/echoip -a ./GeoLite2-ASN.mmdb -c ./GeoLite2-City.mmdb -f ./GeoLite2-Country.mmdb
echoip: open ./GeoLite2-Country.mmdb: no such file or directory
The files are in the same directory. To test on your end, download the files: GeoLite2-ASN.mmdb, GeoLite2-City.mmdb, GeoLite2-Country.mmdb: https://gofile.io/d/G4i6hb
Having a docker-compose.yml would make this much easier to run:
version: "3.7"
services:
echoip:
image: mpolden/echoip
command: "echoip -a ./GeoLite2-ASN.mmdb -c ./GeoLite2-City.mmdb -f ./GeoLite2-Country.mmdb"
ports:
- "8080:8080"
restart: unless-stopped
The files are in the same directory
Docker containers cannot access the host filesystem unless it is mounted as a volume. For example, you could mount the current directory to /data in the container...
docker run --rm -v "${PWD}:/data" -p 8080:8080 mpolden/echoip \
-a /data/GeoLite2-ASN.mmdb \
-c /data/GeoLite2-City.mmdb \
-f /data/GeoLite2-Country.mmdb \
-l 0.0.0.0:8080
A Docker Compose config might look like this
version: "3.8"
services:
echoip:
image: mpolden/echoip
command: >
-l 0.0.0.0:8080
-a /data/GeoLite2-ASN.mmdb
-c /data/GeoLite2-City.mmdb
-f /data/GeoLite2-Country.mmdb
ports:
- "8080:8080"
volumes:
- "./:/data"
restart: unless-stopped

InfluxDB on Docker-Compose can't read SSL cert file

I'm having some troubles trying to configure SSL with InfluxDB v1.8 running on Docker Compose.
I followed the official documentation to enable HTTPS with self-signed certificate, but the container crashes with the following error:
run: open server: open service: open "/etc/ssl/influxdb-selfsigned.crt": no such file or directory
It works if I run this configuration using docker run command:
docker run -p 8086:8086 -v $PWD/ssl:/etc/ssl \
-e INFLUXDB_DB=db0 \
-e INFLUXDB_ADMIN_USER=admin \
-e INFLUXDB_ADMIN_PASSWORD=supersecretpassword \
-e INFLUXDB_HTTP_HTTPS_ENABLED=true \
-e INFLUXDB_HTTP_HTTPS_CERTIFICATE="/etc/ssl/influxdb-selfsigned.crt" \
-e INFLUXDB_HTTP_HTTPS_PRIVATE_KEY="/etc/ssl/influxdb-selfsigned.key" \
-d influxdb
My docker-compose.yml is the following:
version: "3"
services:
influxdb:
image: influxdb
ports:
- "8086:8086"
volumes:
- influxdb:/var/lib/influxdb
- ./ssl:/etc/ssl/
environment:
- INFLUXDB_DB=db0
- INFLUXDB_ADMIN_USER=admin
- INFLUXDB_ADMIN_PASSWORD=supersecretpassword
- INFLUXDB_HTTP_HTTPS_ENABLED=true
- INFLUXDB_HTTP_HTTPS_CERTIFICATE="/etc/ssl/influxdb-selfsigned.crt"
- INFLUXDB_HTTP_HTTPS_PRIVATE_KEY="/etc/ssl/influxdb-selfsigned.key"
- INFLUXDB_HTTP_AUTH_ENABLED=true
volumes:
influxdb:
If I set INFLUXDB_HTTP_HTTPS_ENABLED to false, I can see that cert and key files are mounted as they should in /etc/ssl in the container ( docker exec -it airq_influxdb_1 ls -la /etc/ssl )
Do you have any idea why this is happening and how to solve it?
The environment variables passed in the docker-compose.yml are strings. You don't need to pass the quotes.
The influx DB is looking for the certificate under "/etc/ssl/influxdb-selfsigned.crt"...literally
Simply remove the quotes and the DB will start:
...
- INFLUXDB_HTTP_HTTPS_ENABLED=true
- INFLUXDB_HTTP_HTTPS_CERTIFICATE=/etc/ssl/influxdb-selfsigned.crt
- INFLUXDB_HTTP_HTTPS_PRIVATE_KEY=/etc/ssl/influxdb-selfsigned.key
...

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.

Calling redis-cli in docker-compose setup

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>

Resources