this post was submitted on 12 Oct 2023
2 points (100.0% liked)

Self-Hosted Main

511 readers
1 users here now

A place to share alternatives to popular online services that can be self-hosted without giving up privacy or locking you into a service you don't control.

For Example

We welcome posts that include suggestions for good self-hosted alternatives to popular online services, how they are better, or how they give back control of your data. Also include hints and tips for less technical readers.

Useful Lists

founded 1 year ago
MODERATORS
 

I recently hit an issue with my server which caused me to format my drive running Ubuntu, this caused me to lose my whole docker setup on that system. As an idiot, I didn't have any redundancies in place for such an event.

This lead me to have to re-set up each docker container I had originally, thankfully the volumes were separate, so all I needed was the compose files/run commands for each container, env var, and volume mappings.

With everything setup again, this sent me down a rabbit hole, looking to see if there was a way to back up my docker commands moving forward. Looking online, I can't see anywhere that shown how you can export your current docker containers as run commands for Linux.

I've now managed to create the below shell script, this will build the docker "run" commands complete with env vars, volume mappings, ports, IPs

#!/bin/bash

# Create the "docker_run" directory if it doesn't exist
mkdir -p docker_run

# Get a list of all running containers
containers=$(docker ps -q)

# Iterate over the containers and create individual sh files for each "docker run" command
for container_id in $containers; do
    # Get container information
    container_info=$(docker inspect $container_id)
    
    # Extract container name, image name, and environment variables
    container_name=$(echo $container_info | jq -r '.[0].Name' | cut -d "/" -f 2)
    image_name=$(echo $container_info | jq -r '.[0].Config.Image')
    
    # Extract and format environment variables
    env_vars=$(echo $container_info | jq -r '.[0].Config.Env | .[]' | sed 's/^/-e /' | tr '\n' ' ')

    # Extract container port mappings
    port_mappings=$(echo $container_info | jq -r '.[0].NetworkSettings.Ports | to_entries | .[] | .key + ":" + .value[0].HostPort' | tr '\n' ' ')

    # Extract container IP address and network
    container_ip=$(echo $container_info | jq -r '.[0].NetworkSettings.Networks | to_entries | .[] | .value.IPAddress')
    container_network=$(echo $container_info | jq -r '.[0].NetworkSettings.Networks | keys[0]')

    # Extract and format volume information
    volumes=$(echo $container_info | jq -r '.[0].Mounts | .[] | "-v " + .Source + ":" + .Destination' | tr '\n' ' ')

    # Create the "docker run" command with network, env vars, ports, IP assignment, and volumes
    docker_run_command="docker run -d --name $container_name --network $container_network --ip $container_ip $env_vars -p $port_mappings $volumes $image_name"

    # Create a shell file with the run command
    sh_file_contents="#!/bin/bash\n\n$docker_run_command"
    echo -e "$sh_file_contents" > "docker_run/$container_name.sh"
    # Make the file executable
    chmod +x "docker_run/$container_name.sh"
done

I don't know if this would be helpful for other, but it's certainly helping me, so I thought I'd post it to see if others would like the same.

This will create the "run" commands and store them in a directory "docker_run/{container_name}.sh" so should you ever need to set up a container again you would just need to run the .SH file and the container would pick up from where it last left off.

I've personally set this up as a cronjob, to keep my files updated regularly.

There are probably other ways to go about doing this, however this seems to work for me.

no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here