Here at Vector we commonly use the pre-packaged Netbox container, netbox-docker, to develop and test. One common workflow is for us to spin up the container, build and model Netbox data, and then destroy the container. However, we frequently have the need to re-use or share the data that we have modeled. As such, we developed the following method to export data out of the container and re-import it into a new container. It should be noted that the Netbox Docker project does provide a nifty method of seeding data via "initializers" -- we just haven't found it easy to integrate into our "model-import-export" flow.
Exporting Data out of a Netbox Container
After you have instantiated your container and subsequently built out all of the data, it can be exported out of the container as follows (on the Docker host machine):
docker exec -it netboxdocker_postgres_1 pg_dump -d netbox -U netbox > netbox_data.sql
This will save all the database data in the current directory in a file entitled 'netbox_data.sql'.
Importing Data into a Netbox Container
Netbox Docker uses a separate Postgres container (via Docker Compose) to host the database for Netbox, so all we need to do is get our data imported into that container. Per the DockerHub description of the postgres container, all *.sql files in the /docker-entrypoint-initdb.d directory will be loaded into the database, so our job is simply to copy our exported file, 'netbox_data.sql', into that directory when the container is spun up. In order to do so, we'll need to modify the netbox-docker Docker Compose file to build the postgres container locally instead of simply pulling down an image. Assuming that you have cloned the netbox-docker repo and you are in its base directory, this can be accomplished as follows:
sed -i 's/image: postgres:.*-alpine/build: .\/postgres/' docker-compose.yml
Next we need to create the local directory in which the Postgres container can be built, create its Dockerfile, and copy our Netbox data into it (assuming you are still in the netbox-docker base directory):
mkdir postgres && cd postgres
echo 'FROM postgres:12-alpine' > Dockerfile
echo 'COPY *.sql /docker-entrypoint-initdb.d/' >> Dockerfile
cp /PATH/TO/netbox_data.sql .
cd ..
That's it! Now all you need to do is do your normal Docker Compose commands to bring netbox-docker online and your database will be pre-populated with the previously exported data:
docker-compose pull
docker-compose up
Comments