Skip to content

Commit 933b22d

Browse files
author
Gonzalo Diaz
committed
[CONFIG] [Docker] building process re-organized.
1 parent 15aeac7 commit 933b22d

File tree

3 files changed

+74
-19
lines changed

3 files changed

+74
-19
lines changed

Dockerfile

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ FROM python:3.12.4-alpine3.20 AS base
44
ENV WORKDIR=/app
55
WORKDIR ${WORKDIR}
66

7+
RUN apk add --update --no-cache make
8+
79
###############################################################################
810
FROM base AS lint
911

@@ -35,34 +37,51 @@ COPY ./requirements.txt ${WORKDIR}/
3537
COPY ./setup.cfg ${WORKDIR}/
3638
COPY ./Makefile ${WORKDIR}/
3739

40+
# code linting conf
41+
COPY ./.pylintrc ${WORKDIR}/
42+
COPY ./.coveragerc ${WORKDIR}/
43+
COPY ./setup.cfg ${WORKDIR}/
44+
3845
# markdownlint conf
3946
COPY ./.markdownlint.yaml ${WORKDIR}/
4047

4148
# yamllint conf
4249
COPY ./.yamllint ${WORKDIR}/
4350
COPY ./.yamlignore ${WORKDIR}/
4451

45-
# pylint and covergae
46-
COPY ./.pylintrc ${WORKDIR}/
47-
COPY ./.coveragerc ${WORKDIR}/
48-
4952
CMD ["make", "lint"]
5053

5154
###############################################################################
5255
FROM base AS development
5356

54-
RUN apk add --update --no-cache make
57+
COPY ./Makefile ${WORKDIR}/
58+
COPY ./requirements.txt ${WORKDIR}/
59+
COPY ./setup.cfg ${WORKDIR}/
5560

56-
###############################################################################
57-
FROM development AS builder
61+
RUN make dependencies
5862

5963
COPY ./src ${WORKDIR}/src
60-
COPY ./requirements.txt ${WORKDIR}/
61-
COPY ./Makefile ${WORKDIR}/
62-
COPY ./setup.cfg ${WORKDIR}/
64+
6365
RUN ls -alh
6466

65-
RUN pip install -r requirements.txt
67+
# CMD []
68+
69+
###############################################################################
70+
FROM development AS builder
71+
72+
ENV WORKDIR=/app
73+
WORKDIR ${WORKDIR}
74+
75+
RUN apk add --update --no-cache rsync
76+
77+
RUN rsync -av --prune-empty-dirs \
78+
--exclude '*_test.py' \
79+
--exclude '*.pyc' \
80+
--exclude '.venv' \
81+
--exclude '__pycache__' \
82+
src/ build/
83+
84+
# CMD []
6685

6786
###############################################################################
6887
### In testing stage, can't use USER, due permissions issue
@@ -78,27 +97,36 @@ ENV BRUTEFORCE=false
7897
WORKDIR /app
7998

8099
COPY ./.coveragerc ${WORKDIR}/
100+
COPY ./setup.cfg ${WORKDIR}/
101+
81102
RUN ls -alh
82103

83-
CMD ["make", "test", "-e", "{DEBUG}"]
104+
CMD ["make", "test"]
84105

85106
###############################################################################
86107
### In production stage
87108
## in the production phase, "good practices" such as
88109
## WORKDIR and USER are maintained
89110
##
90-
FROM builder AS production
111+
FROM python:3.12.4-alpine3.20 AS production
91112

92113
ENV LOG_LEVEL=INFO
93114
ENV BRUTEFORCE=false
115+
ENV WORKDIR=/app
116+
WORKDIR ${WORKDIR}
94117

95118
RUN adduser -D worker
96119
RUN mkdir -p /app
97120
RUN chown worker:worker /app
98121

99-
WORKDIR /app
122+
RUN apk add --update --no-cache make
123+
COPY ./Makefile ${WORKDIR}/
124+
125+
COPY --from=builder /app/build/ ${WORKDIR}/
100126

101127
RUN ls -alh
102128

103129
USER worker
104-
CMD ["make", "test", "-e", "{DEBUG}"]
130+
CMD ["make", "run"]
131+
132+
# checkov:skip= CKV_DOCKER_2: production image isn't a service process (yet)

Makefile

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,15 @@ test/styling: dependencies
8282
${RUNTIME_TOOL} -m pycodestyle --statistics src/
8383

8484
format:
85-
autopep8 --in-place --recursive --aggressive --aggressive --verbose src/
85+
${RUNTIME_TOOL} -m autopep8 --in-place --recursive --aggressive --aggressive --verbose src/
86+
87+
build: env
88+
rsync -av --prune-empty-dirs \
89+
--exclude '*_test.py' \
90+
--exclude '*.pyc' \
91+
--exclude '.venv' \
92+
--exclude '__pycache__' \
93+
src/ build/
8694

8795
test: env dependencies
8896
${RUNTIME_TOOL} -m coverage run -m \
@@ -113,17 +121,20 @@ clean:
113121
rm -fr .pytest_cache
114122
rm -fr htmlcov
115123
rm -fr coverage
124+
rm -fr build
116125
find . -path "*/*.pyc" -delete -print
117126
find . -path "*/*.pyo" -delete -print
118127
find . -path "*/__pycache__" -type d -print -exec rm -fr {} ';'
119128

120129
compose/build: env
121130
docker-compose --profile lint build
122131
docker-compose --profile testing build
132+
docker-compose --profile production build
123133

124134
compose/rebuild: env
125135
docker-compose --profile lint build --no-cache
126136
docker-compose --profile testing build --no-cache
137+
docker-compose --profile production build --no-cache
127138

128139
compose/lint/markdown: compose/build
129140
docker-compose --profile lint run --rm algorithm-exercises-py-lint make lint/markdown
@@ -139,7 +150,13 @@ compose/test/static: compose/build
139150

140151
compose/lint: compose/lint/markdown compose/lint/yaml compose/test/styling compose/test/static
141152

153+
compose/test: compose/build
154+
docker-compose --profile testing run --rm algorithm-exercises-py-test make test
155+
142156
compose/run: compose/build
143-
docker-compose --profile testing run --rm algorithm-exercises-py make test
157+
docker-compose --profile production run --rm algorithm-exercises-py make run
144158

145159
all: lint coverage
160+
161+
run:
162+
ls -alh

compose.yaml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22

33
services:
4-
algorithm-exercises-py:
5-
image: algorithm-exercises-py:latest
4+
algorithm-exercises-py-test:
5+
image: algorithm-exercises-py:test
66
build:
77
context: .
88
target: testing
@@ -37,6 +37,16 @@ services:
3737
- ./:/app
3838
profiles: ["development"]
3939

40+
algorithm-exercises-py:
41+
image: algorithm-exercises-py:latest
42+
build:
43+
context: .
44+
target: production
45+
environment:
46+
LOG_LEVEL: ${LOG_LEVEL:-INFO} ## (1) ## INFO | DEBUG
47+
BRUTEFORCE: ${BRUTEFORCE:-false} ## (1) ## true | false
48+
profiles: ["production"]
49+
4050
## REFERENCES:
4151
## (1) Passing Environment variable with fallback value:
4252
## https://stackoverflow.com/a/70772707/6366150

0 commit comments

Comments
 (0)