forked from mtconnect/cppagent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile.alpine
More file actions
173 lines (144 loc) · 5.94 KB
/
Dockerfile.alpine
File metadata and controls
173 lines (144 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# MTConnect Public C++ Agent Docker image build instructions
# TODO: Do we need notes like this to be in `Dockerfile`? I think the docs might be a better place for this. IMHO most notes in this file should be moved to the docs.
# ---------------------------------------------------------------------
# notes
# ---------------------------------------------------------------------
#
# to build locally and push to docker hub, run this with something like -
#
# docker buildx build \
# --platform linux/amd64,linux/arm64 \
# --tag ladder99/agent2 \
# --secret id=access_token,src=ACCESS_TOKEN \
# --push \
# .
#
# then should be able to run with something like
#
# # Note: In this case, I would suggest to map port `5000` to `5000`. The user can always change the port according to their needs.
# docker run -it -p5001:5000 --name agent2 --rm ladder99/agent2:latest
#
# and visit http://localhost:5001 to see demo output
# ---------------------------------------------------------------------
# os
# ---------------------------------------------------------------------
# base image - ubuntu has linux/amd64, linux/arm64 etc
# TODO: How about using a specific Alpine version instead of `latest`?
# FROM alpine:latest AS os
FROM alpine:3.18 AS os
# tzinfo hangs without this
# FIXME: Is this really required on Alpine?
# ARG DEBIAN_FRONTEND='noninteractive'
# ---------------------------------------------------------------------
# build
# ---------------------------------------------------------------------
FROM os AS build
# limit cpus so don't run out of memory on local machine
# symptom: get error - "c++: fatal error: Killed signal terminated program cc1plus"
# can turn off if building in cloud
ARG CONAN_CPU_COUNT=10
ARG WITH_RUBY='True'
# set some variables
ENV PATH="$HOME/venv3.9/bin:$PATH"
ENV CONAN_PROFILE='conan/profiles/docker'
# update os and add dependencies
# note: Dockerfiles run as root by default, so don't need sudo
# Note: `PIP_ROOT_USER_ACTION='ignore'` is required to remove `WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv`. The proper way to fix this warning is to create a non-root user before running `pip install`.
RUN apk update \
&& apk add \
autoconf \
automake \
cmake \
g++ \
git \
make \
py3-pip \
python3 \
ruby \
ruby-rake \
&& PIP_ROOT_USER_ACTION='ignore' pip install conan -v 'conan==2.0.9'
# make an agent directory and cd into it
WORKDIR /root/agent
# bring in the repo contents, minus .dockerignore files
COPY . .
# make installer
RUN conan profile detect \
&& conan install . \
--build=missing \
-c "tools.build:jobs=$CONAN_CPU_COUNT" \
-c tools.build:skip_test=True \
-o "with_ruby=$WITH_RUBY" \
-pr "$CONAN_PROFILE" \
&& conan create . \
--build=missing \
-c "tools.build:jobs=$CONAN_CPU_COUNT" \
-c tools.build:skip_test=True \
-o agent_prefix=mtc \
-o cpack=True \
-o "with_ruby=$WITH_RUBY" \
-o zip_destination=/root/agent \
-pr "$CONAN_PROFILE" \
-tf ''
# ---------------------------------------------------------------------
# release
# ---------------------------------------------------------------------
FROM os AS release
# TODO: How about shortening the description to MTConnect Agent or at least MTConnect C++ Agent?
LABEL author='mtconnect' description='Docker image for the latest Production MTConnect C++ Agent'
ARG BIN_DIR='/usr/bin'
# FIXME: `/etc` is not the best place for the app data.
ARG MTCONNECT_DIR='/etc/mtconnect'
ENV MTCONNECT_DIR="$MTCONNECT_DIR"
ENV DEMO_DIR="$MTCONNECT_DIR/demo/agent"
# install ruby for simulator
# FIXME: test this without installing `unzip`
# FIXME: Do we need to run `apk upgrade` after `apk update`?
RUN apk update \
&& apk add ruby unzip
# change to a new non-root user for better security.
# this also adds the user to a group with the same name.
# --create-home creates a home folder, ie /home/<username>
# TODO: We might also want to merge these two RUN commands (the previous and the following one) into one, although it would be a bit less readable.
RUN adduser -D agent
USER agent
WORKDIR /home/agent
# install agent executable
COPY --chown=agent:agent --from=build /root/agent/build/bin/agent /usr/local/bin/
# Extract the data
RUN unzip *.zip
# Copy data to `MTCONNECT_DIR`
USER root
RUN cp /home/agent/agent-*-Linux/bin/* "$BIN_DIR" \
&& mkdir -p "$MTCONNECT_DIR" \
&& chown -R agent:agent "$MTCONNECT_DIR"
USER agent
RUN cp -r /home/agent/agent-*-Linux/schemas \
/home/agent/agent-*-Linux/simulator \
/home/agent/agent-*-Linux/styles \
/home/agent/agent-*-Linux/demo \
"$MTCONNECT_DIR"
# expose port
EXPOSE 5000
# default command - can override with docker run or docker-compose command.
# this runs the adapter simulator and the agent using the sample config file.
# note: must use shell form here instead of exec form, since we're running
# multiple statements using shell commands (& and &&).
# see https://stackoverflow.com/questions/46797348/docker-cmd-exec-form-for-multiple-command-execution
# FIXME: Test if this works using `mtcagent run "$DEMO_DIR/agent.cfg"` instead of running `cd $DEMO_DIR && mtcagent run agent.cfg`.
CMD /usr/bin/ruby "$MTCONNECT_DIR/simulator/run_scenario.rb" -p 7879 -l "$DEMO_DIR/mazak.txt" \
& /usr/bin/ruby "$MTCONNECT_DIR/simulator/run_scenario.rb" -p 7878 -l "$DEMO_DIR/okuma.txt" \
& mtcagent run "$DEMO_DIR/agent.cfg"
# ---------------------------------------------------------------------
# note
# ---------------------------------------------------------------------
# after setup, the dirs look like this -
#
# /usr/local/bin
# |-- agent - the cppagent application
#
# /etc/mtconnect
# |-- schemas - xsd files
# |-- simulator - agent.cfg, simulator.rb, vmc-3axis.xml, log.txt
# |-- styles - styles.xsl, styles.css, favicon.ico, etc
#
# /home/agent - the user's directory