DEV Community

Earvin
Earvin

Posted on • Originally published at earvinkayonga.com on

Makefile + Dockerfile to boost your productivity

After several months using Docker, I started looking for ways to automate, or at least speed up the general routine of checking if:

  • the container is up,
  • looking for the name of the container,
  • etc..

I’ve used Makefiles in the past for C/C++ projects and completly forgot about them. Then I saw TJ building his React components, his Go projects and everything else he did with them.

Simon Dittlman’s post on Dockerfiles and Makefiles, by automating several common commands, convinced him pretty quickly.

Here is what I use:

PORTS = -p 35729:35729 -p 4000:80
ENV = \
-e VERSION=latest
VOLUMES = \
-v pwd:data
include env_make

NS = earvin/blog VERSION ?= latest

REPO = earvin NAME = earvin INSTANCE = default

.PHONY: build push shell run start stop logs rm release dev

deps: gem update –no-ri –no-rdoc && gem install jekyll pygments.rb rdiscount rouge –no-ri –no-rdoc

build: docker build -t $(NS)/$(REPO):$(VERSION) .

push: docker push $(NS)/$(REPO):$(VERSION)

shell: docker run –rm –name $(NAME)-$(INSTANCE) -i -t $(PORTS) $(ENV) $(NS)/$(REPO):$(VERSION) /bin/bash

run: docker run –rm –name $(NAME)-$(INSTANCE) $(PORTS) $(ENV) $(NS)/$(REPO):$(VERSION)

start: docker run -d –label=jekyll –name $(NAME)-$(INSTANCE) $(PORTS) $(ENV) $(NS)/$(REPO):$(VERSION)

stop: docker stop $(NAME)-$(INSTANCE)

rm: docker rm -f $(NAME)-$(INSTANCE)

release: build make push -e VERSION=$(VERSION)

dev: jekyll serve –watch –source=blog –incremental

logs: docker logs $(NAME)-$(INSTANCE)

default: build

Running:

  • make build start will build your docker container and start it in daemon mode.
  • make build run will also build your docker container and run it
  • make logs will display logs
  • make release will build and publish Docker Images to Registry

Makefiles are extensible. New Rules could be easily defined:

make ssh :

To enter in running Docker Container

docker exec -i -t $(NAME)-$(INSTANCE) sh
Enter fullscreen mode Exit fullscreen mode

NB: sh could be replaced by or bash or /bin/bash depending on if bash is installed in your images. My blog runs on Alpine based Docker Image: I uninstalled bash git g++ openssh to reduce its weight.

make test :

To test the Docker Container. I used that to test my Docker Registry by just runningmake build start test. To test my Docker Registry, I pull an Alpine from the Docker Hub and push it in my Local Registry

// USER and PASSWORD are either exported before running
  //make ssh or defined in the env_make file

  docker pull alpine
  docker tag alpine localhost:5000/alpine
  docker login -u $(USER) -p $(PASSWORD) localhost:5000/alpine
  docker push localhost:5000/alpine
Enter fullscreen mode Exit fullscreen mode

Sources:

Top comments (0)