Skip to content

Commit 3c23f83

Browse files
committed
Update Containerfiles and compose stack, and switch from MySQL to MariaDB
The Debian-variant of MySQL image no longer seems to be an actively supported platform as the proprietary Oracle Linux-variant is: docker-library/mysql#867
1 parent 7aa3d90 commit 3c23f83

File tree

4 files changed

+216
-83
lines changed

4 files changed

+216
-83
lines changed

Taskfile

Lines changed: 124 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,18 +176,22 @@
176176
# Helper Functions #
177177
####################
178178

179+
# The global list of containers defined in the compose stack
180+
#
181+
# TODO: Parse container-compose.yml and read in valid service containers
182+
# instead of hardcoding them here
183+
#
184+
containers=('app' 'database')
185+
179186
function validateContainer {
180187
local args=("$@")
181188

182-
local containers=('app' 'database')
183189
local targetContainer="${args[0]}"
184190

185191
# Validate the targetContainer name
186192
if [[ " ${containers[*]} " =~ [[:space:]]${targetContainer}[[:space:]] ]]; then
187193
return 0
188194
else
189-
error "Error! Invalid container: $targetContainer"
190-
error "Valid containers: ${containers[*]}"
191195
return 1
192196
fi
193197
}
@@ -264,7 +268,7 @@
264268

