From 08da1f6b1045a82e4e720f3de06176451aefcdf8 Mon Sep 17 00:00:00 2001 From: Rasmus Lauritsen Date: Sat, 27 Jul 2024 22:24:07 +0200 Subject: [PATCH] first commit --- Dockerfile.bash | 14 ++++++++++++++ Dockerfile.python | 11 +++++++++++ Dockerfile.python-alpine | 14 ++++++++++++++ hello_world-alpine.py | 13 +++++++++++++ hello_world.py | 13 +++++++++++++ hello_world.sh | 14 ++++++++++++++ readme.md | 31 +++++++++++++++++++++++++++++++ 7 files changed, 110 insertions(+) create mode 100644 Dockerfile.bash create mode 100644 Dockerfile.python create mode 100644 Dockerfile.python-alpine create mode 100644 hello_world-alpine.py create mode 100644 hello_world.py create mode 100755 hello_world.sh create mode 100644 readme.md diff --git a/Dockerfile.bash b/Dockerfile.bash new file mode 100644 index 0000000..bcba489 --- /dev/null +++ b/Dockerfile.bash @@ -0,0 +1,14 @@ +# Use the official Ubuntu image as the base image +FROM ubuntu:latest + +# Set the working directory +WORKDIR /usr/src/app + +# Copy the hello_world.sh script into the container +COPY hello_world.sh . + +# Make the script executable +RUN chmod +x hello_world.sh + +# Run the script +CMD ["./hello_world.sh"] diff --git a/Dockerfile.python b/Dockerfile.python new file mode 100644 index 0000000..7afc06e --- /dev/null +++ b/Dockerfile.python @@ -0,0 +1,11 @@ +# Use the official Python image as the base image +FROM python:3.9.19 + +# Set the working directory +WORKDIR /usr/src/app + +# Copy the hello_world.py script into the container +COPY hello_world.py . + +# Run the script +CMD ["python3", "hello_world.py"] diff --git a/Dockerfile.python-alpine b/Dockerfile.python-alpine new file mode 100644 index 0000000..d9e06c3 --- /dev/null +++ b/Dockerfile.python-alpine @@ -0,0 +1,14 @@ +# Use the official Python image as the base image +FROM alpine:latest + +# Install python +RUN apk add --no-cache python3 + +# Set the working directory +WORKDIR /usr/src/app + +# Copy the hello_world.py script into the container +COPY hello_world-alpine.py . + +# Run the script +CMD ["python3", "hello_world-alpine.py"] diff --git a/hello_world-alpine.py b/hello_world-alpine.py new file mode 100644 index 0000000..a3f7045 --- /dev/null +++ b/hello_world-alpine.py @@ -0,0 +1,13 @@ +import time + +# Total duration in seconds (10 minutes * 60 seconds) +duration = 10 * 60 +# Interval between messages in seconds +interval = 5 +# End time calculation +end_time = time.time() + duration + +# Loop until the current time is less than the end time +while time.time() < end_time: + print("Hello world - from Python in Alpine!") + time.sleep(interval) diff --git a/hello_world.py b/hello_world.py new file mode 100644 index 0000000..2c2139a --- /dev/null +++ b/hello_world.py @@ -0,0 +1,13 @@ +import time + +# Total duration in seconds (10 minutes * 60 seconds) +duration = 10 * 60 +# Interval between messages in seconds +interval = 5 +# End time calculation +end_time = time.time() + duration + +# Loop until the current time is less than the end time +while time.time() < end_time: + print("Hello world - from Python!") + time.sleep(interval) diff --git a/hello_world.sh b/hello_world.sh new file mode 100755 index 0000000..5ea49d2 --- /dev/null +++ b/hello_world.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +# Total duration in seconds (10 minutes * 60 seconds) +DURATION=$((10 * 60)) +# Interval between messages in seconds +INTERVAL=5 +# End time calculation +END_TIME=$((SECONDS + DURATION)) + +# Loop until the current time is less than the end time +while [ $SECONDS -lt $END_TIME ]; do + echo "Hello world - from bash!" + sleep $INTERVAL +done diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..1db089c --- /dev/null +++ b/readme.md @@ -0,0 +1,31 @@ +# Hello World in Docker + +## Bash +```bash +docker buildx build -t hello-world-bash -f Dockerfile.bash . +docker run --rm --name "helloWorldBash" hello-world-bash +``` + +## Python +```bash +docker buildx build -t hello-world-python -f Dockerfile.python . +docker run --rm -it --name "helloWorldPython" hello-world-python +``` + +## Python in Alpine +```bash +docker buildx build -t hello-world-python-alpine -f Dockerfile.python-alpine . +docker run --rm -it --name "helloWorldPythonAlpine" hello-world-python-alpine +``` + +# Let's see how much space each image takes up +```bash +docker images +``` + +```bash +REPOSITORY TAG IMAGE ID CREATED SIZE +hello-world-bash latest 7fab71e07670 21 seconds ago 101MB +hello-world-python-alpine latest e42435db8b4a 30 minutes ago 53.4MB +hello-world-python latest 9fe43bb44dca 47 minutes ago 1GB +```