top of page
  • Brett Gianpetro

Quick Tip: Importing Data into Netbox Docker

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

315 views0 comments

Recent Posts

See All

Ansible, Netbox and BGP-EVPN - New GitHub Repositories!

After our initial foray into generating and applying network device configurations using only Ansible and Netbox with no Python logic layer (see our first musings on this subject here), we decided to

Ansible and Network-as-Code: Key Considerations

So you've decided to take your network to the next level with a network-as-code (NaC) model and commit (no pun intended) to Ansible as key component to your tool chain. Great! At Vector we frequentl

bottom of page