Skip to content

Commit d3eec2c

Browse files
authored
Merge pull request #353 from mtconnect/reduce_docker_image_size
Reduced image size for demo and builds
2 parents 1a2de47 + 57d432e commit d3eec2c

File tree

16 files changed

+282
-634
lines changed

16 files changed

+282
-634
lines changed

.github/workflows/build-docker-image.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ jobs:
3535
# the type of runner that the job will run on
3636
runs-on: ubuntu-latest
3737
strategy:
38+
max-parallel: 1
3839
matrix:
39-
include:
40+
include:
4041
- dockerfile: docker/alpine/Dockerfile
4142
name: agent
42-
- dockerfile: demo/Dockerfile.alpine
43+
- dockerfile: demo/Dockerfile
4344
name: demo
4445

4546
# steps represent a sequence of tasks that will be executed as part of the job
@@ -63,7 +64,11 @@ jobs:
6364
with:
6465
username: ${{ secrets.DOCKERHUB_USERNAME }}
6566
password: ${{ secrets.DOCKERHUB_TOKEN }}
66-
67+
68+
- name: Remove leading v from the version
69+
run: |
70+
echo "VERSION=$(echo ${{ github.ref_name }} | sed 's/^v//')" >> $GITHUB_ENV
71+
6772
# see https://github.com/docker/build-push-action
6873
- name: Build image and push to DockerHub
6974
uses: docker/build-push-action@v5
@@ -75,7 +80,7 @@ jobs:
7580
# docker hub user/repo:tag
7681
tags: |
7782
${{ secrets.DOCKERHUB_USERNAME }}/${{ matrix.name }}:latest
78-
${{ secrets.DOCKERHUB_USERNAME }}/${{ matrix.name }}:${{ github.ref_name }}
83+
${{ secrets.DOCKERHUB_USERNAME }}/${{ matrix.name }}:${{ env.VERSION }}
7984
8085
# push to docker hub
8186
push: true

demo/Dockerfile

Lines changed: 29 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -10,193 +10,75 @@
1010
#
1111
# docker buildx build \
1212
# --platform linux/amd64,linux/arm64 \
13-
# --tag mtconnect/agent:2.0.0.12_RC18 \
13+
# --tag mtconnect/demo:latest \
1414
# --push \
15+
# -f demo/Dockerfile
1516
# .
1617
#
1718
# To run tests, use `--build-arg WITH_TESTS=true`
1819
#
1920
# # Note: In this case, I would suggest to map port `5000` to `5000`. The user can always change the port according to their needs.
20-
# docker run -it --rm --init --name agent -p5000:5000 \
21-
# mtconnect/agent:2.0.0.12_RC18
21+
# docker run -it --rm --init --name agent -p5000:5000 mtconnect/demo
2222
#
2323
# then visit http://localhost:5000 to see the demo output.
2424

