Sudo lxc-attach -n "$(docker inspect --format '{{.Id}}' $BUILD_NAME)" -- bash -c ./configure.sh lxc-attach: attach.c: lxc_attach: 713 failed to get the init pid

In the documentation regarding CircleCI docker exec, there is reference to the syntax

sudo lxc-attach -n "$(docker inspect --format '' $BUILD_NAME)" -- bash -c ./configure.sh

to execute processes within running containers. However, I keep getting the same response:

lxc-attach: attach.c: lxc_attach: 713 failed to get the init pid

When I run the command

docker inspect --format '' $BUILD_NAME

The following is the output:

[
{
“Id”: “f509ed1efaa939e46d4dbc5ac9e6ba3d1e7a7ce2441e6e9a18af021a1bee5eef”,
“Parent”: “5ccc36dc8898d1d96b488f04a4f34357cf63401c1a97f4bb3bca3d050ebd2a6c”,
“Comment”: “”,
“Created”: “2016-06-13T19:21:02.131825755Z”,
“Container”: “1afe0712656be1cd8e3f1a0b122cf4f2ffa17601cbdd56c6238d7ff05e48939c”,
“ContainerConfig”: {
“Hostname”: “914cf42a3e15”,
“Domainname”: “”,
“User”: “”,
“AttachStdin”: false,
“AttachStdout”: false,
“AttachStderr”: false,
“Tty”: false,
“OpenStdin”: false,
“StdinOnce”: false,
“Env”: [
“PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin”,
“TERM=xterm”,
“DEBIAN_FRONTEND=noninteractive”
],
“Cmd”: [
“/bin/sh”,
“-c”,
“#(nop) ENTRYPOINT \u0026{["/docker-entrypoint.sh"]}”
],
“Image”: “5ccc36dc8898d1d96b488f04a4f34357cf63401c1a97f4bb3bca3d050ebd2a6c”,
“Volumes”: null,
“WorkingDir”: “”,
“Entrypoint”: [
“/docker-entrypoint.sh”
],
“OnBuild”: ,
“Labels”: {}
},
“DockerVersion”: “1.8.3-circleci”,
“Author”: “Timothy Dalbey \u003ctimothy.dalbey@mheducation.com\u003e”,
“Config”: {
“Hostname”: “914cf42a3e15”,
“Domainname”: “”,
“User”: “”,
“AttachStdin”: false,
“AttachStdout”: false,
“AttachStderr”: false,
“Tty”: false,
“OpenStdin”: false,
“StdinOnce”: false,
“Env”: [
“PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin”,
“TERM=xterm”,
“DEBIAN_FRONTEND=noninteractive”
],
“Cmd”: null,
“Image”: “5ccc36dc8898d1d96b488f04a4f34357cf63401c1a97f4bb3bca3d050ebd2a6c”,
“Volumes”: null,
“WorkingDir”: “”,
“Entrypoint”: [
“/docker-entrypoint.sh”
],
“OnBuild”: ,
“Labels”: {}
},
“Architecture”: “amd64”,
“Os”: “linux”,
“Size”: 0,
“VirtualSize”: 706867376,
“GraphDriver”: {
“Name”: “btrfs”,
“Data”: null
}
}
]

Any feedback as to why that might be happening?

I found this is a bug on our documentation.

TL;DR the correct command is the following:

sudo lxc-attach -n "$(docker inspect --format "{{.Id}}" $MY_CONTAINER_NAME)" -- bash -c $MY_COMMAND

We are using jekyll and raw markdown looks like this.

sudo lxc-attach -n “$(docker inspect --format “{{.Id}}” $MY_CONTAINER_NAME)” – bash -c $MY_COMMAND

However, when jekyll parses the markdown, {{.Id}} part disappears because jeykll uses curly brackets to embed variables.

I just pushed a fix to escape the curly brackets and now the doc is rendered as expected.

