How to Automatically Create Compose Files From Running Docker Containers
Reading Time: 3 minutesdocker-autocompose is a Python script that lets you effortlessly create Docker Compose files from running Docker containers.
Docker is an essential tool for easy installation of the apps which can run your sites and services, and it’s even easier to manage with docker-compose.
Some projects don’t come with docker-compose files, and it’s time-consuming to create one yourself. docker-autocompose can generate a working docker-compose file from any running Docker container.
Some Docker Apps Don’t Have Compose Files
Docker is an open-source platform for deploying applications in bundled components known as containers. By using Docker, you don’t need to worry about dependencies or interactions between services as these are usually pre-configured to some degree.
You can use Docker to deploy apps including specialized servers, Linux distros, or custom images. Running apps with Docker is relatively easy and can ensure you always have the latest software version.
If you’re not familiar with the concept of dockerized apps, you should read our essential guide to why you should use Docker instead of a virtual machine.
Docker commands are long and usually executed as a single command laying out all the relevant variables. They’re awkward to input, and although you can add an alias to your .bashrc file, it often comes down to repeatedly hitting Ctrl + R to find the exact Docker command you want.
Docker Compose is a tool to help you with cross-container interaction and orchestration. It’s so effective at helping you to manage Docker containers, that many users don’t learn how to use Docker at all. Why would you, when all you need to do is download a single YAML file, and bring it up with a simple two-word command?
While many apps which can be deployed by Docker do have Compose files, not all do, making it difficult for quick, clean Docker installations and deployments. This is especially common with apps for less mainstream hardware, such as ARM64 and ARMhf.
If you prefer to use docker-compose over raw Docker, you might choose not to use a particular piece of software if there isn’t a handy docker-compose.yml in the project’s GitHub repository.
docker-autocompose Makes It Easy to Automatically Generate a Compose File
docker-autocompose is a Python app that can create a working Compose file from any running Docker container.
This means you only need to run the docker command first, then take the generated output and start the app using docker-compose in the future, or you can append it to an existing Compose file to manage all of your services at once.
How to Install docker-autocompose on Linux
As docker-autocompose comes as a Python app, you need to have Python PIP installed on your Linux system.
To install Python PIP on Debian or Ubuntu:
sudo apt install python3-pip
Install PIP on Arch Linux and its derivatives using:
sudo pacman -S python-pip
To install PIP on CentOS and Red Hat Enterprise Linux, run:
sudo yum install python3 python3-wheel
It should go without saying that you also need to have Docker and Docker Compose installed.
Clone the GitHub repository for docker-autocompose and move into the new directory:
git clone https://github.com/Red5d/docker-autocompose.git
cd docker-autocompose
Now install docker-autocompose by running:
sudo python3 setup.py install
docker-autocompose is now installed.
Use docker-autocompose to Generate Compose Files
Often, while searching for essential Linux apps, you’ll stumble across an awesome project which comes with Docker images but without a Compose file. An example of this is the excellent browser-accessible IRC client, Dispatch.
Create a directory for Dispatch to use:
mkdir ~/dispatch
Download and start the client on port 8080 of your local machine with:
docker run -p 8080:80 -v /home/david/dispatch:/data --restart no -d khlieng/dispatch
Visit localhost:8080 in your browser to check if Dispatch is working, then tell docker-autocompose to generate Compose file contents from the running container with:
sudo autocompose.py <container-name-or-id>
In this case, the container name is dispatch, and the container ID will have appeared as output in your terminal immediately after running the docker command.
Alternatively, you can create Compose file contents which you can use to start up all of your running Docker containers with:
sudo autocompose.py $(docker ps -aq)
No Docker Compose file will be generated, so you need to copy the stdout to your clipboard, then create a new file with:
nano docker-compose.yml
Paste in the contents of your clipboard, then save and exit nano with Ctrl + O, then Ctrl + X.
You can now start all of your containers at once with:
docker-compose up -d
Alternatively, you can run docker-autocompose using Docker itself by running:
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock ghcr.io/red5d/docker-autocompose <additional-names-or-ids>
…which is a command that reminds you why you need docker-autocompose to start with.
Instantly Create Working Compose Files From Running Docker Containers
Docker and Docker Compose are an increasingly important part of the Linux app ecosystem and are proving especially valuable for self-hosting sites and services on the ubiquitous Raspberry Pi single-board computer.
If you’ve not already started your journey toward creating your own online ecosystem, you should take a look at some of the awesome web-facing projects you can run on the Pi.
Reference: https://www.makeuseof.com/create-docker-compose-files-from-running-docker-containers/