GithubHelp home page GithubHelp logo

hiteshjasani / nim-mungehosts Goto Github PK

View Code? Open in Web Editor NEW
50.0 2.0 5.0 204 KB

Programmatically alter /etc/hosts file using the command line with Docker containers

License: MIT License

Nim 100.00%
nim dockerfile docker-container hostname

nim-mungehosts's Introduction

mungehosts

Mungehosts is a tool to enable programmatically altering the /etc/hosts file from the Linux command line. Run with arguments, the tool can add aliases for localhost and add new hostname-to-ip mappings. This means you can embed hosts file changes into your scripts -- altering the hosts file on machine startup is a good example.

The reason this was created was due to Docker not providing any way to do this using the Dockerfile syntax. I was building a Dockerfile that had a multi-process server inside of it. One process expected to be able to communicate to another process, whose hostname it received from Zookeeper. However, the hosts file did not have a mapping for that other hostname. To make it work, required aliasing localhost to this other hostname.

Usage with Docker

1. Add the tool to your image in your Dockerfile

ADD https://github.com/hiteshjasani/nim-mungehosts/releases/download/v0.1.1/mungehosts /usr/local/bin/mungehosts
RUN chmod 755 /usr/local/bin/mungehosts

2. Run a startup script when running your container

When you run your container, run a startup script that invokes mungehosts and updates your hosts file before running your server process. See the section below on command line usage for examples.

Command line usage

Grab the Linux binary from releases. It's an executable with no additional dependencies than the normal C runtime so it'll work on any Linux platform.

Aliasing localhost

To alias the hostname foo to localhost.

% sudo mungehosts -l foo

Will result in

127.0.0.1  localhost foo
::1        localhost foo

Any existing aliases will be preserved, so running the command again with trying to alias bar will result in

% sudo mungehosts -l bar
% cat /etc/hosts
127.0.0.1  localhost foo bar
::1        localhost foo bar

Add host mapping

To add a mapping for host baz at 192.168.0.1

% sudo mungehosts -a "192.168.0.1  baz"
% cat /etc/hosts
127.0.0.1  localhost
::1        localhost
192.168.0.1  baz

Help

% mungehosts --help

Dependencies

Runtime

There are no runtime dependencies other than standard C.

% ldd ./mungehosts
        linux-vdso.so.1 =>  (0x00007fffbc9fd000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f7991f40000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7991b7a000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f7992149000)

Platforms

  • Linux -- Full support, binary available in releases
  • Other Unix -- Possibly works, you'll need to build the tool from source
  • Windows -- no support

Basically the tool will work on any Unix platform with a /etc/hosts file. There is a Linux binary in the releases, which I recommend using.

Building the tool from source is not recommended unless you have a high pain tolerance and capacity for delayed gratification. For those who still want to do it, step 1 is installing a Nim compiler.

Sorry, there is no support for Windows.

FAQ

Why can't I update /etc/hosts using the RUN command in a Dockerfile?

Unfortunately, Docker does not persist the /etc/hosts file between RUN commands in Dockerfiles. They only persist changes you make in a running container. Therefore, the recommended way to use this tool is to have a startup script that runs when starting your Docker container. Inside this script, add lines running this tool before starting your server process.

Why can't I simply use sed to modify /etc/hosts?

sed and similar tools actually write their changes to another temp file and then attempt to move it over to /etc/hosts. The problem with this is that some process already has the file open and the move command fails with a message similar to

sed: cannot rename /etc/sedl8ySxL: Device or resource busy

Mungehosts was written to bypass this problem.

Do I have to install Nim?

No. Not if you use the precompiled Linux binary.

I'm getting a too many redirects error on OSX, help?

If you see this kind of message:

Step 4 : ADD https://github.com/hiteshjasani/nim-mungehosts/releases/download/v0.1.0/mungehosts /usr/local/src/mungehosts
2014/11/16 18:36:22 Get https://s3.amazonaws.com/github-cloud/releases/26729671/1047aa0e-6dbf-11e4-93b9-7a358f6958f9?response-content-disposition=attachment%3B%20filename%3Dmungehosts&response-content-type=application/octet-stream&AWSAccessKeyId=AKIAISTNZFOVBIJMK3TQ&Expires=1416181042&Signature=%2F1DgDIn3E9zNQvIg9QMkAWh0SZM%3D:
dial tcp: lookup s3.amazonaws.com on [10.0.2.3]:53: too many redirects

Then the problem may be a DNS issue if you're on OSX. A workaround for this is to log into boot2docker and update the dns servers.

% boot2docker ssh
   ...
docker@boot2docker:~$ cat /etc/resolv.conf
nameserver 10.0.2.3
docker@boot2docker:~$ sudo vi /etc/resolv.conf
docker@boot2docker:~$ cat /etc/resolv.conf
nameserver 10.0.2.3
nameserver 8.8.8.8

nim-mungehosts's People

Contributors

hiteshjasani avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

nim-mungehosts's Issues

Tool should be idempotent and prevent adding duplicates

Running mungehosts multiple times with the same values will add duplicate entries in /etc/hosts. It would be nice if, instead, the tool ignored these duplicates rather than blindly adding them so your file doesn't end up looking like...

...
192.168.0.1  myhost

192.168.0.1  myhost

192.168.0.1  myhost
...

Perhaps it might also be nice to provide a warning or command-line switch to check for duplicate entries such as...

...
192.168.0.1  myhost

192.168.0.1  otherhost

192.168.0.1  samehost
...

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.