Environment variables are not getting created inside docker container - docker

I am using Docker for Windows (2.2.0.5) on my Windows 10 Pro system.
I have created and build the docker image for my dotnet core app (SDK 3.1).
This app is connecting with external MySQL server to fetch data.
The app inside docker container is able to connect with database with hardcoded connection string. But not able to connect with arguments passed using -e flag. Upon investigation i figured out the environment variables are not getting created inside docker container.
Below is my docker run command -
docker run -d -p 5003:80 --name price-cat pricingcatalog:latest -e DB_HOST=165.202.xx.xx -e DB_DATABASE=pricing_catalog -e DB_USER=my-username -e DB_PASS=my-password
I am printing all environment variables created with container using C# code -
Console.WriteLine("All environment variables....Process");
foreach(DictionaryEntry envVar in Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Process)){
Console.WriteLine("key={0}, value={1}", envVar.Key, envVar.Value);
}
Console.WriteLine("============================");
Console.WriteLine("All environment variables....User");
foreach(DictionaryEntry envVar in Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User)){
Console.WriteLine("key={0}, value={1}", envVar.Key, envVar.Value);
}
Console.WriteLine("============================");
Console.WriteLine("All environment variables....Machine");
foreach(DictionaryEntry envVar in Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine)){
Console.WriteLine("key={0}, value={1}", envVar.Key, envVar.Value);
}
Console.WriteLine("============================");
Below is what i am getting -
Is there anything i am missing out here.

Remember that all docker cli arguments must come before the image name. Anything after the image name is passed into the image as the command. If you're expecting those -e ... argument to set environment variables, they need to come before the image name:
docker run -d -p 5003:80 --name price-cat \
-e DB_HOST=165.202.xx.xx \
-e DB_DATABASE=pricing_catalog \
-e DB_USER=my-username \
-e DB_PASS=my-password \
pricingcatalog:latest

Related

share a directory between a docker pgadmin container and windows10

