2021-07-12 21:04:06 +02:00
DOCKER_ENV_ARGS ?= $( docker_env_args)
2021-02-09 17:05:00 +01:00
DOCKER_EXEC_OPTIONS ?=
2021-07-11 09:56:03 +02:00
DOCKER_GID ?= $( call gid,docker)
2022-08-24 11:34:07 +02:00
DOCKER_IMAGE ?= $( USER_DOCKER_IMAGE)
2022-11-11 23:37:27 +01:00
DOCKER_MACHINE ?= $( shell docker run --rm alpine uname -m 2>/dev/null)
2022-08-24 11:34:07 +02:00
DOCKER_NAME ?= $( USER_DOCKER_NAME)
2022-11-25 06:00:37 +01:00
DOCKER_NETWORK ?= $( if $( filter User,$( firstword $( subst /, ,$( STACK) ) ) ) ,$( USER) ,$( DOCKER_NETWORK_PRIVATE) )
DOCKER_NETWORK_PRIVATE ?= $( USER) -$( ENV)
DOCKER_NETWORK_PUBLIC ?= $( HOSTNAME)
2022-01-09 17:32:54 +01:00
# DOCKER_RUN: if empty, run system command, else run it in a docker
2022-02-07 15:09:51 +01:00
DOCKER_RUN ?= $( if $( filter-out false False FALSE,$( DOCKER) ) ,$( DOCKER) )
2022-11-06 00:27:14 +01:00
DOCKER_RUN_LABELS ?= $( patsubst %,-l %,$( DOCKER_LABELS) )
2021-06-14 12:52:48 +02:00
# DOCKER_RUN_OPTIONS: default options of `docker run` command
2022-11-06 00:27:14 +01:00
DOCKER_RUN_OPTIONS += --rm --network $( DOCKER_NETWORK)
2021-06-14 12:52:48 +02:00
# DOCKER_RUN_VOLUME: options -v of `docker run` command to mount additionnal volumes
DOCKER_RUN_VOLUME += -v /var/run/docker.sock:/var/run/docker.sock
DOCKER_RUN_WORKDIR ?= -w $( PWD)
2022-11-11 23:37:27 +01:00
DOCKER_SYSTEM ?= $( shell docker run --rm alpine uname -s 2>/dev/null)
2022-11-29 17:22:35 +01:00
ENV_VARS += DOCKER_MACHINE DOCKER_NETWORK DOCKER_NETWORK_PRIVATE DOCKER_NETWORK_PUBLIC DOCKER_SYSTEM HOST_COMPOSE_PROJECT_NAME HOST_COMPOSE_SERVICE_NAME HOST_DOCKER_REPOSITORY HOST_DOCKER_VOLUME HOST_GID HOST_UID USER_COMPOSE_PROJECT_NAME USER_COMPOSE_SERVICE_NAME USER_DOCKER_IMAGE USER_DOCKER_NAME USER_DOCKER_REPOSITORY USER_DOCKER_VOLUME
HOST_COMPOSE_PROJECT_NAME ?= $( HOSTNAME)
HOST_COMPOSE_SERVICE_NAME ?= $( subst _,-,$( HOST_COMPOSE_PROJECT_NAME) )
HOST_DOCKER_REPOSITORY ?= $( subst -,/,$( subst _,/,$( HOST_COMPOSE_PROJECT_NAME) ) )
HOST_DOCKER_VOLUME ?= $( HOST_COMPOSE_PROJECT_NAME)
HOST_GID ?= 100
HOST_UID ?= 123
2022-11-25 06:00:37 +01:00
RESU_DOCKER_REPOSITORY ?= $( subst -,/,$( subst _,/,$( USER_COMPOSE_PROJECT_NAME) ) )
USER_COMPOSE_PROJECT_NAME ?= $( strip $( RESU) )
2022-11-27 03:33:21 +01:00
USER_COMPOSE_SERVICE_NAME ?= $( subst _,-,$( subst .,-,$( USER_COMPOSE_PROJECT_NAME) ) )
2022-11-22 23:49:44 +01:00
USER_DOCKER_IMAGE ?= $( USER_DOCKER_REPOSITORY) :${ DOCKER_IMAGE_TAG }
USER_DOCKER_NAME ?= $( USER_COMPOSE_PROJECT_NAME)
2022-11-25 06:00:37 +01:00
USER_DOCKER_REPOSITORY ?= $( subst -,/,$( subst _,/,$( USER) ) )
2022-11-22 23:49:44 +01:00
USER_DOCKER_VOLUME ?= $( USER_COMPOSE_PROJECT_NAME)
2022-06-30 23:37:10 +02:00
# https://github.com/docker/libnetwork/pull/2348
2022-10-12 02:50:02 +02:00
i f e q ( $( SYSTEM ) , D a r w i n )
2022-11-11 23:37:27 +01:00
DOCKER_HOST_IFACE ?= $( shell docker run --rm --net= host alpine /sbin/ip -4 route list match 0/0 2>/dev/null | awk '{print $$5}' | awk '!seen[$$0]++' | head -1)
DOCKER_HOST_INET4 ?= $( shell docker run --rm --net= host alpine /sbin/ip -4 addr show $( DOCKER_HOST_IFACE) 2>/dev/null | awk '$$1 == "inet" {sub(/\/.*/,"",$$2); print $$2}' | head -1)
DOCKER_INTERNAL_DOCKER_GATEWAY ?= $( shell docker run --rm alpine nslookup -type= A -timeout= 1 gateway.docker.internal 2>/dev/null | awk 'found && /^Address:/ {print $$2; found=0}; /^Name:\tgateway.docker.internal/ {found=1};' | head -1)
DOCKER_INTERNAL_DOCKER_HOST ?= $( or $( shell docker run --rm alpine nslookup -type= A -timeout= 1 host.docker.internal 2>/dev/null | awk 'found && /^Address:/ {print $$2; found=0}; /^Name:\thost.docker.internal/ {found=1};' | head -1) ,$( shell docker run --rm alpine /sbin/ip -4 route list match 0/0 2>/dev/null | awk '{print $$3}' | awk '!seen[$$0]++' | head -1) )
2022-06-30 23:37:10 +02:00
e l s e
DOCKER_HOST_IFACE ?= $( shell /sbin/ip -4 route list match 0/0 2>/dev/null | awk '{print $$5}' | awk '!seen[$$0]++' | head -1)
DOCKER_HOST_INET4 ?= $( shell /sbin/ip -4 addr show $( DOCKER_HOST_IFACE) 2>/dev/null | awk '$$1 == "inet" {sub(/\/.*/,"",$$2); print $$2}' | head -1)
DOCKER_INTERNAL_DOCKER_GATEWAY ?= $( shell /sbin/ip -4 route list match 0/0 2>/dev/null | awk '{print $$3}' | awk '!seen[$$0]++' | head -1)
DOCKER_INTERNAL_DOCKER_HOST ?= $( shell /sbin/ip addr show docker0 2>/dev/null | awk '$$1 == "inet" {sub(/\/.*/,"",$$2); print $$2}' | head -1)
e n d i f
2021-02-09 17:05:00 +01:00
i f e q ( $( DRONE ) , t r u e )
# When running docker command in drone, we are already in a docker (dind).
# Whe need to find the volume mounted in the current docker (runned by drone) to mount it in our docker command.
# If we do not mount the volume in our docker, we wont be able to access the files in this volume as the /drone/src directory would be empty.
2021-06-14 12:52:48 +02:00
DOCKER_RUN_VOLUME += -v $$ ( docker inspect $$ ( basename $$ ( cat /proc/1/cpuset) ) 2>/dev/null | awk 'BEGIN {FS=":"} $$0 ~ /"drone-[a-zA-Z0-9]*:\/drone"$$/ {gsub(/^[ \t\r\n]*"/,"",$$1); print $$1; exit}' ) :/drone $( if $( wildcard /root/.netrc) ,-v /root/.netrc:/root/.netrc)
2021-02-09 17:05:00 +01:00
e l s e
2021-06-14 12:52:48 +02:00
DOCKER_RUN_VOLUME += -v $( or $( APP_PARENT_DIR) ,$( APP_DIR) ,$( PWD) ) :$( or $( WORKSPACE_DIR) ,$( APP_PARENT_DIR) ,$( APP_DIR) ,$( PWD) )
2021-02-09 17:05:00 +01:00
e n d i f
2021-06-16 13:19:52 +02:00
i f n e q ( $( DOCKER_RUN ) , )
2021-02-09 17:05:00 +01:00
2022-08-24 11:34:07 +02:00
DOCKER_SSH_AUTH := -e SSH_AUTH_SOCK = /tmp/ssh-agent/socket -v $( USER_DOCKER_VOLUME) :/tmp/ssh-agent
2021-02-09 17:05:00 +01:00
2021-06-14 12:52:48 +02:00
# function docker-run: Run docker image 2 with arg 1
2021-02-09 17:05:00 +01:00
d e f i n e d o c k e r - r u n
2021-06-12 03:13:01 +02:00
$( call INFO,docker-run,$( 1) $( comma) $( 2) )
2021-06-14 12:52:48 +02:00
$( call run,$( or $( 2) ,$( DOCKER_IMAGE) ) $( 1) )
2021-02-09 17:05:00 +01:00
e n d e f
i f e q ( $( DRONE ) , t r u e )
2021-06-14 12:52:48 +02:00
# function exec DRONE=true: Run DOCKER_IMAGE with arg 1
2021-02-09 17:05:00 +01:00
d e f i n e e x e c
2021-06-12 03:13:01 +02:00
$( call INFO,exec,$( 1) )
2021-06-14 12:52:48 +02:00
$( call run,$( DOCKER_IMAGE) $( or $( 1) ,$( SHELL) ) )
2021-02-09 17:05:00 +01:00
e n d e f
e l s e
2022-11-19 02:03:37 +01:00
# function exec: call docker-exec
2021-02-09 17:05:00 +01:00
d e f i n e e x e c
2021-06-12 03:13:01 +02:00
$( call INFO,exec,$( 1) )
2022-11-22 23:49:44 +01:00
$( call docker-exec,$( 1) )
2021-02-09 17:05:00 +01:00
e n d e f
e n d i f
2021-06-14 12:52:48 +02:00
# function run: Run docker run with arg 1 and docker repository 2
## attention: arg 2 should end with slash or space
2021-02-09 17:05:00 +01:00
d e f i n e r u n
2021-06-14 12:52:48 +02:00
$( call INFO,run,$( 1) $( comma) $( 2) )
2022-11-19 02:03:37 +01:00
$( if $( DOCKER_RUN_NAME) ,
$( if $( call docker-running,^$( DOCKER_RUN_NAME) $$ ) ,
$( call ERROR,Found already running docker,$( DOCKER_RUN_NAME) )
2022-11-25 06:00:37 +01:00
)
2022-11-19 02:03:37 +01:00
)
2022-11-06 00:27:14 +01:00
$( RUN) docker run $( DOCKER_ENV_ARGS) $( DOCKER_RUN_LABELS) $( DOCKER_RUN_OPTIONS) $( DOCKER_RUN_VOLUME) $( DOCKER_RUN_WORKDIR) $( DOCKER_SSH_AUTH) $( DOCKER_RUN_NAME) $( 2) $( 1)
2021-02-09 17:05:00 +01:00
e n d e f
e l s e
SHELL := /bin/bash
2021-06-14 12:52:48 +02:00
# function docker-run DOCKER=false: Run docker image 2 with arg 1
2021-02-09 17:05:00 +01:00
d e f i n e d o c k e r - r u n
2021-06-12 03:13:01 +02:00
$( call INFO,docker-run,$( 1) $( comma) $( 2) )
2022-11-19 02:03:37 +01:00
$( if $( DOCKER_RUN_NAME) ,
$( if $( call docker-running,^$( DOCKER_RUN_NAME) $$ ) ,
$( call ERROR,Found already running docker,$( DOCKER_RUN_NAME) )
)
)
2022-11-06 00:27:14 +01:00
$( RUN) docker run $( DOCKER_ENV_ARGS) $( DOCKER_RUN_LABELS) $( DOCKER_RUN_OPTIONS) $( DOCKER_RUN_VOLUME) $( DOCKER_RUN_WORKDIR) $( DOCKER_RUN_NAME) $( or $( 2) ,$( DOCKER_IMAGE) ) $( 1)
2021-02-09 17:05:00 +01:00
e n d e f
2021-06-14 12:52:48 +02:00
# function exec DOCKER=false: Call env-exec with arg 1 or SHELL
2021-02-09 17:05:00 +01:00
d e f i n e e x e c
2021-06-12 03:13:01 +02:00
$( call INFO,exec,$( 1) )
2022-11-19 02:03:37 +01:00
$( call env-exec,$( RUN) $( or $( 1) ,$( SHELL) ) )
2021-02-09 17:05:00 +01:00
e n d e f
2022-11-19 02:03:37 +01:00
# function run DOCKER=false: Call exec with arg 1
2021-02-09 17:05:00 +01:00
d e f i n e r u n
2021-06-12 03:13:01 +02:00
$( call INFO,run,$( 1) )
2022-11-19 02:03:37 +01:00
$( call exec,$( 1) )
2021-02-09 17:05:00 +01:00
e n d e f
e n d i f
2022-11-19 02:03:37 +01:00
# function docker-attach: Attach docker 1 or DOCKER_NAME
d e f i n e d o c k e r - a t t a c h
$( call INFO,docker-attach,$( 1) $( comma) )
$( eval attach := $( or $( 1) ,$( DOCKER_NAME) ) )
$( if $( call docker-running,^$( attach) $) ,
$( RUN) docker attach $( attach)
, $( call ERROR,Unable to find docker,$( attach) )
)
e n d e f
# function docker-connect: Call docker-exec
d e f i n e d o c k e r - c o n n e c t
$( call INFO,docker-connect,$( 1) $( comma) )
$( call docker-exec,$( DOCKER_SHELL) )
e n d e f
# function docker-exec: Exec arg 1 in docker DOCKER_NAME
d e f i n e d o c k e r - e x e c
$( call INFO,docker-exec,$( 1) )
$( if $( call docker-running,^$( DOCKER_NAME) $) ,
$( RUN) docker exec $( DOCKER_ENV_ARGS) $( DOCKER_EXEC_OPTIONS) $( DOCKER_RUN_WORKDIR) $( DOCKER_NAME) $( or $( 1) ,$( SHELL) )
, $( call ERROR,Unable to find docker,$( DOCKER_NAME) )
)
e n d e f
# function docker-logs: Print logs of docker 1 or DOCKER_NAME
d e f i n e d o c k e r - l o g s
$( call INFO,docker-logs,$( 1) )
$( eval logs := $( or $( 1) ,$( DOCKER_NAME) ) )
$( if $( call docker-running,^$( logs) $) ,
$( RUN) docker logs --follow --tail= 100 $( logs)
, $( call ERROR,Unable to find docker,$( logs) )
)
e n d e f
# function docker-file: eval DOCKER_FILE in dir 1 or APP_DIR
d e f i n e d o c k e r - f i l e
$( call INFO,docker-file,$( 1) $( comma) )
$( eval dir := $( or $( 1) ,$( APP_DIR) ) )
$( eval DOCKER_FILE := $( wildcard $( dir) /docker/*/Dockerfile $( dir) /*/Dockerfile $( dir) /Dockerfile) )
$( if $( DOCKER_FILE) ,
, $( call ERROR,Unable to find a,Dockerfile,in dir,$( dir) )
)
e n d e f
# function docker-running: Print running dockers matching DOCKER_NAME
d e f i n e d o c k e r - r u n n i n g
$( shell docker ps -q $ ( patsubst %,-f name =%,$ ( or $ ( 1) , ^$ ( DOCKER_NAME ) $ $ , ^$ ) ) 2>/dev /null )
e n d e f
# function docker-rm: Remove docker 1
d e f i n e d o c k e r - r m
$( call INFO,docker-rm,$( 1) $( comma) )
$( eval rm := $( or $( 1) ,$( DOCKER_NAME) ) )
$( if $( call docker-running,^$( rm) $) ,
$( call WARNING,Removing running docker,$( rm) )
)
$( RUN) docker rm -f $( rm)
e n d e f
2021-06-02 00:54:33 +02:00
# function docker-volume-copy: Copy files from a docker volume to another
2021-02-09 17:05:00 +01:00
d e f i n e d o c k e r - v o l u m e - c o p y
2021-06-12 03:13:01 +02:00
$( call INFO,docker-volume-copy,$( 1) $( comma) $( 2) )
2021-02-09 17:05:00 +01:00
$( eval from := $( 1) )
$( eval to := $( 2) )
2021-06-12 03:13:01 +02:00
$( RUN) docker volume inspect $( from) >/dev/null
$( RUN) docker volume inspect $( to) >/dev/null 2>& 1 || $( RUN) docker volume create $( to) >/dev/null
$( RUN) docker run --rm -v $( from) :/from -v $( to) :/to alpine ash -c "cd /from; cp -a . /to"
2021-02-09 17:05:00 +01:00
e n d e f