Pass host env variable to docker container using Vagrant and Docker Provider - docker

I'm using Vagrant with docker provider to work with Ansible, from a container based on Debian.
Everything works but now, I discover AWS and I would like to add AWS env variables inside this container, using Vagrant.
So, to avoid variables in a file, I create 2 environment variable on my Laptop (ArchLinux) that host the container ran by Vagrant.
printenv: (Archlinux Laptop host)
AWS_ACCESS_KEY_ID=1234
AWS_SECRET_ACCESS_KEY=5678
By checking the Vagrant docker provider, seems be possible to add environment variables by using : d.env {}
So, I have :
config.vm.provider "docker" do |d, docker|
d.name = "Debian-Ansible"
d.image = "my/debian-ansible:v1.2-aws"
d.env = {
"AWS_SECRET_ACCESS_KEY":ENV['AWS_SECRET_ACCESS_KEY'],
"AWS_ACCESS_KEY_ID":ENV['AWS_ACCESS_KEY_ID']
}
And the result inside my container:
$ printenv (inside the Debian-Ansible container)
HOSTNAME=Debian-Ansible
HOME=/home/user
TERM=xterm
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/aurelien/.local/bin
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
PWD=/
PS : I tried different syntax (:, =, =>) with same behavior. The variables are blank.
Could you please advice me about using these environment variables usage ?
Thanks :)

Related

Find out value of environment variables being passed to container by docker-compose

I'm troubleshooting a service that's failing to start because of an environment variable issue. I'd like to check out what the environment variables look like from the inside of the container. Is there a command I can pass in my docker-compose.yaml so that instead of starting the service it prints the relevant environment variable to standard output and exits?
Try this:
docker-compose run rabbitmq env
This will run env inside the rabbitmq service. env will print all environment variables (from the shell).
If the service is already running, you can do this instead, which will run env in a new shell in the existing container (which is faster since it does not need to spin up a new instance):
docker-compose exec rabbitmq env
Get the container ID with docker ps.
Then execute a shell for the running rabbitmq container by running docker exec command with the container id for your rabbitmq container.
Once you are on the rabbitmq container, you can echo out the value of any environment variable like you would on any other linux system. e.g. if you declared ENV DEBUG=true at image build time, then is should be able to retrieve that value with echo $DEBUG in the container. Furthermore, once you are in the container, you can poke around the log files for more investigation.
As others have said, first get the container ID with docker ps. When you have done that, view all the properties with docker inspect <id> and you will see something like:
[
{
...
"Config": {
...
"Env": [
"ASPNETCORE_URLS=http://+:80",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"DOTNET_RUNNING_IN_CONTAINER=true",
"DOTNET_VERSION=6.0.1",
"ASPNET_VERSION=6.0.1",
"Logging__Console__FormatterName=Json"
],
...
}
}
]

Using variables in config file

I have this dockerfile that is working correctly.
https://github.com/shantanuo/docker/blob/master/packetbeat-docker/Dockerfile
The only problem is that when my host changes, I need to modify packetbeat.yml file
hosts: ["https://944fe807b7525eaf163f502e08a412c5.us-east-1.aws.found.io:9243"]
password: "rzmYYJUdHVaglRejr8XqjIX7"
Is there any way to simplify this change? Can I use environment variable to replace these 2 values?
Set environment variables in your docker container first.
You can either set them by accessing your container
docker exec -it CONTAINER_NAME /bin/bash
HOST="https://944fe807b7525eaf163f502e08a412c5.us-east-1.aws.found.io:9243"
PASS="rzmYYJUdHVaglRejr8XqjIX7"
Or in your Dockerfile
ENV HOST https://944fe807b7525eaf163f502e08a412c5.us-east-1.aws.found.io:9243
ENV PASS rzmYYJUdHVaglRejr8XqjIX7
And the in the packetbeat.yml
hosts: ['${HOST}']
password: '${PASS}'

how to pass a hostname as env var with rancher server

I am using docker 1.12 and rancher server 1.5.9. I am trying to create a stack in rancher to deploy and orchestrate my app. My issue is that I need to pass as env var the hostname of the host where the container will be running.
Since I have only one image that will be used to create one kind of container on several host (let's say 2 for the tests) I can't pass it like HOSTNAME=myhostname. The value needs to be a var which will be set with the docker host.
Does anyone know how to do that with the rancher server UI?
Does anyone know how rancher retrieve the hostname when adding a custom host?
Can we use the entry point or CMD to do that?
Having an /etc/hosts on the machine that prioritizes the desired name over localhost helped in my case. Obviously, also have an /etc/hostname that agrees with /etc/hosts.
I am using container linux. So for me, it looks like so in the ct-config before converting to ignition.
storage:
files:
- filesystem: "root"
path: "/etc/hostname"
mode: 0644
contents:
inline: ${hostname}
- filesystem: "root"
path: "/etc/hosts"
mode: 0644
contents:
inline: "127.0.0.1 ${hostname} localhost\n
::1 ${hostname} localhost"
Just be sure to have the above before you run the rancher registration line.
sudo docker run --rm --privileged \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /var/lib/rancher:/var/lib/rancher \
rancher/agent:v1.2.7 https://myrancher/v1/scripts/TOKEN

How can I pass environment variables to a Docker container with Terraform?

I'm spawning Docker containers using Terraform's Docker provider, but fail to understand how to pass them environment variables.
The project's documentation on the env option states :
env - (Optional, set of strings) Environment variables to set.
What is a "set of strings" in context ? Can you provide an example docker container resource declaration that uses the env option ?
From Terraforming a Docker environment:
# Start elastic container
resource "docker_container" "elastic" {
image = "${docker_image.elastic.latest}"
name = "elastic"
hostname = "elastic"
env = ["SERVICE=elastic", "PROJECT=stage", "ENVIRONMENT=operations"]
restart= "no"
must_run="true"
}

Environment variable and docker-compose

When running a number of linked services with docker-compose up, how can I add values for environment variables to be passed to specific containers for services? Say I have a service "webapp" which uses the TIMEOUT environment variable, how do I set TIMEOUT=10000 with docker-compose for the container that "webapp" lives in? I have tried the notation -e TIMEOUT=10000 from the docker command, but this does not seem to work.
with docker compose your have to specify environment variables in docker-compose.yml with env_file or environment configuration commands
https://docs.docker.com/compose/compose-file/#env-file
https://docs.docker.com/compose/compose-file/#environment

Resources