265269
function build {
266270
local args=("$@")
267-
local targetContainer="${args[0]}"
271+
local targetContainer="${args[0]}" # TODO: Support multiple containers
268272

269273
# Check if a targetContainer is supplied;
270274
# otherwise, all containers will be built
@@ -273,6 +277,9 @@
273277
validateContainer "$targetContainer"
274278
# Check if a validation error occurred
275279
if [[ "$?" -ne 0 ]]; then
280+
error "Error! Invalid container: $targetContainer"
281+
error "Valid containers: ${containers[*]}"
282+
276283
return "$?"
277284
fi
278285
# "Shift out" the targetContainer arg from the list of args
@@ -294,11 +301,89 @@
294301
#########
295302
# start #
296303
#########
297-
304+
305+
# Either `compose start`s a set of existing containers given their stack service names as args,
306+
# or `compose up`s the entire stack. Use -- to seperate between list of containers and command args.
307+
#
308+
# Note: If `compose up` is determined for use, all args will be passed to that command instaed
309+
#
310+
# Note: `compose start` only starts existing stopped containers,
311+
# while `compose up` creates them from the stack
312+
#
298313
function start {
299-
podman-compose up
300-
return "$?"
314+
local args=("$@")
315+
316+
# Short circut no args to compose-up
317+
if [[ -z "${args[@]}" ]]; then
318+
podman-compose up "${args[@]}"
319+
return "$?"
320+
fi
321+
322+
# Seperate passed container args from passed command args, whether applicable or not.
323+
#
324+
# For this function, only the compose-start command requires such a distinction,
325+
# but the function determines whether to compose-start or compose-up depending on
326+
# the targetContainers supplied anyways.
327+
#
328+
# While doing all the work upfront maybe wasteful, it's more readable at the moment
329+
330+
local separatorHasBeenFound=false
331+
332+
local argsList1=() # targetContainer args
333+
local argsList2=() # args for the command
334+
335+
for currentArg in "${args[@]}"; do
336+
[[ "$currentArg" == '--' ]]
337+
local argIsSeperator="$?"
338+
339+
if [[ "$separatorHasBeenFound" = false && "$argIsSeperator" -eq 0 ]]; then
340+
separatorHasBeenFound=true
341+
elif [[ "$separatorHasBeenFound" = false ]]; then
342+
# No seperator yet; arg belongs in the first bucket
343+
argsList1+=("$currentArg")
344+
else
345+
# Seperator found; arg belongs in the second bucket
346+
argsList2+=("$currentArg")
347+
fi
348+
done
349+
350+
# In an attempt to compose-start, look for at least one valid container from the targetContainers
351+
# otherwise, the container stack will be upped, and both argsLists will be passed to that command
352+
353+
# Assume the first set of args are targetContainers
354+
local targetContainers="$argsList1"
355+
local validTargetContainers=()
356+
357+
for targetContainer in "$targetContainers"; do
358+
359+
validateContainer "$targetContainer"
360+
# If validation was successful, add the container to the set of validTargetContainers for processing;
361+
if [[ "$?" -eq 0 ]]; then
362+
validTargetContainers+="$targetContainer"
363+
else # otherwise, indicate its invalidity and omit it from the list of validTargetContainers
364+
error "Invalid container: $targetContainer"
365+
fi
366+
done
367+
368+
if (( ${#validTargetContainers[@]} != 0 )); then
369+
370+
# Valid targetContainers found;
371+
# gather args and compose-start
372+
local commandArgs="$argsList2"
373+
podman-compose start "${validTargetContainers[@]}" "${commandArgs[@]}"
374+
return "$?"
375+
376+
else
377+
378+
# No valid targetContainers to start supplied;
379+
# compose-up instead, using all supplied args
380+
argsList1+=("${argsList2[@]}")
381+
podman-compose up "${args[@]}"
382+
return "$?"
383+
384+
fi
301385
}
386+
alias up='start'
302387
alias default='start'
303388
alias run='default' # TODO: Make a TASKFILE_RUN_RUNS_DEFAULT=1 flag that automatically runs the
304389
# "default" task if no args are supplied to run. That way, we optionally
@@ -334,8 +419,34 @@
334419
########
335420

336421
function down {
337-
podman-compose down
338-
return "$?"
422+
local args=("$@")
423+
local targetContainer="${args[0]}" # TODO: Support multiple containers
424+
425+
# Check if a targetContainer is supplied;
426+
# otherwise, all containers will be downed
427+
if [[ -n "$targetContainer" ]] then
428+
429+
validateContainer "$targetContainer"
430+
# Check if a validation error occurred
431+
if [[ "$?" -ne 0 ]]; then
432+
error "Error! Invalid container: $targetContainer"
433+
error "Valid containers: ${containers[*]}"
434+
435+
return "$?"
436+
fi
437+
# "Shift out" the targetContainer arg from the list of args
438+
args=("${args[@]:1}")
439+
440+
podman-compose down "$targetContainer" "${args[@]}"
441+
return "$?"
442+
443+
else
444+
445+
# Down the stack
446+
podman-compose down "${args[@]}"
447+
return "$?"
448+
449+
fi
339450
}
340451
alias clean='down'
341452

@@ -350,6 +461,9 @@
350461
validateContainer "$targetContainer"
351462
# Check if a validation error occurred
352463
if [[ "$?" -ne 0 ]]; then
464+
error "Error! Invalid container: $targetContainer"
465+
error "Valid containers: ${containers[*]}"
466+
353467
return "$?"
354468
fi
355469
# "Shift out" the targetContainer arg from the list of args
@@ -367,7 +481,7 @@
367481
function restart {
368482
stop
369483
down
370-
start
484+
start #TODO: Use alias 'up' when it works
371485
return "$?"
372486
}
373487

app/Containerfile

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
ARG image_variant
2+
3+
ARG python_version=3.12
4+
ARG python_image=docker.io/python:${python_version}
5+
6+
ARG base_image=${python_image}
7+
8+
FROM ${base_image} AS base-image
9+
10+
ENV BUILD_ENVIRONMENT=${image_variant}
11+
12+
FROM base-image AS base-shared
13+
14+
15+
16+
FROM base-shared AS os-dependencies-shared
17+
18+
# Install micropipenv
19+
RUN pip install 'micropipenv>=1.8.0,<1.9.0'
20+
21+
FROM os-dependencies-shared AS production-os-dependencies
22+
23+
24+
25+
FROM os-dependencies-shared AS os-dependencies-development
26+
27+
28+
29+
FROM os-dependencies-${image_variant} AS project-dependencies-shared
30+
31+
WORKDIR /usr/src/app/
32+
33+
# COPY in the Pipenv requirements independently of the app source
34+
COPY ./src/Pipfile.lock ./
35+
36+
FROM project-dependencies-shared AS project-dependencies-production
37+
38+
# Install prod app dependencies
39+
RUN micropipenv install --method pipenv
40+
41+
FROM project-dependencies-shared AS project-dependencies-development
42+
43+
# Install app dependencies, including dev ones
44+
RUN micropipenv install --dev --method pipenv
45+
46+
COPY ./src/ ./
47+
48+
FROM project-dependencies-${image_variant} AS post-project-dependencies-shared
49+
50+
51+
52+
FROM post-project-dependencies-shared AS post-project-dependencies-production
53+
54+
55+
56+
FROM post-project-dependencies-shared AS post-project-dependencies-development
57+
58+
59+
60+
FROM post-project-dependencies-${image_variant} AS entrypoint-shared
61+
62+
WORKDIR /usr/src/app/
63+
ENTRYPOINT python
64+
CMD run.py
65+
66+
FROM entrypoint-shared AS final-image
67+
FROM final-image AS moviemanager-app-${image_variant}

app/container/containerfile

Lines changed: 0 additions & 58 deletions
This file was deleted.

compose.yml

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ version: '3'
22
services:
33
app:
44
container_name: app
5-
restart: "no"
6-
working_dir: /var/www/html/
5+
#
6+
#image:
7+
# or
78
build:
89
context: ./app/
9-
dockerfile: ./containerfile
10-
args:
11-
image_variant: development
10+
dockerfile: Containerfile
11+
args:
12+
image_variant: development
13+
#
14+
restart: "no"
1215
environment:
1316
APPLICATION_ENVIRONMENT: DEVELOPMENT
1417
APPLICATION_HOST: '0.0.0.0'
@@ -23,25 +26,32 @@ services:
2326

2427
database:
2528
container_name: database
29+
#
30+
#image:
31+
# or
32+
image: mariadb:lts
33+
#
2634
restart: "no"
27-
build:
28-
context: ./database/
29-
dockerfile: ./containerfile
30-
args:
31-
image_variant: development
3235
environment:
36+
MYSQL_ROOT_PASSWORD: root1234
3337
MYSQL_USER: movie_app
3438
MYSQL_PASSWORD: movie_app1234
35-
MYSQL_ROOT_PASSWORD: root1234
3639
MYSQL_DATABASE: movie_manager
3740
volumes:
3841
# Database conf files
3942
- ./conf.d/mysql.cnf:/etc/mysql/conf.d/mysql.cnf:Z
43+
4044
# Sql files to initialize the server with
45+
#
46+
# Note: Only scripts in that top-level directory
47+
# are executed, in alphabetical order
48+
#
4149
- ./initdb/:/docker-entrypoint-initdb.d/:Z
42-
# Additional sql files
43-
- ./sql/:/usr/src/sql/:Z
44-
# Persist database data to host
45-
- ./dbdata/:/var/lib/mysql/:Z
50+
51+
# Volume-out database data to host as a persistent, named volume
52+
- dbdata:/var/lib/mysql/:Z
4653
ports:
4754
- 3306:3306
55+
56+
volumes:
57+
dbdata:

0 commit comments

Comments
 (0)