Access environment variables passed in docker run from supervisor child process - docker

I'm running a docker container with the following command:
docker run -it -e CustomEnvVariable="val123" -p 80:80 imagename
The image has supervisor running with the following conf file for my app:
[program:myapp]
command=/usr/bin/dotnet /root/myapp/myapp.dll
directory=/root/myapp/
autostart=true
autorestart=true
stderr_logfile=/var/log/myapp.err.log
stdout_logfile=/var/log/myapp.out.log
environment=ASPNETCORE__ENVIRONMENT=Production
stopsignal=INT
user=root
How can I access CustomEnvVariable from within myapp after supervisor launches it?
I tried the following value in the conf file environment value:
environment=ASPNETCORE__ENVIRONMENT=Production,CustomEnvVariable=%(ENV_CustomEnvVariable)s
but I would received this error when restarting supervisor:
Restarting supervisor: Error:
Format string 'ASPNETCORE__ENVIRONMENT=Production,CustomEnvVariable="%(ENV_CustomEnvVariable)s"'
for 'environment' contains names which cannot be expanded
Some more information:
Using supervisor version: 3.0 (install via apt-get install supervisor -y).
I confirmed that the value of CustomEnvVariable is reaching the container by checking the output of echo $CustomEnvVariable.
I did a quick scan of all the environment variables available to from the app, here are the results:
{
"LC_ADDRESS": "",
"SUPERVISOR_PROCESS_NAME": "myapp",
"LC_MONETARY": "",
"LC_MESSAGES": "",
"LC_NAME": "",
"SUPERVISOR_ENABLED": "1",
"SUPERVISOR_SERVER_URL": "unix:///var/run/supervisor.sock",
"LC_COLLATE": "",
"ASPNETCORE__ENVIRONMENT": "Production",
"SUPERVISOR_GROUP_NAME": "myapp",
"LANG": "",
"LC_NUMERIC": "",
"LANGUAGE": "",
"LC_PAPER": "",
"LC_IDENTIFICATION": "",
"LC_CTYPE": "",
"LC_MEASUREMENT": "",
"TERM": "xterm",
"LC_TIME": "",
"LC_ALL": "",
"PATH": "/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin",
"LC_TELEPHONE": "",
"PWD": "/"
}

The syntax is correct, but environment expansion is only available in version > 3.2 of supervisor, as per this github issue.

Related

how to bring up failed container

