After several months using Docker, I started looking for ways to automate, or at least speed up the general routine of checking if:
container is up,
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:
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)
make build startwill build your docker container and start it in daemon mode.
make build runwill also build your docker container and run it
make logswill display logs
make releasewill build and publish Docker Images to Registry
Makefiles are extensible. New Rules could be easily defined:
To enter in running Docker Container
docker exec -i -t $(NAME)-$(INSTANCE) sh
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.
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