Makefile + Dockerfile to boost  your productivity
Published: 2017-10-07 16:41:45

---
title: "Makefile + Dockerfile to boost  your productivity"
published: true
tags: 
canonical_url: 
---

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](https://github.com/tj) building his React components, his Go projects and everything else he did with them.

[Simon Dittlman’s post](http://www.itnotes.de/docker/development/tools/2014/08/31/speed-up-your-docker-workflow-with-a-makefile/) on Dockerfiles and Makefiles, by automating several common commands, convinced him pretty quickly.

Here is what I use:

<noscript><pre>PORTS = -p 35729:35729 -p 4000:80
ENV = \
    -e VERSION=latest
VOLUMES = \
  -v <code>pwd</code>:data</pre></noscript><script src="https://gist.github.com/EarvinKayonga/6038c56cc949c2b93028faaee1cf4e31.js?file=env_make"> </script>

<noscript><pre>include env_make

<p>NS = earvin/blog
VERSION ?= latest</p>

<p>REPO = earvin
NAME = earvin
INSTANCE = default</p>

<p>.PHONY: build push shell run start stop logs rm release dev</p>

<p>deps:
    gem update –no-ri –no-rdoc &amp;&amp; gem install jekyll pygments.rb rdiscount rouge –no-ri –no-rdoc</p>

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

<p>push:
    docker push $(NS)/$(REPO):$(VERSION)</p>

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

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

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

<p>stop:
    docker stop $(NAME)-$(INSTANCE)</p>

<p>rm:
    docker rm -f $(NAME)-$(INSTANCE)</p>

<p>release: build
    make push -e VERSION=$(VERSION)</p>

<p>dev:
    jekyll serve –watch –source=blog –incremental</p>

<p>logs:
    docker logs $(NAME)-$(INSTANCE)</p>

<p>default: build
</p></pre></noscript><script src="https://gist.github.com/EarvinKayonga/6038c56cc949c2b93028faaee1cf4e31.js?file=Makefile"> </script>

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
```

NB: <sub>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.</sub>

#### **make test** :

To test the Docker Container. I used that to test my Docker Registry by just running`make 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
```

### Sources:

- [Simon Dittlmann’s Blog post on how to speed your Docker Workflow with a Makefile](http://www.itnotes.de/docker/development/tools/2014/08/31/speed-up-your-docker-workflow-with-a-makefile/)
- [TJ on Minimizing boilerplate with NPM & Makefiles](https://medium.com/@tjholowaychuk/minimizing-boilerplate-with-npm-makefiles-3cfdce2934e7#.t2yeyure4)