16/09/2018 - DOCKER, LINUX
In this example we are going to keep docker compose commands in Makefile and use make
command to interact with them.
All commands are listed below as follows. The TAG
and ENV
variables are compulsory unless they are set on server level.
Builds all images.
Starts all containers. The test
and dev
env will have application exposed to the host OS. In the case of prod
and stag
env, the application will be baked into the container so it won't be exposed to the host OS.
Stops all containers.
Restarts all containers.
Pulls the image.
Pushes an image.
Cleans all docker artifacts.
Login Docker Hub.
.
├── app
│ ├── a.php
│ └── b.php
├── docker
│ ├── docker-compose.override.yml
│ ├── docker-compose.yml
│ ├── .env
│ ├── 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
ACCOUNT_NAME=inanzzz
COMPOSE_PROJECT_NAME=hello
TAG=latest
version: '3'
services:
php:
volumes:
- ../app:/app:rw
version: '3'
services:
php:
build:
context: ..
dockerfile: ./docker/php/Dockerfile
image: "${ACCOUNT_NAME}/${COMPOSE_PROJECT_NAME}_php:${TAG}"
hostname: php
working_dir: /app
ifndef TAG
$(error The TAG variable is missing.)
endif
ifndef ENV
$(error The ENV variable is missing.)
endif
ifeq ($(filter $(ENV),test dev stag prod),)
$(error The ENV variable is invalid.)
endif
ifeq (,$(filter $(ENV),test dev))
COMPOSE_FILE_PATH := -f docker-compose.yml
endif
IMAGE := inanzzz/hello_php
build:
$(info Make: Building "$(ENV)" environment images.)
@TAG=$(TAG) docker-compose build --no-cache
@make -s clean
start:
$(info Make: Starting "$(ENV)" environment containers.)
@TAG=$(TAG) docker-compose $(COMPOSE_FILE_PATH) up -d
stop:
$(info Make: Stopping "$(ENV)" environment containers.)
@docker-compose stop
restart:
$(info Make: Restarting "$(ENV)" environment containers.)
@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)