Skip to content

Commit

Permalink
Add podman support for building in addition to docker
Browse files Browse the repository at this point in the history
Fix use of DOCKER variable

Add support for selinux
  • Loading branch information
pkegg committed Aug 24, 2021
1 parent 307ad7e commit 77b1e24
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,14 @@ docker-%: GID := $(shell id -g)
# PWD is 'present working directory' and passes through the full path to current dir to docker (becomes 'work')
docker-%: PWD := $(shell pwd)

# Command to use (either `docker` or `podman`)
docker-%: DOCKER_CMD:= $(shell if which docker 2>/dev/null 1>/dev/null; then echo "docker"; elif which podman 2>/dev/null 1>/dev/null; then echo "podman"; fi)

# Podman requires some extra args (`--userns=keep-id` and `--security-opt=label=disable`). Set those args if using podman
docker-%: PODMAN_ARGS:= $(shell if ! which docker 2>/dev/null 1>/dev/null && which podman 2> /dev/null 1> /dev/null; then echo "--userns=keep-id --security-opt=label=disable"; fi)

# Use 'sudo' if docker ps doesn't work. In theory, other things than missing sudo could cause this. But sudo needed is a common issue and easy to fix.
docker-%: SUDO := $(shell if ! docker ps -q 2> /dev/null 1> /dev/null; then echo "sudo"; fi)
docker-%: SUDO := $(shell if which docker 2> /dev/null 1> /dev/null && ! docker ps -q 2> /dev/null 1> /dev/null ; then echo "sudo"; fi)

# Launch docker as interactive if this is an interactive shell (allows ctrl-c for manual and running non-interactive - aka: build server)
docker-%: INTERACTIVE=$(shell [ -t 0 ] && echo "-it")
Expand All @@ -80,25 +86,27 @@ docker-%: INTERACTIVE=$(shell [ -t 0 ] && echo "-it")
docker-%: COMMAND=make $*

# Get .env file ready
docker-%: $(shell env > .env)
docker-%: $(shell env | grep "=" > .env)

# If the user issues a `make docker-shell` just start up bash as the shell to run commands
docker-shell: COMMAND=bash

# Command: builds docker image locally from Dockerfile
docker-image-build:
$(SUDO) docker build . -t $(DOCKER_IMAGE)
$(SUDO) $(DOCKER_CMD) build . -t $(DOCKER_IMAGE)

# Command: pulls latest docker image from dockerhub. This will *replace* locally built version.
docker-image-pull:
$(SUDO) docker pull $(DOCKER_IMAGE)
$(SUDO) $(DOCKER_CMD) pull $(DOCKER_IMAGE)

# Command: pushes the latest Docker image to dockerhub. This is *not* needed to build. It updates the latest build image in dockerhub for everyone.
# Only 351elec admins in dockerhub can do this.
#
# You must login with: docker login --username <username> and provide either a password or token (from user settings -> security in dockerhub) before this will work.
docker-image-push:
$(SUDO) docker push $(DOCKER_IMAGE)
$(SUDO) $(DOCKER_CMD) push $(DOCKER_IMAGE)

# Wire up docker to call equivalent make files using % to match and $* to pass the value matched by %
docker-%:
$(SUDO) docker run $(INTERACTIVE) --env-file .env --rm --user $(UID):$(GID) -v $(PWD):$(DOCKER_WORK_DIR) -w $(DOCKER_WORK_DIR) $(DOCKER_IMAGE) $(COMMAND)
$(SUDO) $(DOCKER_CMD) run $(PODMAN_ARGS) $(INTERACTIVE) --env-file .env --rm --user $(UID):$(GID) -v $(PWD):$(DOCKER_WORK_DIR) -w $(DOCKER_WORK_DIR) $(DOCKER_IMAGE) $(COMMAND)

0 comments on commit 77b1e24

Please sign in to comment.