Skip to content

Commit afbf1ee

Browse files
[sonic-slave]: Split user commands from generic. (#868)
In case when more than one user builds SONiC on same machine, we can keep generic part that installs all packages to slave image apart from creating user and calling user-related commands. Then generic base image will be built only once, allowing other users to build only smaller specific to them layers. Signed-off-by: marian-pritsak <[email protected]>
1 parent 97e4360 commit afbf1ee

File tree

3 files changed

+46
-30
lines changed

3 files changed

+46
-30
lines changed

Makefile

+17-2
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,28 @@ $(shell rm -f .screen)
1212

1313
MAKEFLAGS += -B
1414

15-
SLAVE_TAG = $(shell shasum sonic-slave/Dockerfile | awk '{print substr($$1,0,11);}')
15+
SLAVE_BASE_TAG = $(shell shasum sonic-slave/Dockerfile | awk '{print substr($$1,0,11);}')
16+
SLAVE_TAG = $(shell shasum sonic-slave/Dockerfile.user | awk '{print substr($$1,0,11);}')
17+
SLAVE_BASE_IMAGE = sonic-slave-base
1618
SLAVE_IMAGE = sonic-slave-$(USER)
1719

1820
DOCKER_RUN := docker run --rm=true --privileged \
1921
-v $(PWD):/sonic \
2022
-w /sonic \
2123
-i$(if $(TERM),t,)
2224

25+
DOCKER_BASE_BUILD = docker build --no-cache \
26+
-t $(SLAVE_BASE_IMAGE) \
27+
sonic-slave && \
28+
docker tag $(SLAVE_BASE_IMAGE):latest $(SLAVE_BASE_IMAGE):$(SLAVE_BASE_TAG)
29+
2330
DOCKER_BUILD = docker build --no-cache \
2431
--build-arg user=$(USER) \
2532
--build-arg uid=$(shell id -u) \
2633
--build-arg guid=$(shell id -g) \
2734
--build-arg hostname=$(shell echo $$HOSTNAME) \
2835
-t $(SLAVE_IMAGE) \
36+
-f sonic-slave/Dockerfile.user \
2937
sonic-slave && \
3038
docker tag $(SLAVE_IMAGE):latest $(SLAVE_IMAGE):$(SLAVE_TAG)
3139

@@ -34,6 +42,9 @@ DOCKER_BUILD = docker build --no-cache \
3442
.DEFAULT_GOAL := all
3543

3644
%::
45+
@docker inspect --type image $(SLAVE_BASE_IMAGE):$(SLAVE_BASE_TAG) &> /dev/null || \
46+
{ echo Image $(SLAVE_BASE_IMAGE):$(SLAVE_BASE_TAG) not found. Building... ; \
47+
$(DOCKER_BASE_BUILD) ; }
3748
@docker inspect --type image $(SLAVE_IMAGE):$(SLAVE_TAG) &> /dev/null || \
3849
{ echo Image $(SLAVE_IMAGE):$(SLAVE_TAG) not found. Building... ; \
3950
$(DOCKER_BUILD) ; }
@@ -49,9 +60,13 @@ DOCKER_BUILD = docker build --no-cache \
4960
$@
5061

5162
sonic-slave-build :
52-
@$(DOCKER_BUILD)
63+
$(DOCKER_BASE_BUILD)
64+
$(DOCKER_BUILD)
5365

5466
sonic-slave-bash :
67+
@docker inspect --type image $(SLAVE_BASE_IMAGE):$(SLAVE_BASE_TAG) &> /dev/null || \
68+
{ echo Image $(SLAVE_BASE_IMAGE):$(SLAVE_BASE_TAG) not found. Building... ; \
69+
$(DOCKER_BASE_BUILD) ; }
5570
@docker inspect --type image $(SLAVE_IMAGE):$(SLAVE_TAG) &> /dev/null || \
5671
{ echo Image $(SLAVE_IMAGE):$(SLAVE_TAG) not found. Building... ; \
5772
$(DOCKER_BUILD) ; }

sonic-slave/Dockerfile

-28
Original file line numberDiff line numberDiff line change
@@ -254,31 +254,3 @@ RUN add-apt-repository \
254254
RUN apt-get update
255255
RUN apt-get install -y docker-ce=17.03.2~ce-0~debian-jessie
256256
RUN echo "DOCKER_OPTS=\"--experimental\"" >> /etc/default/docker
257-
258-
# Add user
259-
ARG user
260-
ARG uid
261-
ARG guid
262-
ARG hostname
263-
264-
ENV BUILD_HOSTNAME $hostname
265-
ENV USER $user
266-
267-
RUN groupadd -f -r -g $guid g$user
268-
269-
RUN useradd $user -l -u $uid -g $guid -d /var/$user -m -s /bin/bash
270-
271-
RUN gpasswd -a $user docker
272-
273-
# Config git for stg
274-
RUN su $user -c "git config --global user.name $user"
275-
RUN su $user -c "git config --global user.email [email protected]"
276-
277-
COPY sonic-jenkins-id_rsa.pub /var/$user/.ssh/authorized_keys2
278-
RUN chown $user /var/$user/.ssh -R
279-
RUN chmod go= /var/$user/.ssh -R
280-
281-
# Add user to sudoers
282-
RUN echo "$user ALL=(ALL) NOPASSWD:ALL" >>/etc/sudoers
283-
284-
USER $user

sonic-slave/Dockerfile.user

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
FROM sonic-slave-base
2+
3+
# Add user
4+
ARG user
5+
ARG uid
6+
ARG guid
7+
ARG hostname
8+
9+
ENV BUILD_HOSTNAME $hostname
10+
ENV USER $user
11+
12+
RUN groupadd -f -r -g $guid g$user
13+
14+
RUN useradd $user -l -u $uid -g $guid -d /var/$user -m -s /bin/bash
15+
16+
RUN gpasswd -a $user docker
17+
18+
# Config git for stg
19+
RUN su $user -c "git config --global user.name $user"
20+
RUN su $user -c "git config --global user.email [email protected]"
21+
22+
COPY sonic-jenkins-id_rsa.pub /var/$user/.ssh/authorized_keys2
23+
RUN chown $user /var/$user/.ssh -R
24+
RUN chmod go= /var/$user/.ssh -R
25+
26+
# Add user to sudoers
27+
RUN echo "$user ALL=(ALL) NOPASSWD:ALL" >>/etc/sudoers
28+
29+
USER $user

0 commit comments

Comments
 (0)