difference VM: no hypervisor
Advantages
light: fast installation, not as big as VMs
automation: scripting, configuration
standardization: across environments
Setup
Environment Variables: -e flag in ‘docker run‘
start
docker init: create all docker files
docker build [options, -t necessary!] path. builds image.
path: often . , is the build context path
option -t: tag. imageName. overwrites if existing and old one becomes unnamed.
option -f: file. Dockerfile.
docker run -d [--name containerName] -p 8000:8000 imageName. creates and runs container.
important: set flags (-p, -d) will be used for docker start!
--name: must not exist
existing: docker rm imageName
-d: detached mode for running in background
-p: publish. host:container
docker start containerName. starts existing container.
docker exec name
docker logs name
manage
docker ps -> lists all containers
docker stop name
docker rm name
docker save imageName:tag > fileName.tar
docker compose up [service1] [service2]. runs containers together.
-d, -f otherComposeFile.yaml
docker compose down. shutdown containers.
docker system prune: delete unused containers & images
Dockerfile
FROM [--platform=] [:] [AS ]
WORKDIR
dest: destination in the container!
Purpose: workdir for all subsequent operations
COPY
RUN -> creates layers
ENTRYPOINT: like CMD, specify base script / command to run
pass args: docker run imageName arg1 arg2 "arg3"
Example: if ENTRYPOINT ["/app/entrypoint.sh"] -> docker runs /app/entrypoint.sh arg1 arg2 "arg3"
override EP: docker run --entrypoint otherEP
CMD: overridable commands
override CMD:
- no EP: docker run -it imageName anotherCommandThanCMD example CMD: ["python", "/app/my_script.py", "-sth"]
- with EP: passed args to docker run example: ENTRYPOINT ["python", "/app/my_script.py"], CMD: ["--default-arg","-default-flag"] no CMD: keep container alive by CMD ["sleep", "infinity"] ######### example FROM alpine:latest # Base image WORKDIR /app # Set working directory COPY hello.sh . # Copy file to container RUN chmod +x hello.sh # Make script executable ENTRYPOINT ["./hello.sh"] # Run script on container start ######## Concepts Bind Mount purpose: share files between host & container sytnax: docker run -p 8000:8000 --name nameContainer --mount "type=bind,source=${PWD}/pathHost,target=/app/a.ipynb" nameImage Volume purpose: data persistence: keep data persistent after container deletion multiple containers: share data between multiple containers concept: folder managed by docker to save data
= VM management workflows tool (more heavyweight than containers)
Vagrant Box = VM Image
cmd vagrant init [boxName] [url] vagrant up: start according to vagrantfile vagrant ssh: ssh connection to started VM vagrant reload: restart VM and apply changes according to vagrantfile vagrant halt: shuts down VM but state saved. vagrant destroy: stops & deletes VM
vagrant global-status
Vagrant file
vagrant.configure("2") do |config| #v2 of the config
config.vm.box = "ubuntu/jammy64" # box configuration, Ubuntu Server config.vm.synced_folder "hostDir", "vmDir" # Kopieren von HOST-Verzeichnis in VM-Verzeichnis config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1" # Port-Weiterleitung für Zugriff vom Host auf Port 8080 der VM config.vm.provider "virtualbox" do |vb| # VirtualBox-Providerconfiguration vb.name = "nginx" # box name vb.memory = 2048 # 2GB Memory vb.cpus = 1 # 1 CPU end config.vm.provision "shell", path: "provision-webserver.sh"# Shell-Provisioner für Webanwendung end
#!/bin/bash apt-get update apt-get upgrade -y
apt-get install -y nginx