Skip to content

Commit 921ec03

Browse files
authored
Merge pull request #26 from udx/UAT-90
Implementation and Optimize Docker Configuration [UAT-90]
2 parents 3ebec1a + 8e74eb3 commit 921ec03

File tree

11 files changed

+327
-197
lines changed

11 files changed

+327
-197
lines changed

Dockerfile

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,68 @@
1-
# Use the udx-worker as the base image
2-
FROM usabilitydynamics/udx-worker:0.12.0
1+
# Use the latest udx-worker as the base image
2+
FROM usabilitydynamics/udx-worker:0.16.0
33

44
# Add metadata labels
5-
LABEL maintainer="UDX"
6-
LABEL version="0.9.0"
5+
LABEL version="0.11.0"
76

8-
# Set build arguments for Node.js version, application port, and log directory
9-
ARG NODE_VERSION=22.13.1
10-
ARG LOG_DIR=/var/log/udx-worker-nodejs
7+
# Set build arguments for Node.js version and application port
8+
ARG NODE_VERSION=22.14.0
119
ARG APP_PORT=8080
1210

13-
# Set environment variables
14-
ENV HOME="/usr/src/app"
15-
ENV LOG_DIR="${LOG_DIR}"
16-
ENV APP_PORT="${APP_PORT}"
11+
# Set application-specific environment variables
12+
ENV APP_HOME="/usr/src/app" \
13+
APP_PORT="${APP_PORT}"
1714

18-
# Use root user for package installations and file permissions setup
15+
# Use root user for Node.js installation
1916
USER root
2017

21-
# Set the shell with pipefail option
18+
# Set shell with pipefail option for safer pipe operations
2219
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
2320

