cheatsheet, please refer to linked articles for details.
which ones ? | containers | images | both |
---|---|---|---|
all (details) |
docker rm $(docker ps -a -q) | docker rmi $(docker images -q) | docker rm $(docker ps -a -q) && docker rmi $(docker images -q) |
exited | docker rm $(docker ps -a | awk '/Exited \(/ { print $1 }') | Not applicable | docker rm $(docker ps -a | awk '/Exited \(/ { print $1 }') && docker rmi $(docker images -f "dangling=true" -q) |
dangling (untagged) | Not applicable | docker rmi $(docker images -f "dangling=true" -q) |
testenvironments— this is probably a bad idea.
{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}
' "$containerId"; doneRemember :
Target : an Ubuntu machine on which I can log in via ssh to play with Ansible.
There's an advanced version of this procedure describing the setup with SSH keys.
FROM ubuntu:16.04 RUN apt-get update && \ apt-get install -y iproute2 iputils-ping openssh-server && \ apt-get clean && \ useradd -d /home/ansible -s /bin/bash -m ansible && \ echo ansible:elbisna | chpasswd EXPOSE 22 CMD [ "sh", "-c", "service ssh start; bash"]
Sending build context to Docker daemon 2.56kB Step 1/4 : FROM ubuntu:16.04 ---> 5e13f8dd4c1a Step 2/4 : RUN apt-get update && apt-get install -y iproute2 iputils-ping openssh-server && apt-get clean && useradd -d /home/ansible -s /bin/bash -m ansible && echo ansible:elbisna | chpasswd ---> Running in 9f0ef385f89b Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB] Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [109 kB] ... ... ... Step 4/4 : CMD [ "sh", "-c", "service ssh start; bash"] ---> Running in a377195f387b Removing intermediate container a377195f387b ---> 65887834663f Successfully built 65887834663f Successfully tagged myubuntu:latest
REPOSITORY TAG IMAGE ID CREATED SIZE myubuntu latest 65887834663f 2 minutes ago 205MB
a914ca30d7181e7d39a272f633a8e180151c2b441845d0eae882bd7d8b16fdff
root@a914ca30d718:/#
root@hostname:/#
root@a914ca30d718:/# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 141: eth0@if142: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0 valid_lft forever preferred_lft forever
root@a914ca30d718:/# service ssh status * sshd is running root@a914ca30d718:/# ss -punta Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port tcp LISTEN 0 128 *:22 *:* users:(("sshd",pid=31,fd=3)) tcp LISTEN 0 128 :::22 :::* users:(("sshd",pid=31,fd=4))
root@a914ca30d718:/# read escape sequencethen back to your system prompt
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a914ca30d718 myubuntu "sh -c 'service ssh …" 43 minutes ago Exited (0) 6 seconds ago myubuntu1
myubuntu1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a914ca30d718 myubuntu "sh -c 'service ssh …" 45 minutes ago Up 24 seconds 22/tcp myubuntu1
root@a914ca30d718:/#
ansible@a914ca30d718:~$
FROM ubuntu:16.04 ARG userName='ansible' ARG homeDir="/home/$userName" ARG sshDir="$homeDir/.ssh" ARG authorizedKeysFile="$sshDir/authorized_keys" ARG publicSshKey='./ansible.pub' RUN apt-get update && \ apt-get install -y iproute2 iputils-ping openssh-server && \ apt-get clean && \ useradd -d "$homeDir" -s /bin/bash -m "$userName" && \ mkdir -p "$sshDir" COPY "$publicSshKey" "$authorizedKeysFile" RUN chown -R "$userName":"$userName" "$sshDir" && \ chmod 700 "$sshDir" && \ chmod 600 "$authorizedKeysFile" EXPOSE 22 CMD [ "sh", "-c", "service ssh start; bash"]
{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}
' myubuntu1)CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESno running container
REPOSITORY TAG IMAGE ID CREATED SIZE tomcat latest 142fe91d8add 19 minutes ago 723MB tomcat 9.0.39-jdk11-openjdk 2703bbe9e9d4 6 days ago 648MB
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8e0f14c049b5 142fe91d8add "catalina.sh run" 46 seconds ago Up 44 seconds 8009/tcp, 8080/tcp amazing_satoshi
8e0f14c049b5
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESnot running anymore
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8e0f14c049b5 142fe91d8add "catalina.sh run" 4 minutes ago Exited (143) About a minute ago amazing_satoshi
8e0f14c049b5and this time I get the shell prompt back !
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8e0f14c049b5 142fe91d8add "catalina.sh run" 6 minutes ago Up 17 seconds 8009/tcp, 8080/tcp amazing_satoshi
8e0f14c049b5
8e0f14c049b5
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESit's gone!
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 31316d067d12 142fe91d8add "catalina.sh run" 35 seconds ago Up 33 seconds 8009/tcp, 8080/tcp myContainer
# "Log As root In Container" laric() { containerName=$1 [ -z "$containerName" ] && { cat <<-EOF No container name specified. Must be one of : $(docker ps | awk '!/NAMES/ {print "\t"$NF}' | sort) EOF return } containerId=$(docker ps | awk -v containerName="$containerName" '$NF==containerName {print $1}') docker exec -it "$containerId" bash }
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4bac82b3ba41 molecule_local/ubuntu:18.04 "bash -c 'while true…" 28 minutes ago Up 28 minutes ubuntu1804
ubuntu1804
log into the container, just run a shell :
root@ubuntu1804:/#
OCI runtime exec failed: exec failed: container_linux.go:348: starting container process caused "exec: \"/bin/bash\": stat /bin/bash: no such file or directory": unknownThis just means /bin/bash can't be found in the image (i.e. Bash is not installed). Workaround : try with /bin/sh.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4bac82b3ba41 molecule_local/ubuntu:18.04 "bash -c 'while true…" 28 minutes ago Up 28 minutes ubuntu1804 51a02953b18a molecule_local/centos:7 "bash -c 'while true…" 4 hours ago Up 4 hours instance
51a02953b18a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4bac82b3ba41 molecule_local/ubuntu:18.04 "bash -c 'while true…" 28 minutes ago Up 28 minutes ubuntu1804 51a02953b18a molecule_local/centos:7 "bash -c 'while true…" 4 hours ago Up 4 hours (Paused) instance
51a02953b18a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4bac82b3ba41 molecule_local/ubuntu:18.04 "bash -c 'while true…" 28 minutes ago Up 28 minutes ubuntu1804 51a02953b18a molecule_local/centos:7 "bash -c 'while true…" 4 hours ago Up 4 hours instance
51a02953b18a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4bac82b3ba41 molecule_local/ubuntu:18.04 "bash -c 'while true…" 28 minutes ago Up 28 minutes ubuntu1804
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4bac82b3ba41 molecule_local/ubuntu:18.04 "bash -c 'while true…" 28 minutes ago Up 28 minutes ubuntu1804 51a02953b18a molecule_local/centos:7 "bash -c 'while true…" Exited (137) 13 minutes ago instance
Steps below are run as root.
nfsServer='10.27.25.5'; nfsExportPath="/filer/projects/$HOSTNAME"; nfsMountPoint='/mnt/nfs'; dockerDataDir='/var/lib/docker'; dockerDataDir_backupDir="$nfsMountPoint/varLibDocker"; backupDirVolumes='backupDockerVolumes'; volumesToBackup='volume1Name;volume1Dir volume2Name;volume2Dir volume3Name;volume3Dir'; backupDirContainers='backupDockerContainers'; containerData='container1Name;container1ServiceName container2Name;container2ServiceName'
volumesToBackup
and containerData
are actually shell tuplesvolumenDir
relates to is not (yet) clear to me. Looks like it's something "inside" the volume itself, or some kind of mount point...containernServiceName
will be used to build commands such as : systemctl start containernServiceName
getContainerIdByName() { imageName=$1; docker ps -a | awk -v needle="$imageName" '$0 ~ needle {print $1}'; }
systemctl stop docker.service; mkdir -p "$dockerDataDir_backupDir"; umount "$nfsMountPoint"; mount -t nfs -o v3,async "$nfsServer:$nfsExportPath" "$nfsMountPoint" && time rsync -avz --delete "$dockerDataDir/" "$dockerDataDir_backupDir"; systemctl start docker.service
This step provides double security for the data (snapshot + file copy), but is not mandatory. It can take a long time, depending on the amount of data and the network/disks performance.cd "$nfsMountPoint"; mkdir -p "$backupDirVolumes"; for tuple in $volumesToBackup; do volumeName=$(echo $tuple | cut -d ';' -f 1); volumeDir=$(echo $tuple | cut -d ';' -f 2); archiveName="$backupDirVolumes/$volumeName.tar"; echo -e "\n######## WORKING ON '$volumeName' '$volumeDir' ########"; docker run -it --rm -v "$volumeName":"$volumeDir" -v "$PWD/$backupDirVolumes":"/$backupDirVolumes" alpine tar -cf "$archiveName" "$volumeDir"; ls -lh "$archiveName"; done
absolutePathToBackupDir="$nfsMountPoint/$backupDirContainers"; mkdir -p "$absolutePathToBackupDir"; for tuple in $containerData; do containerName=$(echo $tuple | cut -d ';' -f 1); containerId=$(getContainerIdByName "$containerName"); echo -e "\n######## WORKING ON '$containerName' ########"; imageName="$containerName:$(date +%F_%H-%M-%S)"; archiveName="$absolutePathToBackupDir/$containerName.tar"; echo ' stopping...'; docker stop "$containerId"; echo ' committing...'; docker commit "$containerId" "$imageName"; echo ' saving...'; docker save "$containerName" -o "$archiveName"; ls -lh "$archiveName"; done
for tuple in $volumesToBackup; do volumeName=$(echo $tuple | cut -d ';' -f 1); echo -e "\nCreating volume '$volumeName' : "; docker volume create "$volumeName" && echo 'OK' || echo 'KO'; done
cd "$nfsMountPoint"; for tuple in $volumesToBackup; do volumeName=$(echo $tuple | cut -d ';' -f 1); volumeDir=$(echo $tuple | cut -d ';' -f 2); echo -e "\n######## RESTORING '$volumeName' '$volumeDir' ########"; archiveName="$backupDirVolumes/$volumeName.tar"; docker run -it --rm -v "$volumeName":"$volumeDir" -v $PWD"/$backupDirVolumes":"/$backupDirVolumes" alpine tar -xf "$archiveName"; done
for tuple in $containerData; do containerName=$(echo $tuple | cut -d ';' -f 1); echo -e "\n######## RESTORING CONTAINER '$containerName' ########"; containerToRestore="$nfsMountPoint/$backupDirContainers/$containerName.tar"; [ -f "$containerToRestore" ] && docker load --input "$containerToRestore" || echo "file '$containerToRestore' not found"; done
for tuple in $containerData; do containerName=$(echo $tuple | cut -d ';' -f 1); containerServiceName=$(echo $tuple | cut -d ';' -f 2); echo -e "\nStarting container '$containerName' ('$containerServiceName') :"; systemctl start "$containerServiceName" && echo 'OK' || echo 'KO'; done