From 3edd9bd1ca33b0635a79cb080d10dca6d28a5327 Mon Sep 17 00:00:00 2001 From: Cory Sayles Date: Mon, 5 Aug 2024 03:54:32 -0400 Subject: [PATCH] Docker build - Just for fun (#203) * Adding files to run in docker * Updating build and run scripts with usage * Updating build and run scripts to check for docker * Fixing commented code * Added docker compose file and added instructions --- .dockerignore | 4 ++++ .env | 2 ++ Dockerfile.spud | 27 +++++++++++++++++++++++++++ docker-compose.yaml | 24 ++++++++++++++++++++++++ potato-build.sh | 34 ++++++++++++++++++++++++++++++++++ potato-run.sh | 40 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 131 insertions(+) create mode 100644 .dockerignore create mode 100644 .env create mode 100644 Dockerfile.spud create mode 100644 docker-compose.yaml create mode 100755 potato-build.sh create mode 100755 potato-run.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..856bbada --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +# .dockerignore +.git +README.md +LICENSE \ No newline at end of file diff --git a/.env b/.env new file mode 100644 index 00000000..7dc4dda0 --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +# Change this to --vegan if you want a vegan potato +VEGAN="--nonvegan" diff --git a/Dockerfile.spud b/Dockerfile.spud new file mode 100644 index 00000000..06e18366 --- /dev/null +++ b/Dockerfile.spud @@ -0,0 +1,27 @@ +########### Phase 1 - Compile ########### +FROM maven:3.9.8-eclipse-temurin-8-alpine AS potato-builder + +# Set the working directory and copy src +WORKDIR /usr/src/Potato +COPY . /usr/src/Potato/ + +# Build with Maven +RUN mvn clean install; + +########### Phase 2 - Package ########### +FROM eclipse-temurin:21 + +# Can be overridden with `docker run -e VEGAN="--vegan"` +ENV VEGAN="" + +# Make am appropriate user name +RUN useradd -u 500 mrpotatohead +USER mrpotatohead + +WORKDIR /home/mrpotatohead + +# Cook this potato right every time +COPY --from=potato-builder /usr/src/Potato/target /home/mrpotatohead/target + +# Allow user to pass in an argument when running the container +CMD ["sh","-c","java -jar target/Potato.jar ${VEGAN}"] diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 00000000..9fdcf7a5 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,24 @@ +# HOW TO BUILD & RUN THE POTATO IMAGE +# 1. Run the potato-build.sh script and pass in an optional tag for the container +# e.g. ./potato-build.sh 1.0.0 +# 1a. Run the potato-run.sh script and pass in an optional prepare type argument +# e.g. ./potato-run.sh "--vegan" +# 2. You can run using docker compose run +# e.g. docker compose run --build potato (Builds image before running) +# e.g. docker compose run potato (if you already built it) +# e.g. docker compose run potato --rm (removes the container upon exit) +# 2a. To pass in an argument, either edit the .env file, +# or set a local environment variable: e.g. export VEGAN="--vegan" +# 3. You can run using docker compose up. +# e.g. docker compose up +# 3a. Make sure to clean up containers since compose up doesn't have a --rm option +# e.g. docker container rm potato-potato-1 + +services: + potato: + build: + context: . + dockerfile: ./Dockerfile.spud + environment: + - VEGAN=${VEGAN} + diff --git a/potato-build.sh b/potato-build.sh new file mode 100755 index 00000000..099c4e73 --- /dev/null +++ b/potato-build.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +# HOW TO BUILD THE POTATO IMAGE +# 1. Run this script and pass in an optional tag for the container +# e.g. ./potato-build.sh 1.0.0 +# 2. Alternatively, you can can run using docker compose +# e.g. docker compose run --build potato +# 2a. To pass in an argument, either edit the .env file, +# or set a local environment variable: e.g. export VEGAN="--vegan" + +# Tag for the built container +TAG=latest + +if ! [ -z $1 ]; then + if [[ $1 == "-h" || $1 == "?" ]]; then + # Print usage + echo "Usage: $0 " + echo "Example: $0 1.0.0" + exit 0 + else + TAG=$1 + fi +fi + +# Check if Docker is installed +if ! [ -x "$(command -v docker)" ]; then + echo "You need to install Docker." + exit 1 +else + echo "Using tag: '$TAG'" +fi + +# Perform the build +docker build -t potato:$TAG -f Dockerfile.spud . diff --git a/potato-run.sh b/potato-run.sh new file mode 100755 index 00000000..78d50c70 --- /dev/null +++ b/potato-run.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +# HOW TO RUN THE POTATO IMAGE +# 1. Run this script and pass in an optional prepare type argument +# e.g. ./potato-run.sh "--vegan" +# 2. You can run using docker compose run +# e.g. docker compose run --build potato (builds the container before running) +# e.g. docker compose run potato (if you already built it) +# e.g. docker compose run potato --rm (removes the container upon exit) +# 2a. To pass in an argument, either edit the .env file, +# or set a local environment variable: e.g. export VEGAN="--vegan" +# 3. You can run using docker compose up and passing in the --build arg +# e.g. docker compose up --build +# 3a. Make sure to clean up containers since compose up doesn't have a --rm option +# e.g. docker container rm potato-potato-1 + + +# Preparation type for the built potato container +PREPARE_TYPE="--nonvegan" + +if ! [ -z $1 ]; then + if [[ $1 == "-h" || $1 == "?" ]]; then + # Print usage + echo "Usage: $0 " + echo "Example: $0 --vegan" + exit 0 + else + PREPARE_TYPE=$1 + fi +fi +# Check if Docker is installed +if ! [ -x "$(command -v docker)" ]; then + echo "You need to install Docker." + exit 1 +else + echo "Running Potato with argument: \"$PREPARE_TYPE\"" +fi + +# Perform the build +docker run -it --name potato-in-a-container --rm -e VEGAN=${PREPARE_TYPE} potato:latest