Tag: linux

Dropbox in Docker with systemd

My CentOS 7 headless install of Dropbox recently quit syncing due to the new Dropbox linux requirements. Rather than try to hack it back together I found this great guide on running Dropbox in a Docker container which utilizes a pre-built container that can update itself, assuming Dropbox puts any more effort into their linux client. Cough. You can audit the security/operation of the container via its Dockerfile.

However one thing both the guide docs and container docs above leave out is automating operation of the Dropbox container via systemd, which of course is the default init system most places nowadays including my CentOS 7 and Ubuntu 18.04 bionic servers. The error I kept encountering was:

The name "dropbox" is already in use by container f9e5798a82e9

This is because all the guides suggest using the Docker parameter --name=dropbox which will only succeed the first time you issue the docker run command. When you restart the container via run instead of start it instructs docker to build a new container. Rebuilding the container is also when the Dockerfile would update to the latest version of Dropbox so we can't simply switch to start.

To get around this we use the systemd directive ExecStartPre=-/usr/bin/docker rm dropbox to remove any previous containers using the name dropbox, and additionally prefix the command with a - which tells systemd to ignore any failures reported by the command. ie: if there is no container to remove docker reports an error we don't care about.

  1. Update the unit file fields:
    MY_USERNAME, MY_UID, and MY_GID
  2. Drop it into:
    /etc/systemd/system/dropbox.service
  3. Reload, enable, and start Dropbox:
    systemctl daemon-reload && systemctl enable dropbox && systemctl start dropbox

This is working on my Ubuntu bionic box and I always welcome improvements, just comment on the gist!


cd shortcuts with CDPATH and bash tab completion

For years I've had some fairly hacky ways of changing directories quicker. I'd wager a nickel these kind of aliases are pretty common:

alias cdblog='cd ~/sites/my_blog'
alias cdapp='cd ~/repos/my_app'

That grows pretty unwieldy when working with a bunch of repos. So then I thought I was super clever by using bash functions to cd directly into a git repo, since all my repos were in one of two parent dirs:

function repos(){ cd ~/repos/$1; }
function sites(){ cd ~/sites/$1; }

me@local$ repo my_app
me@local$ sites my_blog

It was a bit more typing, but fewer aliases to remember and more scalable. However the loss of tab completion using this method is a bummer.

When I finally got annoyed enough to look for a better way, of course it was waiting for me in a superuser post. Using the CDPATH environment variable and the bash-completion package (same name on centos/fedora/os x/ubuntu) I can now cd into my most used directories, complete with tab completion, from anywhere on the cli.

  1. Install bash-completion, substituting appropriate package manager:

    brew install bash-completion
    
  2. OS X ONLY: append to your ~/.bash_profile:

    if [ -f $(brew --prefix)/etc/bash_completion ]; then
      . $(brew --prefix)/etc/bash_completion
    fi
    
  3. Everyone append to your ~/.bash_profile:

    export CDPATH=.:~:~/repos:~/sites
    
  4. Activate changes:

    source ~/.bash_profile
    
  5. Kick the tires, from say /tmp:

    me@local tmp$ cd my<TAB><TAB>
    my_blog/ my_app/
    

Tab completion should be working as you'd expect against all your favorite directories, no matter where you are on the cli. This made navigating around dozens of repos quite a bit easier for me, though for a handful of my most used ones I may still treat myself to a dedicated alias. Because I'm weak.

© Justin Montgomery. Built using Pelican. Theme is subtle by Carey Metcalfe. Based on svbhack by Giulio Fidente.