Compare commits
9 Commits
7a458790d2
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| fcffbdb8f5 | |||
| e12386decf | |||
| fef5fbc220 | |||
| 4c20c41426 | |||
| 10966f3c1f | |||
| bec5fc997e | |||
| 516e17a8d8 | |||
| 176d22bbe4 | |||
| d96368feb6 |
88
README.md
Normal file
@ -0,0 +1,88 @@
|
||||
## Uvod
|
||||
|
||||
- Docker je platforma za development, delivery i izvršavanje aplikacija
|
||||
- Docker omogućava da se aplikacija izolira u repetativnoj okolini i odvoji od infrastrukture
|
||||
|
||||
[Features of Docker](https://www.geeksforgeeks.org/features-of-docker/):
|
||||
- Open-source platform
|
||||
- An Easy, lightweight, and consistent way of delivery of applications
|
||||
- Fast and efficient development life cycle
|
||||
- Segregation of duties
|
||||
- Service-oriented architecture
|
||||
- Security
|
||||
- Scalability
|
||||
- Reduction in size
|
||||
- Image management
|
||||
- Networking
|
||||
- Volume management
|
||||
|
||||
|
||||
## Struktura
|
||||
|
||||
- Dockerfile
|
||||
- image
|
||||
- container registry
|
||||
- lokalni
|
||||
- dockerhub
|
||||
- AWS ECR
|
||||
- container
|
||||
|
||||
|
||||
## Dockerfile
|
||||
|
||||
- base image
|
||||
- odabir najmanjeg funkcionalnog
|
||||
- debian prije ubuntua
|
||||
- debian-slim prije debiana
|
||||
- alpine
|
||||
- from scratch
|
||||
- env varijable
|
||||
|
||||
|
||||
## Docker image
|
||||
|
||||
- single stage build
|
||||
- multistage build
|
||||
|
||||
|
||||
## Docker container
|
||||
|
||||
run, publish port 3000:
|
||||
```
|
||||
docker run --name api-server-3000 --publish 3000:3000 --env CONTAINER_NAME="Awesome API server on port 3000" -d api-server
|
||||
```
|
||||
|
||||
run, publish port 3001:
|
||||
```
|
||||
docker run --name api-server-3001 --publish 3001:3000 --env CONTAINER_NAME="Awesome API server on port 3001" -d api-server
|
||||
```
|
||||
run, publish port 3002:
|
||||
```
|
||||
docker run --name api-server-3002 --publish 3002:3000 --env CONTAINER_NAME="Awesome API server on port 3002" -d api-server
|
||||
```
|
||||
|
||||
# Media
|
||||
|
||||
## Docker vs VM
|
||||
|
||||

|
||||
|
||||
## Docker elements
|
||||
|
||||

|
||||
|
||||
## Dockerfile layers
|
||||
|
||||

|
||||
|
||||
## Port publishing
|
||||
|
||||

|
||||
|
||||
## Horizontal scaling
|
||||
|
||||

|
||||
|
||||
## Volumes
|
||||
|
||||

|
||||
@ -1,18 +1,32 @@
|
||||
FROM python:3.11-slim-bookworm
|
||||
|
||||
# uid to run application
|
||||
ARG USER=1000
|
||||
# gid to run application
|
||||
ARG USER_GROUP=1000
|
||||
|
||||
# set virtual env path
|
||||
ENV \
|
||||
PATH=/venv/bin:$PATH
|
||||
|
||||
# does nothing, for documentation only
|
||||
EXPOSE 3000
|
||||
|
||||
# create and set main app directory
|
||||
WORKDIR /app
|
||||
|
||||
# copy app files to /app directory
|
||||
COPY ./app .
|
||||
|
||||
# set permissions to log directory and install dependencies
|
||||
RUN \
|
||||
chown -R ${USER}:${USER_GROUP} /app/log && \
|
||||
python -m venv /venv && \
|
||||
pip install -r requirements.txt
|
||||
|
||||
# set user to run application
|
||||
USER ${USER}:${USER_GROUP}
|
||||
|
||||
# start shell script when container starts
|
||||
ENTRYPOINT ["/app/run.sh"]
|
||||
|
||||
|
||||
@ -1,13 +1,14 @@
|
||||
IMAGE_NAME=api-server
|
||||
CONTAINER_NAME=api-server
|
||||
|
||||
|
||||
build: clean
|
||||
@docker build \
|
||||
--progress=plain \
|
||||
--no-cache \
|
||||
--tag $(IMAGE_NAME) \
|
||||
.
|
||||
|
||||
|
||||
run:
|
||||
@docker run \
|
||||
--name $(IMAGE_NAME) \
|
||||
@ -16,6 +17,7 @@ run:
|
||||
--detach \
|
||||
$(CONTAINER_NAME)
|
||||
|
||||
|
||||
run-mount-log:
|
||||
@docker run \
|
||||
--name $(IMAGE_NAME) \
|
||||
|
||||
21
media/docker-elements.svg
Normal file
|
After Width: | Height: | Size: 181 KiB |
21
media/docker-vs-vm.svg
Normal file
|
After Width: | Height: | Size: 299 KiB |
21
media/dockerfile-layers.svg
Normal file
|
After Width: | Height: | Size: 103 KiB |
5633
media/dockerizacija.excalidraw
Normal file
21
media/horizontal-scaling.svg
Normal file
|
After Width: | Height: | Size: 44 KiB |
21
media/port-publishing.svg
Normal file
|
After Width: | Height: | Size: 120 KiB |
BIN
media/volumes.png
Normal file
|
After Width: | Height: | Size: 4.1 MiB |
86
media/volumes.svg
Normal file
|
After Width: | Height: | Size: 68 KiB |
5
multistage-build/.dockerignore
Normal file
@ -0,0 +1,5 @@
|
||||
**/.env/
|
||||
**/__pycache__/
|
||||
**/Makefile
|
||||
**/Dockerfile
|
||||
*.log
|
||||
40
multistage-build/Dockerfile
Normal file
@ -0,0 +1,40 @@
|
||||
FROM python:3.11-slim-bookworm
|
||||
|
||||
# uid to run application
|
||||
ARG USER=1000
|
||||
# gid to run application
|
||||
ARG USER_GROUP=1000
|
||||
|
||||
# set virtual env path
|
||||
ENV \
|
||||
PATH=/venv/bin:$PATH
|
||||
|
||||
# does nothing, for documentation only
|
||||
EXPOSE 3000
|
||||
|
||||
# create and set main app directory
|
||||
WORKDIR /app
|
||||
|
||||
# install sys libs and create virtual env
|
||||
RUN \
|
||||
apt update && \
|
||||
apt install -y gcc libpq-dev python3-dev && \
|
||||
python -m venv /venv
|
||||
|
||||
# copy app files to /app directory
|
||||
COPY ./app .
|
||||
|
||||
# set permissions to log directory and install dependencies && cleanup
|
||||
RUN \
|
||||
chown -R ${USER}:${USER_GROUP} /app/log && \
|
||||
pip install -r requirements.txt && \
|
||||
apt purge --auto-remove -y
|
||||
|
||||
# set user to run application
|
||||
USER ${USER}:${USER_GROUP}
|
||||
|
||||
# start shell script when container starts
|
||||
ENTRYPOINT ["/app/run.sh"]
|
||||
|
||||
# no additional parameters to run.sh script
|
||||
CMD []
|
||||
56
multistage-build/Dockerfile.multistage
Normal file
@ -0,0 +1,56 @@
|
||||
FROM python:3.11-slim-bookworm AS install-dependencies
|
||||
|
||||
# create and set main app directory
|
||||
WORKDIR /app
|
||||
|
||||
# set virtual env path
|
||||
ENV \
|
||||
PATH=/venv/bin:$PATH
|
||||
|
||||
# install sys libs and create virtual env
|
||||
RUN \
|
||||
apt update && \
|
||||
apt install -y gcc libpq-dev python3-dev && \
|
||||
python -m venv /venv
|
||||
|
||||
# copy app files to /app directory
|
||||
COPY ./app/requirements.txt .
|
||||
|
||||
# install dependencies && cleanup
|
||||
RUN \
|
||||
pip install -r requirements.txt
|
||||
|
||||
|
||||
FROM python:3.11-slim-bookworm
|
||||
|
||||
# uid to run application
|
||||
ARG USER=1000
|
||||
# gid to run application
|
||||
ARG USER_GROUP=1000
|
||||
|
||||
# set virtual env path
|
||||
ENV \
|
||||
PATH=/venv/bin:$PATH
|
||||
|
||||
# does nothing, for documentation only
|
||||
EXPOSE 3000
|
||||
|
||||
# create and set main app directory
|
||||
WORKDIR /app
|
||||
|
||||
COPY --from=install-dependencies /venv /venv
|
||||
|
||||
# copy app files to /app directory
|
||||
COPY ./app .
|
||||
|
||||
# set permissions to log directory
|
||||
RUN chown -R ${USER}:${USER_GROUP} /app/log
|
||||
|
||||
# set user to run application
|
||||
USER ${USER}:${USER_GROUP}
|
||||
|
||||
# start shell script when container starts
|
||||
ENTRYPOINT ["/app/run.sh"]
|
||||
|
||||
# no additional parameters to run.sh script
|
||||
CMD []
|
||||
33
multistage-build/Makefile
Normal file
@ -0,0 +1,33 @@
|
||||
IMAGE_NAME=api-server-ms
|
||||
CONTAINER_NAME=api-server-ms
|
||||
|
||||
|
||||
build: clean
|
||||
@docker build \
|
||||
--progress=plain \
|
||||
--tag $(IMAGE_NAME) \
|
||||
.
|
||||
|
||||
|
||||
build-multistage: clean
|
||||
@docker build \
|
||||
--progress=plain \
|
||||
--tag $(IMAGE_NAME) \
|
||||
--file Dockerfile.multistage \
|
||||
.
|
||||
|
||||
|
||||
run:
|
||||
@docker run \
|
||||
--name $(IMAGE_NAME) \
|
||||
--publish 3000:3000 \
|
||||
--env CONTAINER_NAME="Awesome API server" \
|
||||
--volume /var/log/api-server:/app/log \
|
||||
--detach \
|
||||
$(CONTAINER_NAME)
|
||||
|
||||
|
||||
clean:
|
||||
- @docker stop $(CONTAINER_NAME)
|
||||
- @docker rm $(CONTAINER_NAME)
|
||||
- @docker rmi $(IMAGE_NAME)
|
||||
2
multistage-build/app/Makefile
Normal file
@ -0,0 +1,2 @@
|
||||
run:
|
||||
@./run.sh
|
||||
29
multistage-build/app/main.py
Normal file
@ -0,0 +1,29 @@
|
||||
from fastapi import FastAPI
|
||||
import logging
|
||||
import os
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
LOG_FILE = "./log/api-server.log"
|
||||
CONTAINER_NAME = os.environ.get("CONTAINER_NAME", "Unknown")
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
logging.info(f"Container {CONTAINER_NAME} received request")
|
||||
return {
|
||||
"message": f"Hello World from {CONTAINER_NAME}",
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
logging.basicConfig(
|
||||
filename=LOG_FILE,
|
||||
format="%(asctime)s %(levelname)s %(message)s",
|
||||
level=logging.INFO,
|
||||
)
|
||||
logging.info(f"Starting server {CONTAINER_NAME}")
|
||||
|
||||
|
||||
main()
|
||||
3
multistage-build/app/requirements.txt
Normal file
@ -0,0 +1,3 @@
|
||||
fastapi
|
||||
uvicorn
|
||||
psycopg2
|
||||
9
multistage-build/app/run.sh
Executable file
@ -0,0 +1,9 @@
|
||||
#!/usr/bin/sh
|
||||
|
||||
# start command with exec to replace execution shell and properly receive signals from docker
|
||||
# without exec, container can't be gracefully terminated with ctrl+c or docker stop
|
||||
exec uvicorn \
|
||||
main:app \
|
||||
--host 0.0.0.0 \
|
||||
--port 3000 \
|
||||
--lifespan off
|
||||