escaping double quote characters with Jenkins docker plugin - docker

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

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

How to run a docker run command from Jenkinsfile shell if the command contains a "-"?

Currently I am trying to run the following command using sh from Jenkinsfile
sh "docker run -e key1=${value1} -e key2=\\'run-cli --users ${USERS} --names ${NAMES}\\' -i -t --network host ${DOCKER_HOST}/path/image:tag"
However, it fails with unknown flag: --users each time. It seems like docker isn't treating it as an env variable but instead reading it as a part of its command. I tried every possible combination of quotes and escape sequences but it doesn't work. It runs perfectly fine when running it directly in the console, but fails when running through jenkins. Any workaround to get this working?
You can use the answer I gave here
The idea is to use:
sh ("""
docker run -e key1=${value1} -e key2="run-cli --users ${USERS} --names ${NAMES" -i -t --network host ${DOCKER_HOST}/path/image:tag
""")

Visual Studio Dockerfile EntryPoint Override Explained?

I am new to Docker and trying to understand but I have noticed the Visual Studio does a lot of 'magic' behind the scenes. I have managed to figure out all my questions about the docker run command VS uses when you debug an ASP.NET Core app with Docker support except one.
docker run
-dt
-v "C:\Users\jnhaf\vsdbg\vs2017u5:/remote_debugger:rw"
-v "D:\ProtoTypes\WebAppDockerOrNot\WebAppDockerOrNot:/app"
-v "C:\Users\jnhaf\AppData\Roaming\ASP.NET\Https:/root/.aspnet/https:ro"
-v "C:\Users\jnhaf\AppData\Roaming\Microsoft\UserSecrets:/root/.microsoft/usersecrets:ro"
-v "C:\Users\jnhaf\.nuget\packages\:/root/.nuget/fallbackpackages2"
-v "C:\Program Files\dotnet\sdk\NuGetFallbackFolder:/root/.nuget/fallbackpackages"
-e "DOTNET_USE_POLLING_FILE_WATCHER=1"
-e "ASPNETCORE_ENVIRONMENT=Development"
-e "ASPNETCORE_URLS=https://+:443;http://+:80"
-e "ASPNETCORE_HTTPS_PORT=44328"
-e "NUGET_PACKAGES=/root/.nuget/fallbackpackages2"
-e "NUGET_FALLBACK_PACKAGES=/root/.nuget/fallbackpackages;/root/.nuget/fallbackpackages2"
-p 4800:80
-p 44328:443
--entrypoint tail webappdockerornot:dev -f /dev/null
The final argument --entrypoint tail webappdockerornot:dev -f /dev/null is the one that confuses me. I get that VS is overriding the entry point setup in the Dockerfile but what I do not understand nor can find online is what tail webappdockerornot:dev and the -f /dev/null. I figured out that webappdockerornot:dev is the docker image but can someone explain how this argument works or provide a link to something that explains it.
We can break down that command line a little differently as
docker run \
... some other arguments ... \
--entrypoint tail \
webappdockerornot:dev \
-f /dev/null
and match this against a general form
docker run [OPTIONS] [IMAGENAME:TAG] [CMD]
So the --entrypoint tail option sets the entry point to tail, and the "command" part is -f /dev/null. When Docker actually launches the container, it passes the command as additional arguments to the entrypoint. In the end, the net effect of this is
Ignore what the Dockerfile said to do; after setting up the container runtime environment, run tail -f /dev/null instead.
which in turn is a common way to launch a container that doesn't do anything but also stays running. Then you can use docker exec and similar debugging-oriented tools to do things inside the container.

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