$ sudo lxc-attach -n "$(docker inspect --format "{{.Id}}" $BUILD_NAME)" -- bash -c ./configure.sh
Exit code: 1
sudo lxc-attach -n "$(docker inspect --format "{{.Id}}" $BUILD_NAME)" -- bash -c ./configure.sh
lxc-attach: attach.c: lxc_attach: 713 failed to get the init pid

sudo lxc-attach -n "$(docker inspect --format "{{.Id}}" $BUILD_NAME)" -- bash -c ./configure.sh returned exit code 1

Doesn’t appear to resolve the issue.

Can you try using the container ID instead of $BUILD_NAME? You should be able to get the id with docker ps.

@TimothyDalbey

I just wanted to clarify in case there is confusion. Where do you get $BUILD_NAME from? Was it specified in our doc somewhere or have we told you to use it? If so, please scratch it. We need to pass the container id/name to docker inspect command, so using $BUILD_NAME doesn’t make sense here. As I told in the previous reply, passing the container id/name that you get from docker ps command should make this work.

That certainly returns the results I would expect it to - do you think the results would be different if I used the container ID? I can certainly try that.

The $BUILD_NAME variable comes from the machine: environment: section of the circle.yml file. It’s simply the name of the image which in this case is cicd/dlemessenger.

So, please note in my first comment the docker inspect command is already proven to be functional.

Ah, I see. I thought $BUILD_NAME is the name of your Github project but apparently you posted that it returns result. Sorry for missing that.

Now, it’s very strange that why it doesn’t work. If you run docker inspect --format "{{.Id}}" $BUILD_NAME does it return the correct container id? Ultimately, we just want to pass the full length container id to lxc-attach -n.

Also, what version of docker you are using? If you are using our experimental 1.10, then this technique doesn’t work since 1.10 isn’t using LXC. If that’s the case, can you please use 1.9?

FYI: Here is the output when I run the whole thing in my build.

ubuntu@box162:~$ docker version
Client:
 Version:      1.9.1-circleci-cp-workaround
 API version:  1.21
 Go version:   go1.4.3
 Git commit:   517b158
 Built:        Thu Dec 17 02:33:18 UTC 2015
 OS/Arch:      linux/amd64

Server:
 Version:      1.9.1-circleci-cp-workaround
 API version:  1.21
 Go version:   go1.4.3
 Git commit:   517b158
 Built:        Thu Dec 17 02:33:18 UTC 2015
 OS/Arch:      linux/amd64

ubuntu@box162:~$ docker run -d --name "mycontainer" -it busybox tail -f /dev/null
d21f5d6124c9ea66cb4fcd2b46c54b67da18cf6e9213fd579642c6fa274d9c0d

ubuntu@box162:~$ docker ps
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS               NAMES
d21f5d6124c9        busybox             "tail -f /dev/null"   5 seconds ago       Up 3 seconds                            mycontainer

ubuntu@box162:~$ sudo lxc-attach -n "$(docker inspect --format "{{.Id}}" mycontainer)" -- sh
/ # hostname
d21f5d6124c9

/ # exit

OK - so I’m starting to see what’s wrong here:

docker run -d $BUILD_NAME daemonmode
05fdd1e6ae0726f4d8004766df1c2ee3a779992b7b5276293f04514ad2b0b694

which corresponds to

docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
05fdd1e6ae07 cicd/dlemessenger “/docker-entrypoint.s” 1 seconds ago Up Less than a second romantic_hopper

However, when I run the inspect command I get the following:

docker inspect --format “” $BUILD_NAME
[
{
“Id”: “e01bd448654e4a5342f330a36e71b3d7c9a4a3521dbd78bbc71921c57eb2da26”,
“Parent”: “233a7fcaf44260dd8514801ddb5990a4e9a8f23df4ed3849f2f573bf25ae2e43”,
“Comment”: “”,
“Created”: “2016-06-16T02:42:58.247160894Z”,
“Container”: “f27ae3d23dd3c47aafd2d902d965ab40913d83829aeea8d70e33577526c2c0cc”,
“ContainerConfig”: {
“Hostname”: “914cf42a3e15”,
“Domainname”: “”,
“User”: “”,
“AttachStdin”: false,
“AttachStdout”: false,
“AttachStderr”: false,
“Tty”: false,
“OpenStdin”: false,
“StdinOnce”: false,
“Env”: [
“PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin”,
“TERM=xterm”,
“DEBIAN_FRONTEND=noninteractive”
],
“Cmd”: [
“/bin/sh”,
“-c”,
“#(nop) ENTRYPOINT \u0026{["/docker-entrypoint.sh"]}”
],
“Image”: “233a7fcaf44260dd8514801ddb5990a4e9a8f23df4ed3849f2f573bf25ae2e43”,
“Volumes”: null,
“WorkingDir”: “”,
“Entrypoint”: [
“/docker-entrypoint.sh”
],
“OnBuild”: ,
“Labels”: {}
},
“DockerVersion”: “1.8.3-circleci”,
“Author”: “Timothy Dalbey \u003ctimothy.dalbey@mheducation.com\u003e”,
“Config”: {
“Hostname”: “914cf42a3e15”,
“Domainname”: “”,
“User”: “”,
“AttachStdin”: false,
“AttachStdout”: false,
“AttachStderr”: false,
“Tty”: false,
“OpenStdin”: false,
“StdinOnce”: false,
“Env”: [
“PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin”,
“TERM=xterm”,
“DEBIAN_FRONTEND=noninteractive”
],
“Cmd”: null,
“Image”: “233a7fcaf44260dd8514801ddb5990a4e9a8f23df4ed3849f2f573bf25ae2e43”,
“Volumes”: null,
“WorkingDir”: “”,
“Entrypoint”: [
“/docker-entrypoint.sh”
],
“OnBuild”: ,
“Labels”: {}
},
“Architecture”: “amd64”,
“Os”: “linux”,
“Size”: 0,
“VirtualSize”: 706981421,
“GraphDriver”: {
“Name”: “btrfs”,
“Data”: null
}
}
]

Which is of course, not the same container ID? (It’s the last ID returned by the build process)

Step 30 : ENTRYPOINT /docker-entrypoint.sh
—> Running in f27ae3d23dd3
—> e01bd448654e
Removing intermediate container f27ae3d23dd3
Successfully built e01bd448654e

Finally, here’s the docker version:

docker -v
Docker version 1.8.3-circleci, build 5074b56

So, I guess the question is, why is the inspect function returning the wrong ID?

Moar update. I think I’ve reached the bottom of this one.

docker inspect --type=image --format  "" $BUILD_NAME
[
{
    "Id": "cc6ff2c6cceb8f83ed7af4bdb04ea936913b5f33343339f3a6f79157fbb96c97",
    "Parent": "7eccdd92d0602c1d4ab89c9718ce4f451275afcde3d3a9c8816a4600a9e303f6",
    "Comment": "",
    "Created": "2016-06-16T05:00:47.891505088Z",
    "Container": "200695c0e1e279c073f3b3a37fa5cd31524fda0c3c38d2c825885fbf6632dd18",
    "ContainerConfig": {
        "Hostname": "914cf42a3e15",
        "Domainname": "",
        "User": "",
        "AttachStdin": false,
        "AttachStdout": false,
        "AttachStderr": false,
        "Tty": false,
        "OpenStdin": false,
        "StdinOnce": false,
        "Env": [
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
            "TERM=xterm",
            "DEBIAN_FRONTEND=noninteractive"
        ],
        "Cmd": [
            "/bin/sh",
            "-c",
            "#(nop) ENTRYPOINT \u0026{[\"/docker-entrypoint.sh\"]}"
        ],
        "Image": "7eccdd92d0602c1d4ab89c9718ce4f451275afcde3d3a9c8816a4600a9e303f6",
        "Volumes": null,
        "WorkingDir": "",
        "Entrypoint": [
            "/docker-entrypoint.sh"
        ],
        "OnBuild": [],
        "Labels": {}
    },
    "DockerVersion": "1.8.3-circleci",
    "Author": "Timothy Dalbey \u003ctimothy.dalbey@mheducation.com\u003e",
    "Config": {
        "Hostname": "914cf42a3e15",
        "Domainname": "",
        "User": "",
        "AttachStdin": false,
        "AttachStdout": false,
        "AttachStderr": false,
        "Tty": false,
        "OpenStdin": false,
        "StdinOnce": false,
        "Env": [
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
            "TERM=xterm",
            "DEBIAN_FRONTEND=noninteractive"
        ],
        "Cmd": null,
        "Image": "7eccdd92d0602c1d4ab89c9718ce4f451275afcde3d3a9c8816a4600a9e303f6",
        "Volumes": null,
        "WorkingDir": "",
        "Entrypoint": [
            "/docker-entrypoint.sh"
        ],
        "OnBuild": [],
        "Labels": {}
    },
    "Architecture": "amd64",
    "Os": "linux",
    "Size": 0,
    "VirtualSize": 706981421,
    "GraphDriver": {
        "Name": "btrfs",
        "Data": null
    }
}
]