From within my docker pgadmin container, I want to access a postgresql backup file located in my windows10 OS.
So I'm trying to set up a shared directory.
Running this command works fine. Directory is linked to the container.
docker run --name=windows10 -d -v C:\Users\johndoe:/windows10 -p 5554:80 dpage/pgadmin4 -e PGADMIN_DEFAULT_EMAIL=john#doe.com -e PGADMIN_DEFAULT_PASSWORD=whatever
However, the directory won't mount because it's giving this error log on startup:
You need to specify PGADMIN_DEFAULT_EMAIL and PGADMIN_DEFAULT_PASSWORD environment variables
What is this sorcery??
Move the environment variables to before the image name
docker run --name=windows10 -d -v C:\Users\johndoe:/windows10 -p 5554:80 -e PGADMIN_DEFAULT_EMAIL=john#doe.com -e PGADMIN_DEFAULT_PASSWORD=whatever dpage/pgadmin4
-e is an option and must be specified between run and IMAGE (see https://docs.docker.com/engine/reference/commandline/run/)

Confluent Schema Registry Docker image not exposing port 8081 outside the container

I'm running the following container using the docker image for the Confluent Schema Registry. Everything runs fine inside the container meaning I can run a shell command inside the container against localhost:8081/subjects and get an empty list back as expected.
However, I'm trying to spin up the Schema Registry in a container just so I could build an application locally that points to this schema registry instance. So I tried exposing port 8081 to my local machine. But localhost:8081 is not accessible from my machine. Is there no way to do what I'm trying to do here? I tried running the schema registry without docker on my windows machine but I didn't see a windows specific schema-registry-start file.
docker run -d \
--net=host \
--add-host=linuxkit-00155da9f301:127.0.0.1 \
-p 8081:8081 \
--name=schema-registry \
-e SCHEMA_REGISTRY_KAFKASTORE_CONNECTION_URL=iptozookeepernode1:2181,iptozookeepernode2:2181 \
-e SCHEMA_REGISTRY_HOST_NAME=localhost \
-e SCHEMA_REGISTRY_LISTENERS=http://localhost:8081 \
-e SCHEMA_REGISTRY_DEBUG=true \
confluentinc/cp-schema-registry:latest
For me, the issue was related to port 8081 on localhost (used by McAfee), changed port mapping to 8017:8081 and it's working fine.

How to set docker env file that inside the image

i am a totally docker newb, so sorry for that
i have stand-alone docker image (some node app),
that i want to run in different environments.
i want to set up the env file with run RUN --env-file <path>
How ever, i want to use the env files that inside the image (so i can use different files per env),
and not on server.
so would be the path inside image.
is there any way to do so?
perhaps like "cp" (docker cp [OPTIONS] CONTAINER:<path>)
but doesn't seem to work.
what the best practice here?
am i making sense?
Thanks!!
Docker bind mounts are a fairly effective way to inject configuration files like this into a running container. I would not try to describe every possible configuration in your built image; instead, let that be configuration that's pushed in from the host.
Pick some single specific file to hold the configuration. For the sake of argument, let's say it's /usr/src/app/env. Set up your application however it's built to read that file at startup time. Either make sure the application can still start up if the file is missing, or build your image with some file there with reasonable default settings.
Now when you run your container, it will always read settings from that known file; but, you can specify a host file that will be there:
docker run -v $PWD/env.development:/usr/src/app/env myimage
Now you can locally have an env.development that specifies extended logging and a local database, and an env.production with minimal logging and pointing at your production database. If you set up a third environment (say a shared test database with some known data in it) you can just run the container with this new configuration, without rebuilding it.
Following is the command to run docker
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Example
docker run --name test -it debian
focus on following switch
--env , -e Set environment variables
--env-file You can pass environment variables to your containers with the -e flag.
An example from a startup script:
sudo docker run -d -t -i -e REDIS_NAMESPACE='staging' \
-e POSTGRES_ENV_POSTGRES_PASSWORD='foo' \
-e POSTGRES_ENV_POSTGRES_USER='bar' \
-e POSTGRES_ENV_DB_NAME='mysite_staging' \
-e POSTGRES_PORT_5432_TCP_ADDR='docker-db-1.hidden.us-east-1.rds.amazonaws.com' \
-e SITE_URL='staging.mysite.com' \
-p 80:80 \
--link redis:redis \
--name container_name dockerhub_id/image_name
In case, you have many environment variables and especially if they're meant to be secret, you can use an env-file:
$ docker run --env-file ./env.list ubuntu bash
The --env-file flag takes a filename as an argument and expects each
line to be in the VAR=VAL format, mimicking the argument passed to
--env. Comment lines need only be prefixed with #

java running inside docker container cannot see environment variables

I am new with Docker. I have a small Java application that I am trying to run inside Docker. I have created a Dockerfile to build the image.
My application is reading Environment Variables to know which database to connect to.
When running the command
docker run -d -p 80:80 occm -e "MYSQL_USER=user" -e "MYSQL_PASSWORD=password" -e "MYSQL_PORT=3306" -e "MYSQL_HOST=somehost"
and then enumerating all the variables using System.getenv, I dont see any of them. So I have added to the Docker file
ENV MYSQL_HOST=localhost
now when I run the container I see this variable, but I see it with the localhost value and not somehost.
What am I doing wrong?
The problem is how you are running your docker image.
$ docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
So, you are passing -e "..." -e "..." as command and arguments
You need to use -e as [OPTIONS].
$ docker run -d -p 80:80 -e "MYSQL_USER=user" -e "MYSQL_PASSWORD=password" -e "MYSQL_PORT=3306" -e "MYSQL_HOST=somehost" occm

Connection string in build variable VSTS

I am trying to start my docker image from a linux shell using build variables that pass into environment variables for the connection strings. When I start the app in the container it reports a malformed connection string. App runs when I compile it with the connection string hard coded so I know it works. I'm sure i'm probably not escaping the ; correctly or something like that. I notice that it just dumps each thing after ; on a new line in the VSTS log.
These are the Variables I created in VSTS
ConnString1 "Server=172.17.0.4\;Port=5432\;Database=dbname\;User Id=userid\;Password=mypassword\;"
ConnString2 "Server=172.17.0.4\;Port=5432\;Database=dbname2\;User Id=userid\;Password=mypassword\;"
This is my SSH command
docker image pull mydockername/myimage
docker run -d -e ConnString1=$(ConnString1) -e ConnString2=$(ConnString2) -v /home/mylinuxuser/CONFIGS/LIVE:/bin/Debug/netcoreapp2.0/publish/Configs --restart always -p 5000:5000 --name containername mydockername/myimage
This is a snippet of the output
2017-11-01T15:21:40.7137030Z Current agent version: '2.120.1'
[CONNSTRING1] --> ["Server=172.17.0.4\;Port=5432\;Database=dbname\;User Id=userid\;Password=mypassword\;"]
[CONNSTRING2] --> ["Server=172.17.0.4\;Port=5432\;Database=dbname2\;User Id=userid\;Password=mypassword\;"]
2017-11-01T15:21:43.2862730Z docker run -d -e ConnString1="Server=172.17.0.4\;Port=5432\;Database=dbname\;User Id=userid\;Password=mypassword\;" -e ConnString2="Server=172.17.0.4\;Port=5432\;Database=dbname2\;User Id=userid\;Password=mypassword\;" -v /home/********/CONFIGS/LIVE:/bin/Debug/netcoreapp2.0/publish/Configs --restart always -p 5000:5000 --name containername teh********/myimage
2017-11-01T15:21:43.2883710Z Port=5432\
2017-11-01T15:21:43.2895830Z Database=dbname\
2017-11-01T15:21:43.2906910Z User Id=userid\
2017-11-01T15:21:43.2918030Z Password=mypassword\
2017-11-01T15:21:43.2931210Z " -e ConnString2="Server=172.17.0.4\
2017-11-01T15:21:43.2944180Z Port=5432\
2017-11-01T15:21:43.2956140Z Database=dbame2\
2017-11-01T15:21:43.2968130Z User Id=userid\
2017-11-01T15:21:43.2980310Z Password=mypassword\
2017-11-01T15:21:43.2994020Z " -v /home/********/CONFIGS/LIVE:/bin/Debug/netcoreapp2.0/publish/Configs --restart always -p 5000:5000 --name containername teh********/myimage
2017-11-01T15:21:43.4025020Z 33237871bd9f7e1b3cf6665386ae12111d91a5c9e36d0e3781fa0e77af92e42a
These are the enviornment variables that get put into the container
ConnString2=Server=172.17.0.4Port=5432Database=beertradeauthUser Id=useridPassword=mypassword
ConnString1=Server=172.17.0.4Port=5432Database=beertradeUser Id=useridPassword=mypassword
Got this from vsts github and it worked
"The task doesn't change your inline script. it runs it as is. The issue is in your script.
Instead of:
docker run -d -e EnvVar1=$connstring1 ...
Does this work?
Take \ and double quotes out of User value
change script to(note quotes): docker run -d -e EnvVar1="${connstring1}" ..."

Resources