↓ Skip to main content

Unpacking an archive as a non-root user during docker build

The way we build our WebSphere traditional Docker images is as a two-step process. First we install the products using Installation Manager and generate a tar file containing the installed product. Then we suck that tar file in to a clean image so that the resulting image does not contain all the cruft left lying around from the install process. (Not including Installation Manager in the final image also helps to reinforce that these images are intended to be immutable.)

The ADD Dockerfile command is very convenient for copying in a tar file from the context and unpacking it, all in one atomic operation. Unfortunately the ADD command ignores the current user and always unpacks as root. (Aside: Docker recently re-opened the proposal created by IBMer Megan Kostick to address this problem.) You could run a chown following the ADD but this results in all the files being copied in to a new layer (not cool when the contents of your tar file weighs in at 1.5GB!). Our initial starting point was to make sure that all the files already had the right ownership when they are added to the tar file. This involved creating the same user/group in the initial image and relying on getting the same uid/guid in the eventual image, something I wasn’t entirely happy with.

A related problem that we had run in to elsewhere was that the copy and unpack magic of ADD doesn’t extend to zip files, a format in which many of our install binaries are found. Where those binaries are already hosted somewhere, it’s simple enough to use wget or curl to pull the files, unpack, and perform any necessary cleanup as part of a RUN command. The obvious solution to my local tar or zip file was to host the file somehow. I decided to spin up a python container to serve up the files as follows:

docker run -d --name server -v $(pwd):/host -w /host \
  python:2-slim python -m SimpleHTTPServer
ip=$(docker inspect \
  --format '{{ .NetworkSettings.IPAddress }}' \
  server)
url="http://${ip}:8000/"

That URL can then be consumed in a subsequent build. For example, if I had example.tar.gz in the directory on the host, I could unpack as the user/group foo:bar in my image with the following Dockerfile:

FROM ubuntu:16.04
RUN apt-get update && apt-get install -y wget \
  && rm -rf /var/lib/apt/lists/* \
  && groupadd bar && useradd foo -g bar -m
ARG URL
USER foo
RUN wget -q -O - $URL/example.tar.gz | tar xz

To build the image, we then just need to pass in the URL as a build argument and, when we’re done, we can clean up the python container:

docker build --build-arg URL=$url .
docker rm -f server

The result of all of this is that we then get the default non-root behavior of tar which is to unpack as the current user.

Related

Containerizing background processes

The lifetime of a Docker container is tied to the lifetime of the PID 1 process executed when the container was started. WebSphere Liberty has a convenient server run command to run the application server in the foreground. Sadly, that’s not the case with the traditional WebSphere’s startServer.sh script which simply starts the server process in the background and then exits. To ensure that the container didn’t exit as well, we started out with a script something along the following lines:

WebSphere Liberty on IBM Containers: Part 2

Deploying an Application # In the first part of this series we looked at how to get started running a WebSphere Liberty image in IBM Containers using the Bluemix console. The container was just running an empty Liberty server. In this post we’ll look at building and deploying an image that adds an application. I was originally intending to stick to using the browser for this post but I think it’s actually easier to do this from the command line. I’m going to assume that you already have Docker installed locally, either natively on Linux, via Docker Machine, or via the Docker for Mac/Windows beta.