but!

docker inspect --type=container --format “” $BUILD_NAME
Error response from daemon: no such id: cicd/dlemessenger

docker inspect --type=container --format “” $BUILD_NAME returned exit code 1

Action failed: docker inspect --type=container --format “” $BUILD_NAME

So, it’s matching the image, not the container. And so, it dawned on me that actually, the image name is not the name of the SPECIFIC CONTAINER - and the image match was a big ruse. The name of the container can be programmatically set in the run statement with the --name argument and thus may also be referenced easily.

Once I did that, everything else worked as expected.

*Just a quick note otherwise, some sort of glossary that lays out terminology in no uncertain terms would be really great.

docker run -d $BUILD_NAME daemonmode

What this commands does is running a container from an image called $BUILD_NAME and run a command called daemonmode.

However, we want to pass a container id/name to docker inspect but you are passing the image name. I didn’t know that you can also pass an image name to format command but the result you pasted is about the $BUILD_NAME image, not a container.

So, to make this work, I think you should use the following command to start a container.

docker run --name $BUILD_CONTAINER_NAME -d $BUILD_NAME daemonmode

This command will run a named container called $BUILD_CONTAINER_NAME from $BUILD_NAME image.

You can pass the container name to inspect command like this: docker inspect --format '' $BUILD_CONTAINER_NAME.

$BUILD_CONTAINER_NAME could be really anything but I will suggest you to set in your circel.yml just like the image. Please note that you can’t use “/” for container name, so maybe just “dlemessenger” could work.

Assuming you set $BUILD_CONTAINER_NAME correctly from circel.yml, finally the final command looks like this:

# start a container
docker run --name $BUILD_CONTAINER_NAME -d  $BUILD_NAME daemonmode

# exec into the container
sudo lxc-attach -n "$(docker inspect --format "{{.Id}}" $BUILD_CONTAINER_NAME)" -- bash -c ./configure.sh

So - still running into issues here.

pid=$(docker inspect --format '{{.Id}}' compose_test_1);
echo "Test Container ID: $pid";
sudo lxc-attach -n "$pid" -- bash -c /var/www/html/tengu/Symfony/bin/phpunit -c app --testsuite Unit

Evaluates to

Test Container ID: bc5c874633d12a5f749188e2d579af0116b2d3240fa9974bd6fe52b6af6aab75
lxc-attach: attach.c: lxc_attach: 713 failed to get the init pid

I AM using a updated version of docker in the build. Would that cause issues?

Looks like there are known issues with the aforementioned functionality and

- curl -sSL https://s3.amazonaws.com/circle-downloads/install-circleci-docker.sh | bash -s -- 1.10.0

as per this thread: Docker 1.10.0 is available.