Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to start detached #36

Closed
01e9 opened this issue Apr 3, 2018 · 6 comments
Closed

Option to start detached #36

01e9 opened this issue Apr 3, 2018 · 6 comments

Comments

@01e9
Copy link

01e9 commented Apr 3, 2018

I tried docker run's --detach but it doesn't detach

x11docker --hostdisplay -- "--detach --cap-add=SYS_PTRACE" my_image my-command.sh

Or tricks like adding & at the end of command, disown, nohup should work fine?

@01e9
Copy link
Author

01e9 commented Apr 3, 2018

Not needed anymore.


I have a script that does the following:

  1. Build the image if it doesn't exist

  2. Start IDE via x11docker

  3. Add container IP in /etc/hosts to be able to access it in browser.

    Port mapping is not a solution because I already have a container mapped to the same port.

I wanted the step 2. to not stop the script so it will continue to step 3: get container IP and add it to /etc/hosts.

But I resolved that by switching step 3. and 2. and by providing a static IP to container.

Here is the script:

#!/bin/bash

[ -n "$(which x11docker)" ] \
    || { echo "x11docker is required"; exit 1; }

SCRIPT_DIR=$(dirname $(readlink -f "$0")) # /home/user/project/docker
PROJECT_DIR=$(dirname ${SCRIPT_DIR}) # /home/user/project

CMD=${@}
[ -n "${CMD}" ] \
    || { echo "Command is required"; exit 1; }

[ -n "$(docker images -q --filter=reference='my_image')" ] \
    || docker build -t my_image ${SCRIPT_DIR}

NETWORK=my_app
SUBNET='172.33.0.0/16'
IP='172.33.0.11'
HOST='project.dev'

[ -n "$(docker network ls -q --filter name=${NETWORK})" ] \
    || docker network create --subnet="${SUBNET}" ${NETWORK}

if grep -q "${HOST}" /etc/hosts; then
    if [ "${IP}" != "$(grep "${HOST}" /etc/hosts | grep -o --regexp='^[0-9\.]\+')" ]; then
        echo "Updating ${HOST} in /etc/hosts"
        sudo sed -i "s/^.*\t\(${HOST}\)/${IP}\t\1/g" /etc/hosts
    fi
else
    echo "Adding ${HOST} in /etc/hosts"
    echo "${IP}	${HOST}" | sudo tee -a /etc/hosts
fi

x11docker --hostdisplay --homedir ${HOME} --clipboard --stdout --stderr \
    --cap-default --no-entrypoint \
    -- "--cap-add=SYS_PTRACE \
        --sysctl net.ipv4.ip_unprivileged_port_start=0 \
        --workdir ${PROJECT_DIR} \
        --network ${NETWORK} \
        --hostname ${HOST} \
        --ip ${IP}" \
    my_image \
    ${CMD}

@01e9 01e9 closed this as completed Apr 3, 2018
@mviereck
Copy link
Owner

mviereck commented Apr 3, 2018

I tried docker run's --detach but it doesn't detach

x11docker already runs docker in detached mode. Alternativly, you can run x11docker in background with &.

But I resolved that by switching step 3. and 2. and by providing a static IP to container.

You can parse --verbose output for x11docker: container IP: 172.17.0.3

Update: x11docker V3.9.8.1 shows the container ID on stdout like docker does in detached mode. With the container ID you can run docker inspect to get informations like IP adress.
Get ID with e.g.:

read containerid < <(x11docker [...])

This syntax has also the effect to run x11docker in background like with &.

--workdir ${PROJECT_DIR}

This does not take effect as x11docker always executes cd $HOME.
Use sh -c "cd /my/workdir ; command" as image command instead.

mviereck added a commit that referenced this issue Apr 3, 2018
…-dbus-system

         (remove root-sh-shell in parent tree and enable no-new-privileges)
         Output of container ID on stdout (#36)

Signed-off-by: mviereck <[email protected]>
@01e9
Copy link
Author

01e9 commented Apr 3, 2018

--workdir ${PROJECT_DIR}

This does not take effect as x11docker always executes cd $HOME

Actually it works (even after this latest update)

$ docker exec x11docker_... pwd shows /home/user/project instead of /home/user

Also docker inspect x11docker_... shows

[
    {
        "Config": {
            "WorkingDir": "/home/user/project",
            ...
        },
        ...

My Dockerfile doesn't specify WORKDIR

FROM ubuntu:17.10

RUN apt-get update \
    && apt-get install -y \
        wget tar git man nano htop curl \
        libgtk2.0-0 libcanberra-gtk-module libxext-dev libxrender-dev libxtst-dev libxslt-dev dmz-cursor-theme \
        libx11-xcb-dev libxss-dev gconf2 libnss3 libsecret-1-0 gnome-keyring libcap2-bin inotify-tools apgdiff \
    && (curl -sL https://deb.nodesource.com/setup_9.x | bash -) \
    && apt-get install -y \
        build-essential nodejs \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

ENV TINI_VERSION 0.17.0
ADD https://github.com/krallin/tini/releases/download/v${TINI_VERSION}/tini /tini
RUN chmod +x /tini

EXPOSE 443

ENTRYPOINT ["/tini", "--"]
CMD ["sleep", "infinity"]

@mviereck
Copy link
Owner

mviereck commented Apr 3, 2018

ok, docker exec regards it.
Image commands executed by x11docker will be in HOME.
E.g. x11docker -- "--workdir=/etc" x11docker/lxde pcmanfm will show HOME instead of /etc in pcmanfm.

@mviereck
Copy link
Owner

mviereck commented Apr 4, 2018

I've added a new x11docker option --workdir that sets working directory for both docker exec and x11docker image command.
With --workdir you don't need the sh -c workaround. Set it left of -- and all is fine.

@01e9
Copy link
Author

01e9 commented Apr 4, 2018

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants