rah606
rah606

Reputation: 89

Docker image - add user to group

I have an custom docker image, already builded. There is no Dockerfile available. Inside container, instead of root, an custom user, let's say test, is available. This user is attached to group test. This is default user for container.

I try to build my own image using described above base image, and add user test also to group workers. How to do this?

Using known commands (useradd, addgroup, usermod) known by me, is not helpful. After docker exec inside container, command id returns me only one group (test).

Also access to files mounted to container with permissions anotheruser:workers / 770 is not possible.

Is there a chance to do this?

Upvotes: 6

Views: 3399

Answers (1)

F1Linux
F1Linux

Reputation: 4373

Process:

A brief outline of the process for modifying your current image to incorporate changes into a new image is as follows:

  1. Create a Dockerfile with the current image and your changes to modify it
  2. Execute the docker build command to cut a new image from the Dockerfile
  3. Update your docker-compose.yml file's image: directive with the new image's name ("tag")

A Simple Worked Example:

Create a Dockerfile in the same directory as your docker-compose.yml file:

FROM zabbix/zabbix-agent2:ubuntu-6.0-latest
USER root
RUN groupadd -r docker -g 998 && usermod -aG docker zabbix

The original docker-compose.yml file containing the image you're currently using:

version: '3.5'
services:
 zabbix-agent:
  image: zabbix/zabbix-agent2:ubuntu-6.0-latest

<SNIP>

Execute the docker build command with the -t switch to give it a memorable name:

docker build -t zabbix/zabbix-agent2:ubuntu-6.0-latest-dockerfix --no-cache . -f Dockerfile-ZabbixAgent2

Then we change the original image's name in our docker-compose.yml file to the tag of the new image based on the changes in the Dockercompose file.

Comment-out the name of the base image and append the new tag below it:

version: '3.5'
services:
 zabbix-agent:
  #image: zabbix/zabbix-agent2:ubuntu-6.0-latest
   image: zabbix/zabbix-agent2:ubuntu-6.0-latest-dockerfix
<SNIP>

When you raise the container up now, you'll see the new changes to your users & groups from the RUN command in the Dockerfile.

A VERY detailed example can be found HERE if you need further context.

Hope this helps-

Upvotes: 3

Related Questions