2525
# ---------------------------------------------------------------------
26-
# os
26+
# Use the mtconnect/agent base image
2727
# ---------------------------------------------------------------------
28-
29-
# base image - ubuntu has amd64, arm64 etc.
30-
# 22.04 is the current long term support release, maintained until 2025-04.
31-
FROM ubuntu:22.04 AS os
32-
33-
# tzinfo hangs without this
34-
ARG DEBIAN_FRONTEND='noninteractive'
35-
36-
# ---------------------------------------------------------------------
37-
# build
38-
# ---------------------------------------------------------------------
39-
40-
FROM os AS build
41-
42-
# limit cpus so don't run out of memory on local machine
43-
# symptom: get error - "c++: fatal error: Killed signal terminated program cc1plus"
44-
# can turn off if building in cloud
45-
ARG CONAN_CPU_COUNT=2
46-
47-
ARG WITH_RUBY='True'
48-
49-
# set some variables
50-
ENV PATH="$HOME/venv3.9/bin:$PATH"
51-
ENV CONAN_PROFILE='conan/profiles/docker'
52-
53-
# update os and add dependencies
54-
# note: Dockerfiles run as root by default, so don't need sudo
55-
RUN apt-get update \
56-
&& apt-get install -y \
57-
autoconf \
58-
automake \
59-
build-essential \
60-
cmake \
61-
git \
62-
python3 \
63-
python3-pip \
64-
rake \
65-
ruby \
66-
&& rm -rf /var/lib/apt/lists/* \
67-
&& pip install conan -v 'conan==2.0.9'
68-
69-
# make an agent directory and cd into it
70-
WORKDIR /root/agent
71-
72-
# bring in the repo contents, minus .dockerignore files
73-
COPY . .
74-
75-
ARG WITH_TESTS=false
76-
ARG WITH_TESTS_ARG=argument
77-
ARG SHARED=False
78-
79-
# Build and optionally test
80-
RUN if [ -z "$WITH_TESTS" ] || [ "$WITH_TESTS" = "false" ]; then \
81-
WITH_TESTS_ARG="--test-folder="; \
82-
else \
83-
WITH_TESTS_ARG=""; \
84-
fi \
85-
&& conan profile detect \
86-
&& conan create . \
87-
--build=missing \
88-
-c "tools.build:jobs=$CONAN_CPU_COUNT" \
89-
-o agent_prefix=mtc \
90-
-o cpack=True \
91-
-o "with_ruby=$WITH_RUBY" \
92-
-o cpack_destination=/root/agent \
93-
-o cpack_name=dist \
94-
-o cpack_generator=TGZ \
95-
-pr "$CONAN_PROFILE" \
96-
${WITH_TESTS_ARG}
97-
98-
# ---------------------------------------------------------------------
99-
# release
100-
# ---------------------------------------------------------------------
101-
102-
FROM os AS release
28+
FROM mtconnect/agent as mtconnect
10329

10430
# TODO: How about shortening the description to MTConnect Agent or at least MTConnect C++ Agent?
105-
LABEL author='mtconnect' description='MTConnect C++ Agent'
106-
107-
ARG BIN_DIR='/usr/local/bin'
108-
ARG LIB_DIR='/usr/local/lib'
109-
110-
ARG MTCONNECT_CONF_DIR='/etc/mtconnect'
111-
ARG MTCONNECT_DATA_DIR='/usr/local/share/mtconnect'
112-
ARG MTCONNECT_LOG_DIR='/var/log/mtconnect'
31+
LABEL author='mtconnect' description='MTConnect C++ Agent Demo'
11332

114-
ENV MTCONNECT_CONF_DIR="$MTCONNECT_CONF_DIR"
115-
ENV MTCONNECT_DATA_DIR="$MTCONNECT_DATA_DIR"
116-
ENV MTCONNECT_LOG_DIR="$MTCONNECT_LOG_DIR"
117-
ENV DEMO_DIR="$MTCONNECT_DATA_DIR/demo"
118-
119-
# install ruby for simulator
120-
RUN apt-get update \
121-
&& apt-get install -y ruby
33+
USER root
12234

123-
# change to a new non-root user for better security.
124-
# this also adds the user to a group with the same name.
125-
# -m creates a home folder, ie /home/<username>
126-
ARG UID=1000
127-
ARG GID=1000
35+
RUN apk add ruby
12836

129-
RUN groupadd \
130-
--gid $GID \
131-
agent \
132-
&& useradd \
133-
--create-home \
134-
--uid $UID \
135-
--gid $GID \
136-
agent
37+
COPY --chown=agent:agent \
38+
./simulator/* \
39+
/mtconnect/data/simulator/
40+
COPY --chown=agent:agent \
41+
./demo/ \
42+
/mtconnect/data/demo/
43+
COPY --chown=agent:agent \
44+
./demo/agent/agent.dock \
45+
./demo/agent/Devices.xml \
46+
/mtconnect/config/
13747

13848
USER agent
13949
WORKDIR /home/agent
14050

141-
# install agent executable
142-
COPY --chown=agent:agent --from=build /root/agent/dist.tar.gz /home/agent/
143-
144-
# Extract the data
145-
RUN tar xf dist.tar.gz \
146-
&& rm dist.tar.gz
147-
148-
# Copy the agent binary and create folders used by the agent
149-
USER root
150-
RUN mkdir -p "$BIN_DIR" "$LIB_DIR" \
151-
&& cp /home/agent/dist/bin/* "$BIN_DIR" \
152-
&& cp /home/agent/dist/lib/* "$LIB_DIR" \
153-
&& mkdir -p "$MTCONNECT_CONF_DIR" \
154-
"$MTCONNECT_DATA_DIR" \
155-
"$MTCONNECT_LOG_DIR" \
156-
&& chown agent:agent "$MTCONNECT_CONF_DIR" \
157-
"$MTCONNECT_DATA_DIR" \
158-
"$MTCONNECT_LOG_DIR"
159-
160-
USER agent
161-
162-
# Copy the agent data
163-
RUN cp -r /home/agent/dist/share/mtconnect/schemas \
164-
/home/agent/dist/share/mtconnect/simulator \
165-
/home/agent/dist/share/mtconnect/styles \
166-
/home/agent/dist/share/mtconnect/demo \
167-
"$MTCONNECT_DATA_DIR" \
168-
&& cp /home/agent/dist/share/mtconnect/demo/agent/agent.dock "$MTCONNECT_CONF_DIR/agent.cfg" \
169-
&& cp /home/agent/dist/share/mtconnect/demo/agent/Devices.xml "$MTCONNECT_CONF_DIR" \
170-
&& rm -r /home/agent/dist
171-
17251
# expose port
17352
EXPOSE 5000
17453

54+
# Allow the directories to be mounted
55+
VOLUME ["/mtconnect/config", "/mtconnect/log", "/mtconnect/data"]
56+
17557
# default command - can override with docker run or docker-compose command.
17658
# this runs the adapter simulator and the agent using the sample config file.
17759
# note: must use shell form here instead of exec form, since we're running
17860
# multiple statements using shell commands (& and &&).
17961
# see https://stackoverflow.com/questions/46797348/docker-cmd-exec-form-for-multiple-command-execution
180-
CMD /usr/bin/ruby "$MTCONNECT_DATA_DIR/simulator/run_scenario.rb" -p 7879 -l "$DEMO_DIR/agent/mazak.txt" \
181-
& /usr/bin/ruby "$MTCONNECT_DATA_DIR/simulator/run_scenario.rb" -p 7878 -l "$DEMO_DIR/agent/okuma.txt" \
182-
& mtcagent run
62+
63+
CMD /usr/bin/ruby /mtconnect/data/simulator/run_scenario.rb -p 7879 -l /mtconnect/data/demo/agent/mazak.txt \
64+
& /usr/bin/ruby /mtconnect/data/simulator/run_scenario.rb -p 7878 -l /mtconnect/data/demo/agent/okuma.txt \
65+
& mtcagent run /mtconnect/config/agent.dock
18366

18467
# ---------------------------------------------------------------------
18568
# note
18669
# ---------------------------------------------------------------------
18770

18871
# after setup, the dirs look like this -
18972
#
190-
# /usr/local/bin
73+
# /usr/bin
19174
# |-- agent - the cppagent application
19275
#
193-
# /etc/mtconnect - Configuration files agent.cfg and Devices.xml
76+
# /mtconnect/config - Configuration files agent.cfg and Devices.xml
19477
#
195-
# /usr/local/share/mtconnect
78+
# /mtconnect/data
19679
# |-- schemas - xsd files
197-
# |-- simulator - agent.cfg, simulator.rb, vmc-3axis.xml, log.txt
19880
# |-- styles - styles.xsl, styles.css, favicon.ico, etc
19981
#
20082
# /home/agent - the user's directory
20183
#
202-
# /var/log/mtconnect - logging directory
84+
# /mtconnect/log - logging directory

0 commit comments

Comments
 (0)