Export Docker Run Commands reddit / selfhosted ---------------------------------------------------------------------- FatalV0rt3x · 2023-10-12 11:24 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. ---------------------------------------------------------------------- sk1nT7 · 2023-10-12 11:36 1. Use docker compose and neglect docker run commands. 2. Sync your compose files into a private git repository 3. Implement a 3-2-1 backup strategy 4. Profit This tool is also a gem and can create compose files based on your running containers: https://github.com/Red5d/docker-autocompose I've also a few compose examples here: https://github.com/Haxxnet/Compose-Examples ---------------------------------------------------------------------- bklyngaucho · 2023-10-12 11:57 \^This is the way ---------------------------------------------------------------------- isleepbad · 2023-10-12 12:15 Why reinvent the wheel? Use infrastructure as code such as SALT, Ansible or Terraform. Back those up never worry about setting up infrastructure ever again. ---------------------------------------------------------------------- geo38 · 2023-10-12 12:36 A simpler way to recreate the docker run command for a container is the runlike tool: https://hub.docker.com/r/assaflavie/runlike/ ---------------------------------------------------------------------- paschty · 2023-10-12 14:12 You can use [https://www.composerize.com/](https://www.composerize.com/) to convert your run commands to to docker-compose files. ---------------------------------------------------------------------- CatoDomine · 2023-10-12 14:41 Useful script if you have a bunch of running containers that you used `docker run` to start. I appreciate the work and the sharing. However, this is not the best way to manage docker containers. As others have mentioned, you really should be using `docker-compose`. Keep your compose.yml files in version control. ---------------------------------------------------------------------- sk1nT7 · 2023-10-12 14:48 But it is quite limited and incomplete in my personal experience. Not sure whether it became better but it worked for very easy docker run commands only in the past. ---------------------------------------------------------------------- joecool42069 · 2023-10-12 15:48 Seems like an awful lot of work to avoid using docker-compose which solves this problem. ---------------------------------------------------------------------- FatalV0rt3x · 2023-10-12 18:12 For me, docker-compose seems to take longer to set up a container vs run commands. The time between running the docker-compose up command and the container being ready is quite long, I don't know why but the run commands seem quicker. Totally agree though VC with compose files would be better for change tacking and backup, plus I can easily pull them into portainer as stacks. ---------------------------------------------------------------------- FatalV0rt3x · 2023-10-12 18:13 Docker autocompose looks pretty good. I usually have speed issues with compose. Might be worth giving it another go. ---------------------------------------------------------------------- sk1nT7 · 2023-10-12 18:29 Speed issues? ---------------------------------------------------------------------- FatalV0rt3x · 2023-10-12 18:49 Yes, for some reason when using docker-compose on my server it takes forever to spin up the container, using the run commands it completes within a few seconds ---------------------------------------------------------------------- sk1nT7 · 2023-10-12 18:57 Hmm that's weird. Both will just pull the relevant images and then start the containers. No real magic behind. May update your system. The docker-compose command is deprecated. It's docker compose now. ---------------------------------------------------------------------- FatalV0rt3x · 2023-10-12 20:00 Yes, I'm aware, but this still has the same problem unfortunately ---------------------------------------------------------------------- FatalV0rt3x · 2023-10-12 20:01 Yes I'm aware, this still has the same issue unfortunately ---------------------------------------------------------------------- broetchenrackete · 2023-10-12 22:26 I agree that using docker compose is probably the way to go but there is a tool doing exactly what you were looking for. Creating docker run commands from running containers: https://github.com/bcicen/docker-replay ---------------------------------------------------------------------- FatalV0rt3x · 2023-10-12 23:01 If only I'd known this before I'd built my shell script. Couldn't find one when searching google ---------------------------------------------------------------------- Toastytodd4113113 · 2023-10-13 00:16 i use the it tools container. has a TON of useful tools