Sign inSign up

rubensa/archlinux

By rubensa

Updated 8 months ago

Docker image based on archlinux with non root user support

Image
Operating systems
0

363

rubensa/archlinux repository overview

Docker image based on archlinux with non root user support

GitHub repository.

This is a Docker image based on archlinux that allows you to connect and run with a non-root user created inside the image.

The internal user (user) has sudo and the image includes fixuid so you can set internal user (user) UID and internal group (group) GID to your current UID and GID by providing that info means of "--user" docker running option.

Building

You can build the image like this:

#!/usr/bin/env bash

DOCKER_REPOSITORY_NAME="rubensa"
DOCKER_IMAGE_NAME="archlinux"
DOCKER_IMAGE_TAG="latest"

docker build --no-cache \
  -t "${DOCKER_REPOSITORY_NAME}/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}" \
  .

You can also add build image args to change default non-root user (user:1000) and group (group:1000) like this:

#!/usr/bin/env bash

DOCKER_REPOSITORY_NAME="rubensa"
DOCKER_IMAGE_NAME="archlinux"
DOCKER_IMAGE_TAG="latest"

# Get current user UID
USER_ID=$(id -u)
# Get current user main GID
GROUP_ID=$(id -g)
# Get current user name
USER_NAME=$(id -un)
# Get current user main group name
GROUP_NAME=$(id -gn)

# Get current user UID
USER_ID=2000
# Get current user main GID
GROUP_ID=2000
# Get current user name
USER_NAME=testuser
# Get current user main group name
GROUP_NAME=testgroup

prepare_docker_user_and_group() {
  # On build, if you specify USER_NAME, USER_ID, GROUP_NAME or GROUP_ID those are used to define the
  # internal user and group created instead of default ones (user:1000 and group:1000)
  BUILD_ARGS+=" --build-arg USER_ID=$USER_ID"
  BUILD_ARGS+=" --build-arg GROUP_ID=$GROUP_ID"
  BUILD_ARGS+=" --build-arg USER_NAME=$USER_NAME"
  BUILD_ARGS+=" --build-arg GROUP_NAME=$GROUP_NAME"
}

prepare_docker_user_and_group

docker build --no-cache \
  -t "${DOCKER_REPOSITORY_NAME}/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}" \
  ${BUILD_ARGS} \
  .

But this is generally not needed as the container can change user UID and group GID on run if "--user" option is provided (see bellow).

Running

You can run the container like this:

#!/usr/bin/env bash

DOCKER_REPOSITORY_NAME="rubensa"
DOCKER_IMAGE_NAME="archlinux"
DOCKER_IMAGE_TAG="latest"

# Get current user UID
USER_ID=$(id -u)
# Get current user main GID
GROUP_ID=$(id -g)

prepare_docker_timezone() {
  # https://www.waysquare.com/how-to-change-docker-timezone/
  ENV_VARS+=" --env=TZ=$(realpath --relative-to /usr/share/zoneinfo /etc/localtime)"
}

prepare_docker_user_and_group() {
  RUNNER+=" --user=${USER_ID}:${GROUP_ID}"
}

prepare_docker_userdata_volumes() {
  # Shared working directory
  MOUNTS+=" --mount type=bind,source=${PWD},target=/work"
}
prepare_docker_timezone
prepare_docker_user_and_group
prepare_docker_userdata_volumes

docker run --rm --init -it \
  --name "${DOCKER_IMAGE_NAME}" \
  ${ENV_VARS} \
  ${RUNNER} \
  ${MOUNTS} \
  "${DOCKER_REPOSITORY_NAME}/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}" "$@"

This way, the internal user UID and group GID are changed to the current host user:group launching the container and the existing files under his internal HOME directory that where owned by user and group are also updated to belong to the new UID:GID.

Also, a /work directory in the container is bind mounted to the current host working directory so you can share files between host and container.

Tag summary

Content type

Image

Digest

sha256:eaadb29d3

Size

183.1 MB

Last updated

8 months ago

docker pull rubensa/archlinux