Last Updated: January 28, 2019
·
7.581K
· shicholas

Beautifully Short Dockerfiles

I'm glad there has been a movement in software engineering to write small things. I've seen this in the languages I code in: ruby, angularJS and now docker.

I liken Docker to LEGOS® of the Internet, because I can write small building blocks with Dockerfiles; being able to layer these images on top of one other opens so much possibility!

Language Images

I can write simple language images to provide a base for running processes in those languages.

My ruby Dockerfile (with Docker I never have to worry about gemsets again!)

FROM debian:jessie
MAINTAINER Nicholas Shook, nicholas.shook@gmail.com

# install ruby dependencies
RUN apt-get update && apt-get install -y \
      build-essential \
      zlib1g-dev \
      libssl-dev \
      libreadline6-dev \
      libyaml-dev

# install ruby from source and cleanup afterward (from murielsalvan/ruby)
ADD http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.1.tar.gz /tmp/
RUN cd /tmp && \
      tar -xzf ruby-2.1.1.tar.gz && \
      cd ruby-2.1.1 && \
      ./configure && \
      make && \
      make install && \
      cd .. && \
      rm -rf ruby-2.1.1 && \
      rm -f ruby-2.1.1.tar.gz

Now I can run a command like

docker run shicholas/ruby ruby -v
  # ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-linux]

Similarly, installing go is pretty straightforward

FROM debian:jessie
MAINTAINER Nicholas Shook, nicholas.shook@gmail.com

RUN apt-get update && apt-get install -y \
      golang \
      git

ENV GOPATH /usr/lib/go/bin
RUN mkdir -p "$GOPATH"
ENV PATH $GOPATH:$PATH

I can use this image as a base for another image that executes go commands.

Database Images

Databases are also something I can use layering to write small Dockerfiles. Take for example redis, here is my base image:

FROM debian:jessie
MAINTAINER Nicholas Shook, nicholas.shook@gmail.com

RUN apt-get update && apt-get install -y \
      wget \
      build-essential

RUN wget http://download.redis.io/redis-stable.tar.gz
RUN tar xvzf redis-stable.tar.gz

And now I can build two images on top of that, one for the server and one for the client.

FROM shicholas/redis
MAINTAINER Nicholas Shook, nicholas.shook@gmail.com

EXPOSE 6379
CMD cd redis-stable && src/redis-server

and

FROM shicholas/redis
MAINTAINER Nicholas Shook, nicholas.shook@gmail.com

EXPOSE 6380
CMD cd redis-stable && src/redis-cli -h $REDIS_PORT_6379_TCP_ADDR

Now I can run these commands to set up a redis-cli talking to a redis-server

docker run -d --name=redis shicholas/redis-server  
docker run --name=redis-cli --link=redis:redis -i -t shicholas/redis-cli

I've never have gotten into dev-ops, until I met Docker. Being able to write small components, that I can track in source control, to handle all of my server processes is just really damn exciting.