Connection string in build variable VSTS - docker

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}" ..."

Related

How to use list in "docker's run -e options"?

I downloaded my spring boot project from git on Amazon Linux2 OS and made it a docker image.
Then, I tried to use the "docker run" command to run the docker container with this image.
At this time, there are values in the form of an array among the environmental variables I need, and other "docker run options" worked well, but there was a problem with the values in this form of an array.
// Command 1
docker run -itd --name example_container -e DDL_AUTO={update,create,validate} -p 80:8080 example_image
In command 1's case, docker: invalid reference format: repository name must be lowercase. error occured.
// Command 2 (put space between update, create, validate)
docker run -itd --name example_container -e DDL_AUTO={update, create, validate} -p 80:8080 example_image
In command 2's case, docker: invalid reference format. error occured.
Is there a way to put list optionally in this command format?
You need to put quotes around the environment variable value, since it contains characters the shell would otherwise interpret. Try:
docker run -itd --name example_container -e DDL_AUTO="{update,create,validate}" -p 80:8080 example_image

Environment variables are not getting created inside docker container

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

Sonarqube doesn't work on Docker

I'm running a docker container; it's sonarqube: When I use this command:
docker run -d --restart=always --name sonarqube -p 9000:9000 -p 9092:9092 sonarqube
The container runs well, but when I use the command to run and to configure the database, this command:
docker run -d --restart=always --name sonarqube -p 9000:9000 -p 9092:9092 -e SONARQUBE_JDBC_USERNAME=my_user_name -e SONARQUBE_JDBC_PASSWORD=my_password -e SONARQUBE_JDBC_URL=jdbc:postgres://host:123qweasdzxc#ec2-54-243-28-109.compute-1.amazonaws.com:5432/database?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory sonarqube
I'm getting this error:
"docker run" requires at least 1 argument.
See 'docker run --help'.
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] [flags]
Run a command in a new container
What is wrong? or How to fix this little problem?
Use single quotes in your "SONARQUBE_JDBC_URL" environment variable. I just tried to delimit that particular variable so that docker understands it as a complete string with it's starting & ending point. Due to some reason, it was unable to fetch the IMAGE_NAME argument which was required to run the container.
docker run -d --restart=always --name sonarqube -p 9000:9000 -p 9092:9092 -e SONARQUBE_JDBC_USERNAME=my_user_name -e SONARQUBE_JDBC_PASSWORD=my_password -e SONARQUBE_JDBC_URL='jdbc:postgres://host:123qweasdzxc#ec2-54-243-28-109.compute-1.amazonaws.com:5432/database?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory' sonarqube
This worked for me.
The sonarqube docker image latest version did not work for me either, However below image worked for me. If you using docker for learning purpose you can follow below steps. Please note, By default, the image will use an embedded H2 database that is not suited for production.
Run below command to pull the docker image:
docker pull sonarqube:lts-community
Run below command to create the docker container for sonarqube:
-h, --hostname string Container host name
-d, --detach Run container in background and print container ID
docker run --name sanarqube -h sonarqube -p 8084:9000 -d sonarqube
search sonarqube UI in your favourite browser
http://YOUR-IP:8084/
Then to stop the conatiner
docker stop sonarqube
Then to start the conatiner
docker start sonarqube
Hope this help!

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

escaping double quote characters with Jenkins docker plugin

I want to spin up a container using the Jenkins docker plugin as follows:
docker.image('microsoft/mssql-server-linux').run("\"ACCEPT_EULA=Y\" -e \"SA_PASSWORD=P#ssword1\" --name SQLLinuxMaster -d -i -p 15565:1433")
my initial thoughts were that \" should work, however when I run a build the command is failing, I look in the Jenkins log and it appears that (what I think should be) the escaped double quotes are not appearing.
Can someone please point me in the right direction as to how I should be correctly escaping the the double quote characters in the run argument.
Using the conventional docker command line the following spins up the container as desired:
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=P#ssword1" --name SQLLinuxChris -d -i -p 15565:1433 microsoft/mssql-server-linux
You can use
docker.image('microsoft/mssql-server-linux').run("-e ACCEPT_EULA=Y -e SA_PASSWORD=P#ssword1 --name SQLLinuxMaster -d -i -p 15565:1433")
You don't need double quotes. Also you were missing a -e at the start earlier, which may have caused the issue

Resources