16/09/2018 - DOCKER, LINUX
In this example we are going to keep native docker commands in Makefile and use make
command to interact with them.
All commands are listed below as follows. The TAG
variable is compulsory unless it is set on server level.
Builds an image. This bakes application into the image.
Tags an image.
Starts a container.
Stops a container.
Restarts a container.
Pulls an image.
Pushes an image.
Cleans all docker artifacts.
Login Docker Hub.
.
├── app
│ ├── a.php
│ └── b.php
├── docker
│ ├── Makefile
│ └── php
│ └── Dockerfile
└── .gitignore
echo 'Hello a'.PHP_EOL;
echo 'Hello b'.PHP_EOL;
FROM php:7.2-cli-alpine
COPY ./app /app
CMD tail -f /dev/null
ifndef TAG
$(error The TAG variable is missing.)
endif
ACCOUNT := inanzzz
SERVICE := hello_php
IMAGE := $(ACCOUNT)/$(SERVICE)
build:
$(info Make: Building "$(TAG)" tagged images.)
@docker build -t $(IMAGE):$(TAG) -f php/Dockerfile ..
@make -s tag
@make -s clean
tag:
$(info Make: Tagging image with "$(TAG)".)
@docker tag $(IMAGE):latest $(IMAGE):$(TAG)
start:
$(info Make: Starting "$(TAG)" tagged container.)
@docker run -dit --name $(SERVICE) $(IMAGE):$(TAG)
stop:
$(info Make: Stopping "$(TAG)" tagged container.)
@docker stop $(SERVICE)
@docker rm $(SERVICE)
restart:
$(info Make: Restarting "$(TAG)" tagged container.)
@make -s stop
@make -s start
push:
$(info Make: Pushing "$(TAG)" tagged image.)
@docker push $(IMAGE):$(TAG)
pull:
$(info Make: Pulling "$(TAG)" tagged image.)
@docker pull $(IMAGE):$(TAG)
clean:
@docker system prune --volumes --force
login:
$(info Make: Login to Docker Hub.)
@docker login -u $(DOCKER_USER) -p $(DOCKER_PASS)