Files
docker/README.md
2025-02-15 11:12:44 +00:00

204 lines
3.8 KiB
Markdown

# docker
## Setup
``` bash
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh
docker run hello-world
```
### Alphine
source https://wiki.alpinelinux.org/wiki/Docker
```bash
apk add docker
addgroup username docker
rc-update add docker default
service docker start
```
### fuse overlay
```bash
apt install --yes fuse-overlayfs
```
## Containers
### Containers list
```bash
docker container list
```
### Interactive shell
```bash
#example with ubuntu
docker container run -it ubuntu /bin/bash
```
### Remove when finished
```bash
#example with ubuntu
docker container run -rm ubuntu
```
### Attach to a running container
```bash
#attach to stdout of container with name ubuntu
docker container run -a ubuntu
## connects to container with name ubuntu and attach to bash (interactive)
docker exec -it pm_wp_adminer /bin/bash
```
## Volumes
### Export / Backup
```bash
#export volumes (tar) from dbstore container into volume
docker run --rm --volumes-from dbstore -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata
```
### Import / Restores
```bash
#inports volumes (untar) into dbstore2
docker run --rm --volumes-from dbstore2 -v $(pwd):/backup ubuntu bash -c "cd /dbdata && tar xvf /backup/backup.tar --strip 1"
```
### NFS
Notes:
docker-compose.yaml
```yaml
volumes:
volume00:
driver: local
driver_opts:
type: ${VOLUME_TYPE}
o: ${VOLUME_O}
device: "${VOLUME_DEVICE}"
```
.env
```text
VOLUME_TYPE="nfs"
# DNS server ip, RW:read and write
VOLUME_O="addr=10.10.1.1,rw"
# Folder on NFS Server
## Must exists on nfs server, or security errors when starting container
VOLUME_DEVICE=":/export/docker-volumes/volume00"
```
## Network
### Macvlan
#### Create
How to create a docker macvlan network
```bash
# run on host
docker network create -d macvlan --subnet=192.168.1.249/24 --gateway=192.168.1.1 -o parent=eth0 macvlan_network
```
#### Host comunication
if network required between host and docker container on a macvlan docker network
* Option 1 - Create another macvlan on host
* Setup vars
```bash
MACVLAN_NAME=macvlan_bridge
HOST_ETHERNET_ADP=eth0
NETWORK=192.168.1.0/24
```
* Create mavclan adapter
```bash
ip link add $MACVLAN_NAME link $HOST_ETHERNET_ADP type macvlan mode bridge
ip addr add $NETWORK dev $MACVLAN_NAME
ifconfig $MACVLAN_NAME up
```
* Add routing
```bash
ip route add $CONTAINER_MAVLAN_IP dev $MACVLAN_NAME
```
* Delete mavclan adapter
```bash
ifconfig $MACVLAN_NAME down
ip link del $MACVLAN_NAME
```
* Persisting macvlan configuration
Example:
* network: 192.168.1.0/24
* macvlan name:: macvlan_bridge
* macvlan static ip: 192.168.1.240/32
* docker containers on macvlan networks:
* 192.168.1.254
* 192.168.1.253
* 192.168.1.250
* 192.168.1.248
```text
# create/edit /etc/network/interfaces.d/macvlan_bridge
iface macvlan_bridge inet manual
pre-up ip link add macvlan_bridge link eth0 type macvlan mode bridge
pre-up ip addr add 192.168.1.240/32 dev macvlan_bridge
up ip link set macvlan_bridge up
post-up ip route add 192.168.1.254 dev macvlan_bridge
post-up ip route add 192.168.1.253 dev macvlan_bridge
post-up ip route add 192.168.1.252 dev macvlan_bridge
post-up ip route add 192.168.1.250 dev macvlan_bridge
post-up ip route add 192.168.1.248 dev macvlan_bridge
```
## log
write to docker log buffer
``` bash
echo "Hello World!" >> /proc/1/fd/1
```
clear all docker logs
``` bash
truncate -s 0 /var/lib/docker/containers/*/*-json.log;
```
## delete all images
``` bash
docker rmi -f $(docker images -aq)
```
## delete all images
``` bash
docker volume rm $(docker volume ls -qf dangling=true)
```