have a container that failed after a long setup and i want to log in (exec bash) at that point instead of executing the slow setup again. Is there any way?
The container is a left over from a docker build process, it is still the FROM ... AS builder stage.
if i try to start it, it will fail right away.
$ docker start -ai 3d35a7f7a7b4
/bin/sh: mvn: command not found
trying to exec anything right away doesn't work either
$ docker start 3d35a7f7a7b4 & docker exec 3d35a7f7a7b4 -it /bin/sh
[1] 403273
3d35a7f7a7b4
unable to upgrade to tcp, received 500
[1]+ Done docker start 3d35a7f7a7b4
more info:
$ docker inspect 3d35a7f7a7b4
[
{
"Id": "3d35a7f7a7b4018ebbbd9aa59356714d7fed291a43752cbcb86dd852c946cc1e",
"Created": "2022-07-06T23:56:37.001004587Z",
"Path": "/bin/sh",
"Args": [
"-c",
"mvn --version"
],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 127,
"Error": "",
"StartedAt": "2022-07-07T00:02:35.755444447Z",
"FinishedAt": "2022-07-07T00:02:35.75741167Z"
},
"Image": "sha256:4819e2469963fdf531ec5bce5401b7ae7d28cd403528c0109512b5170ef61752",
...
this is not an optimal answer. Here just for documentation (and for people to vote up if it is the best one can do with docker)
docker run can be used on the image of the stopped container, and you can pass the CMD parameter right away. But any other peculiarity of the stopped container will also have to be repeated. e.g. network.
for the example on the question:
host$ docker run -it sha256:4819e2469963fdf531ec5bce5401b7ae7d28cd403528c0109512b5170ef61752 /bin/bash
container# _

starting container process caused: exec: "[\"/bin/sh -c\"": stat ["/bin/sh -c": no such file or directory

I am building and deploying an application via Docker and ECS Fargate. I have my entrypoint command defined in the ECS Task definition. Upon pushing the image into a private ECR repository, I am getting this error when ECS Fargate attempts to deploy the docker image. Any advice would be helpful. Below is the dockerfile, Task Definition, and the error.
Dockerfile
FROM centos:7
COPY /src/main/build/application.zip /tmp/application.zip
COPY /src/main/residual-container-setup/application/init.sh /tmp/init.sh
#Environment variables and Entry point being defined via task definition
Task Definition
{
"containerDefinitions": [
{
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/application",
"awslogs-region": "us-east-2",
"awslogs-stream-prefix": "ecs"
}
},
"entryPoint": [
"[\"/bin/sh -c\"",
"\"/tmp/init.sh\"]"
],
"portMappings": [
{
"hostPort": 9003,
"protocol": "tcp",
"containerPort": 9003
}
],
"cpu": 0,
"environment": [
{
"name": "HOST",
"value": "dev.application.com"
},
{
"name": "REST_PORT",
"value": "8003"
}
],
"mountPoints": [],
"volumesFrom": [],
"image": "xxxxxxxxxxxx.dkr.ecr.us-east-2.amazonaws.com/application:latest",
"essential": true,
"name": "application"
}
]
Error
container_linux.go:380: starting container process caused: exec: "[\"/bin/sh -c\"": stat ["/bin/sh -c": no such file or directory
I attempted running the container locally with the following command: `docker run -it $docker_image /bin/sh
I was unable to even exec into the container. I believe I may need to install additionally in the image to get this to work. Any advice would be helpful.
Update
I have updated the dockerfile to update the permissions on the init script using the following command: chmod +x /tmp/init.sh
I have also update the task definition entrypoint attribute to ["/bin/sh", "-c", "/tmp/init.sh"]
After making these changes I am now being presented with the following:
container_linux.go:380: starting container process caused: exec: "-c": executable file not found in $PATH
Your entrypoint is defined wrongly.
The way you did it Linux thinks the path to the binary is "/bin/sh -c". If you check the container image I'm pretty sure you do not find that file either.

not able to run startup command in container created via docker-engine's go sdk api

I am trying to create a container with docker engine's go sdk api. I need to run a command on startup in the container, which I pass as a parameter to Client.ContainerCreate() api. I tried passing that command in different ways, but everytime found some issue. Below is the code I use:
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: "hyperledger/fabric-ca",
Cmd: []string{"/bin/sh"," fabric-ca-server start -b admin:adminpw"},
Env: []string{"FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server", "FABRIC_CA_SERVER_CA_NAME=ca.example.com"}, }, nil, nil, "ca.example.com")
if err != nil {
fmt.Println(" failed to create container, err:", err)
} else {
fmt.Println(" Container ID :", resp.ID, "warning:", resp.Warnings, "err:", err)
}
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
fmt.Println("failed to start container, err:", err)
}
1)
If I don't provide Cmd parameter, container is created.
Container ID : 02edc80d6545ca2c8089a191ba9174070e1527dc027191e0d7686bff23a9f39d warning: [] err: <nil>
vignesh#vignesh-ThinkPad-E470 ~ $ docker container list
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
02edc80d6545 hyperledger/fabric-ca "/bin/sh -c 'fabric-…" 7 minutes ago Up 7 minutes 7054/tcp ca.example.com
2)
If I provide Cmd parameter as Cmd: []string{"/bin/sh"," fabric-ca-server start -b admin:adminpw"}, ContainerCreate() returns a container Id, but docker container list doesn't show any container and docker container inspect shows status as exited.
Container ID : c2f7bbc54e09665b0797eeaea43723f3fddf4538db8bf4327362b2535b9a088b warning: [] err: <nil>
vignesh#vignesh-ThinkPad-E470 ~ $ docker container list
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
vignesh#vignesh-ThinkPad-E470 ~ $ docker container inspect c2f7bbc54e09665b0797eeaea43723f3fddf4538db8bf4327362b2535b9a088b
[
{
"Id": "c2f7bbc54e09665b0797eeaea43723f3fddf4538db8bf4327362b2535b9a088b",
"Created": "2019-02-05T20:05:11.360875853Z",
"Path": "/bin/sh",
"Args": [
" fabric-ca-server start -b admin:adminpw"
],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 127,
"Error": "",
"StartedAt": "2019-02-05T20:05:12.448079587Z",
"FinishedAt": "2019-02-05T20:05:12.644957269Z"
},
3) If I provide Cmd parameter as Cmd: []string{"sh -c"," fabric-ca-server start -b admin:adminpw"}, ContainerCreate() returns a container Id, but ContainerStart() gives "exec: \"sh -c\": executable file not found in $PATH" error. docker container list doesn't show any container.
Container ID : d2752eb14267ccc170121d28ea9c51f2cd99227eba3d53b4430bd4b7eeec4787 warning: [] err: <nil>
failed to start container, err: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"sh -c\": executable file not found in $PATH": unknown
vignesh#vignesh-ThinkPad-E470 ~ $ docker container list
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
vignesh#vignesh-ThinkPad-E470 ~ $ docker container inspect d2752eb14267ccc170121d28ea9c51f2cd99227eba3d53b4430bd4b7eeec4787
[
{
"Id": "d2752eb14267ccc170121d28ea9c51f2cd99227eba3d53b4430bd4b7eeec4787",
"Created": "2019-02-06T17:02:13.989788091Z",
"Path": "sh -c",
"Args": [
" fabric-ca-server start -b admin:adminpw"
],
"State": {
"Status": "created",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 127,
"Error": "OCI runtime create failed: container_linux.go:348: starting container process caused \"exec: \\\"sh -c\\\": executable file not found in $PATH\": unknown",
"StartedAt": "0001-01-01T00:00:00Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
4) If I provide Cmd parameter as Cmd: []string{"fabric-ca-server start -b admin:adminpw"}, ContainerCreate() returns a container Id, but ContainerStart() gives \"exec: \\"fabric-ca-server start -b admin:adminpw\\": executable file not found in $PATH\": unknown" error. docker container list doesn't show any container.
Container ID : d81d4b8f5ae964ec8ef805671a8e4233b41ea363ad890da0218c0ef586d7a72c warning: [] err: <nil>
failed to start container, err: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"fabric-ca-server start -b admin:adminpw\": executable file not found in $PATH": unknown
vignesh#vignesh-ThinkPad-E470 ~ $ docker container list
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
vignesh#vignesh-ThinkPad-E470 ~ $ docker container inspect d81d4b8f5ae964ec8ef805671a8e4233b41ea363ad890da0218c0ef586d7a72c
[
{
"Id": "d81d4b8f5ae964ec8ef805671a8e4233b41ea363ad890da0218c0ef586d7a72c",
"Created": "2019-02-06T17:12:10.443261734Z",
"Path": "fabric-ca-server start -b admin:adminpw",
"Args": [],
"State": {
"Status": "created",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 127,
"Error": "OCI runtime create failed: container_linux.go:348: starting container process caused \"exec: \\\"fabric-ca-server start -b admin:adminpw\\\": executable file not found in $PATH\": unknown",
"StartedAt": "0001-01-01T00:00:00Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
There is a .yaml file to create the same container via docker-compose command where sh -c 'fabric-ca-server start -b admin:adminpw' is passed as startup command and the container is created.
I am trying to create that container via go code and facing issues.
As I am not seeing any issue with the command in .yaml file, I think command is fine. I am not able to figure out what I am missing. Kindly help :)
Had posted this question at github and got the answer there. https://github.com/moby/moby/issues/38687

Issue in building docker image using habitus

I am trying to build Docker image using Habitus, so that I can securely pass build time secrets (github ssh keys) to docker.
My Habitus build file:
build:
version: 2016-03-14
steps:
builder:
name: search/poirot
dockerfile: Dockerfile
secrets:
id_rsa:
type: file
value: _env(HOME)/.ssh/id_rsa
cleanup:
commands:
- rm -rf /root/.ssh/
I have the latest Docker for Mac installed on my machine. I am running this command to build:
sudo ./habitus
--certs=$HOME/.docker/machine/certs/ --host=192.168.99.100:59124
I am using the IP and port from "~/.docker/machine/machines/default/config.json"
Content:
"Driver": {
"IPAddress": "192.168.99.100",
"MachineName": "default",
"SSHUser": "docker",
"SSHPort": 59124,
"SSHKeyPath": "/Users/shiladityamandal/.docker/machine/machines/default/id_rsa",
"StorePath": "/Users/shiladityamandal/.docker/machine",
"SwarmMaster": false,
"SwarmHost": "tcp://0.0.0.0:3376",
"SwarmDiscovery": "",
"VBoxManager": {},
"HostInterfaces": {},
"CPU": 1,
"Memory": 2048,
"DiskSize": 20000,
"NatNicType": "82540EM",
"Boot2DockerURL": "",
"Boot2DockerImportVM": "",
"HostDNSResolver": false,
"HostOnlyCIDR": "192.168.99.1/24",
"HostOnlyNicType": "82540EM",
"HostOnlyPromiscMode": "deny",
"NoShare": false,
"DNSProxy": true,
"NoVTXCheck": false
},
I keep getting the following error during build:
Build for step test/test failed due to Post
https://192.168.99.100:59124/build?dockerfile=Dockerfile.generated&rm=1&t=search%2Fservice:
dial tcp 192.168.99.100:59124: i/o timeout
What am I doing wrong?
I was following this process- https://dzone.com/articles/using-ssh-private-keys-securely-in-docker-build
Solved it myself. Had to connect to 192.168.99.100:2376

Why does docker stop immediately after starting and how to prevent it from stopping?

I am trying to start a docker container using the following POST request:
Content-Type: application/json
{
"Hostname":"",
"Domainname": "",
"User":"",
"Memory":0,
"MemorySwap":0,
"CpuShares": 512,
"Cpuset": "0,1",
"AttachStdin":true,
"AttachStdout":true,
"AttachStderr":true,
"PortSpecs":6002,
"Tty":false,
"OpenStdin":false,
"StdinOnce":false,
"Env":null,
"Cmd":
[
"python",
"app.py"
],
"Image":"jobinar/smile_webapp",
"Volumes":{
"/tmp": {}
},
"WorkingDir":"",
"NetworkDisabled": false,
"ExposedPorts":{
"5000/tcp": {}
}
}
However, the container immediately stops after starting. How do I configure my request to prevent it from exiting?
I would appreciate a POST request which does this instead of the command-line way.
EDIT: I get a 201 CREATED response with the id of the created container and I can see that the container is created by running by using the docker ps -a command.
If you have upgraded you docker version you habe to delete /var/lib/docker/network on ubuntu

Resources