21+
# Set working directory for Node.js installation
22+
WORKDIR /tmp
23+
2424
# Install Node.js
25-
ARG BUILDPLATFORM
2625
RUN set -ex && \
27-
# Parse platform architecture
28-
case "${BUILDPLATFORM}" in \
29-
"linux/amd64") ARCH="x64" ;; \
30-
"linux/arm64") ARCH="arm64" ;; \
31-
*) echo "Unsupported platform: ${BUILDPLATFORM}" && exit 1 ;; \
32-
esac && \
33-
# Download and install Node.js for the build platform
34-
curl -fsSL "https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-${ARCH}.tar.xz" -o node.tar.xz && \
35-
tar -xJf node.tar.xz && \
36-
mv "node-v${NODE_VERSION}-linux-${ARCH}" /usr/local/node && \
26+
# Detect architecture
27+
ARCH=$(dpkg --print-architecture 2>/dev/null || echo "x64") && \
28+
if [ "$ARCH" = "amd64" ]; then ARCH="x64"; fi && \
29+
if [ "$ARCH" = "arm64" ]; then ARCH="arm64"; fi && \
30+
# Download Node.js binary and checksum
31+
curl -fsSLO "https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-${ARCH}.tar.xz" && \
32+
curl -fsSLO "https://nodejs.org/dist/v${NODE_VERSION}/SHASUMS256.txt" && \
33+
# Verify checksum
34+
grep " node-v${NODE_VERSION}-linux-${ARCH}.tar.xz\$" SHASUMS256.txt | sha256sum -c - && \
35+
# Extract and install
36+
mkdir -p /usr/local/node && \
37+
tar -xJf "node-v${NODE_VERSION}-linux-${ARCH}.tar.xz" --strip-components=1 -C /usr/local/node && \
38+
# Create symlinks
3739
ln -sf /usr/local/node/bin/node /usr/local/bin/node && \
3840
ln -sf /usr/local/node/bin/npm /usr/local/bin/npm && \
3941
ln -sf /usr/local/node/bin/npx /usr/local/bin/npx && \
40-
# Clean up
41-
rm node.tar.xz && \
42-
rm -rf /tmp/* /var/tmp/*
42+
# Verify installation
43+
node --version && \
44+
npm --version && \
45+
# Cleanup
46+
rm -rf /tmp/*
4347

4448
# Copy application files
45-
COPY src/index.js "${HOME}/index.js"
46-
COPY src/configs/services.yaml /usr/local/configs/worker/services.yaml
47-
COPY src/tests/ "${HOME}/tests/"
48-
COPY LICENSE "${HOME}/LICENSE"
49+
# Create application directory
50+
RUN mkdir -p "${APP_HOME}" && \
51+
chown -R "${USER}:${USER}" "${APP_HOME}" && \
52+
chmod -R 755 "${APP_HOME}"
4953

50-
# Ensure the log directory exists, then adjust permissions
51-
RUN mkdir -p "${LOG_DIR}" \
52-
&& chown -R "${USER}:${USER}" "${HOME}" "${HOME}/tests" "${LOG_DIR}" \
53-
&& chmod -R 755 "${HOME}" "${LOG_DIR}"
54+
# Copy license and examples
55+
COPY --chown=${USER}:${USER} LICENSE "${APP_HOME}/LICENSE"
5456

5557
# Expose the application port
5658
EXPOSE ${APP_PORT}
5759

58-
# Switch to the non-root user
60+
# Switch to the non-root user and set working directory
5961
USER "${USER}"
62+
WORKDIR "${APP_HOME}"
6063

61-
# Set the working directory
62-
WORKDIR "${HOME}"
64+
# Use the parent image's entrypoint
65+
ENTRYPOINT ["/usr/local/worker/bin/entrypoint.sh"]
6366

64-
# Set the default command
67+
# Use the default command from parent image
6568
CMD ["tail", "-f", "/dev/null"]

Makefile

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ include Makefile.help
66
.DEFAULT_GOAL := help
77

88
# Phony targets ensure Make doesn't get confused by filenames
9-
.PHONY: build run deploy run-it clean exec log test dev-pipeline run-test run-all-tests wait-container-ready
9+
.PHONY: build run deploy run-it clean exec log test dev-pipeline run-all-tests wait-container-ready
1010

1111
# Docker Commands for Reusability
1212
DOCKER_RUN_BASE := docker run --rm --name $(CONTAINER_NAME) \
1313
-e NODE_ENV=$(NODE_ENV) \
14-
-e LOG_DIR=$(LOG_DIR) \
1514
-p $(HOST_PORT):$(CONTAINER_PORT)
1615

1716
# Determine if we should run in detached mode or interactively
@@ -77,30 +76,19 @@ wait-container-ready:
7776
done
7877
@echo "Container is ready."
7978

80-
# Run a specific test script (specified by TEST_SCRIPT)
81-
run-test: clean run
82-
@echo "Running test script $(TEST_SCRIPT) in test environment..."
83-
@NODE_ENV=test
84-
@$(MAKE) --no-print-directory wait-container-ready
85-
@if [ -z "$(TEST_SCRIPT)" ]; then \
86-
echo "Error: TEST_SCRIPT variable is not set. Specify a script to run, e.g., make run-test TEST_SCRIPT=example.sh"; \
87-
exit 1; \
88-
fi
89-
@$(DOCKER_EXEC) sh $(CONTAINER_SRC_PATH)/tests/$(TEST_SCRIPT) || echo "Test $(TEST_SCRIPT) failed"
90-
@$(DOCKER_CLEAN)
91-
92-
# Run all tests in the tests directory with test environment
93-
run-all-tests: clean run
94-
@echo "Starting Docker container for test execution in test environment..."
95-
@NODE_ENV=test
96-
@$(MAKE) --no-print-directory wait-container-ready
97-
@echo "Executing all test scripts..."
98-
@for test_script in $(SRC_PATH)/tests/*.sh; do \
99-
echo "Running $$(basename $$test_script)..."; \
100-
$(DOCKER_EXEC) sh $(CONTAINER_SRC_PATH)/tests/$$(basename $$test_script) || echo "Test $$(basename $$test_script) failed"; \
101-
done
102-
@$(MAKE) clean
103-
@echo "All tests completed."
79+
# Run all tests in the tests directory
80+
run-all-tests:
81+
@echo "Running all tests in Docker container..."
82+
@docker run --rm \
83+
-v $(PWD)/src/tests:/usr/src/app/tests \
84+
-v $(PWD)/src/examples/simple-server/index.js:/usr/src/app/index.js \
85+
-v $(PWD)/src/examples/simple-server/.config/worker/services.yaml:/home/udx/.config/worker/services.yaml \
86+
-e NODE_ENV=test \
87+
$(DOCKER_IMAGE) \
88+
/bin/sh -c 'cd /usr/src/app/tests && for test_script in *.sh; do \
89+
echo "Running $$test_script..."; \
90+
sh ./$$test_script || { echo "Test $$test_script failed"; exit 1; }; \
91+
done'
10492

10593
# Run validation tests (build and run-all-tests)
10694
test: build run-all-tests

Makefile.variables

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ NODE_ENV ?= development
1111
# Paths Configuration
1212
SRC_PATH ?= src
1313
CONTAINER_SRC_PATH ?= /usr/src/app
14-
HOST_LOG_DIR ?= ./logs
15-
LOG_DIR ?= /var/log/udx-worker-nodejs
1614

1715
# Networking Configuration
1816
HOST_PORT ?= 8080

0 commit comments

Comments
 (0)