GithubHelp home page GithubHelp logo

boon-code / docker-inside Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 1.0 164 KB

Allows to run a docker container as current user (similar to Jenkins docker.inside())

License: MIT License

Python 98.45% Shell 1.55%
docker jenkins

docker-inside's Introduction

Docker Inside

Build Status PyPI

dockerinside is a Python (Python3 only) package that shall simplify running docker images as the current user similar to the way Jenkins awesome docker.inside() works. There are two main use-cases:

  • You want to easily share access between a container and your environment without having to manually modify the user id and group id of the files created by the running container.
  • You want to run dockerized GUI applications but share your home environment

You don't have to write a Dockerfile just to adapt to your environment (user id / group id). It is a much more elegant approach to adapt the environment during startup of the container on the fly as Jenkins does it.

Installation

Prerequisits:

  • Python3 installation (>= 3.6)
  • python3-venv is recommended to create a virtual environment

Install current stable version (preferably in a virtual environment):

    pip install git+https://github.com/boon-code/docker-inside.git

For convenience, dockerinside uses su-exec which is statically compiled using alpine and musl-c library. To build it, you have to run

    # use --auto-pull to download alpine image for compilation if it's not available
    docker-inside-setup --auto-pull

This will create a directory ~/.config/docker_inside/ and put a file named su-exec there. You can also compile su-exec and create the file structure yourself or not use it at all. If this file doesn't exist, su is used to switch user id which might cause problems with tty handling, so it's highly recommended to use su-exec.

Big thanks to Natanael Copa (ncopa) for sharing su-exec.

NO_README

If you experience problems related to packaging the README.md or related to setuptools you can disable this behavior using the environment variable NO_README (f.e. NO_README=1).

Usage

Basic

Running an ubuntu:16.04 container as current user with the home directory mounted:

    docker-inside -H ubuntu:16.04

which is roughly equivalent to running

    docker run --rm -ti --user "$(id -u)" -v "${HOME}:${HOME}" ubuntu:16.04

but does already add users and groups so you won't see I have no name! in your shell prompt.

Fake Home

You can also use a fake home directory

    mkdir -p /tmp/fake-home
    docker-inside --mount-as-home /tmp/fake-home ubuntu:16.04 -- echo "Hello, World" \>~/readme.txt
    #DockerInside : MainThread : INFO : Starting container: 59133ebeb3a3116999f66b4e302ba675a74f02ac83ae526704f2f4cdbd82ed5d
    #DockerInside : MainThread : INFO : Container 59133ebeb3a3116999f66b4e302ba675a74f02ac83ae526704f2f4cdbd82ed5d stopped
    cat /tmp/fake-home/readme.txt
    #Hello, World

Jenkins Debugging

Sometimes, I just quickly want to debug a problem on a failing Jenkins job which uses docker with lot's of bind mounts which is as simple as this:

    cd <WORKSPACE>
    docker-inside -v <first-mount>:<some-path> \
                  -v <second-mount>:<some-other-path> \
                  -v <WORKSPACE> \
                  -w <WORKSPACE> \
                  <IMAGE_TO_USE> \
                  [optional-command]

Additional Use-Cases

Please let me know I you need support for more options from original docker run command or have any other suggestions how to improve this package. Please also let me know if your Docker image is failing using this package and I will see if I can fix the issue. Adding users and groups is unfortunately quite different among distributions.

Related

I still use the package myself on a regular basis when using Docker. However, to avoid having to rely on work-arounds used in this script, podman is a simple and powerful replacement for Docker. podman handles user permissions and bind mounts in a sane way per default. Check it out!

docker-inside's People

Contributors

boon-code avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

pombredanne

docker-inside's Issues

root : MainThread : INFO : setup returned 3 on "docker-inside-setup --auto-pull"

First give me this:

DockerInside.Setup : MainThread : WARNING : Image 'alpine:3.6' not found locally -> pull it
root : MainThread : INFO : setup returned 3

Then i pulled 'alpine:3.6' with docker pull ...

Now i execute docker-inside-setup --auto-pull, it seems to be doing something for a while and at the end just throw me:

root : MainThread : INFO : setup returned 3

Am behind a corporate proxy, environment http_proxy and https_proxy vars are settle. Any clue here???

ImportError No module named ssl_match_hostname on docker-inside-setup --auto-pull

Any base dependency not mentioned in readme???

Here is the full trace:

docker-inside-setup --auto-pull
Traceback (most recent call last):
File "/home/roly/.local/bin/docker-inside-setup", line 6, in
from dockerinside.setup.init import setup_main
File "/home/roly/.local/lib/python2.7/site-packages/dockerinside/init.py", line 9, in
import docker
File "/home/roly/.local/lib/python2.7/site-packages/docker/init.py", line 2, in
from .api import APIClient
File "/home/roly/.local/lib/python2.7/site-packages/docker/api/init.py", line 2, in
from .client import APIClient
File "/home/roly/.local/lib/python2.7/site-packages/docker/api/client.py", line 10, in
from .. import auth
File "/home/roly/.local/lib/python2.7/site-packages/docker/auth.py", line 9, in
from .utils import config
File "/home/roly/.local/lib/python2.7/site-packages/docker/utils/init.py", line 3, in
from .decorators import check_resource, minimum_version, update_headers
File "/home/roly/.local/lib/python2.7/site-packages/docker/utils/decorators.py", line 4, in
from . import utils
File "/home/roly/.local/lib/python2.7/site-packages/docker/utils/utils.py", line 13, in
from .. import tls
File "/home/roly/.local/lib/python2.7/site-packages/docker/tls.py", line 5, in
from .transport import SSLHTTPAdapter
File "/home/roly/.local/lib/python2.7/site-packages/docker/transport/init.py", line 3, in
from .ssladapter import SSLHTTPAdapter
File "/home/roly/.local/lib/python2.7/site-packages/docker/transport/ssladapter.py", line 23, in
from backports.ssl_match_hostname import match_hostname
ImportError: No module named ssl_match_hostname

Tried:
pip install backports.ssl-match-hostname

Getting:
Requirement already satisfied: backports.ssl-match-hostname in /home/roly/.local/lib/python2.7/site-packages (3.7.0.1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.