How to Set a Static IP Address in Boot2Docker: A Complete Step-by-Step Guide
Website Visitors:Configuring a static IP address on your Boot2Docker virtual machine (VM) is critical when you need predictable network access for development, testing, or integration with other services. Boot2Docker, once the default Docker VM solution for non-native environments, runs on TinyCore Linux and offers a simple startup script mechanism that can automate network settings—including static IP assignment—at every boot.
In this guide, I’ll show you how to overwrite DHCP and set a fixed IP address on your Boot2Docker VM’s primary network adapter (usually eth0). We’ll use Boot2Docker’s powerful /var/lib/boot2docker/bootsync.sh startup script, ensure it’s executable, and reboot to apply the changes.
Prerequisites
- You have a Boot2Docker VM running (via VirtualBox, VMware, or similar).
- You can SSH into the Boot2Docker VM.
- You have root privileges inside the VM (most Boot2Docker sessions start with root access).
Step 1: Identify Your Network Interface
Login to Boot2Docker VM and list network interfaces with:
|
|
Look for the main adapter, typically named eth0, which will have a hardware MAC address and your current IP (usually assigned via DHCP).
Step 2: Create the bootsync.sh Script
Boot2Docker executes /var/lib/boot2docker/bootsync.sh at every boot, before the Docker daemon starts. We’ll use this script to kill the DHCP client and assign a static IP.
Run below command to create bootsync.sh file and edit with vi editor:
|
|
Paste the following script, replacing the IP, netmask, gateway, and DNS as needed for your environment:
|
|
Step 3: Make bootsync.sh Executable
Boot2Docker won’t run your script unless it’s executable. Set permissions with:
|
|
Step 4: Reboot the VM
Now, to activate your changes, reboot your Boot2Docker VM:
|
|
Step 5: Verify Static IP Assignment
After reboot, SSH back in and check your IP:
|
|
You should see your chosen static IP assigned to eth0. Also, check that DNS resolution works:
|
|
Place all the commands you want to run automatically after a reboot inside the same
bootsync.shfile, using thesudofor each command as needed. Files or folders placed in the /var/lib/boot2docker directory persist and are not deleted after a reboot.
Troubleshooting Tips
- Interface name: If your main NIC is not
eth0, change the script variable to match (e.g.,eth1). - Persistent changes: The bootsync.sh script runs on every boot, providing persistent static configuration.
- Docker containers: This setting affects the VM’s primary IP. For container IPs, use Docker’s bridge/network controls.
- Updating settings: Edit
/var/lib/boot2docker/bootsync.sh, update values, and reboot. - DHCP conflict: Disable or adjust DHCP settings in your VM host network (e.g., VirtualBox Host-only Manager) for best results.
Conclusion
With this workflow, Boot2Docker can reliably present the same IP every time, making development and networking much more predictable, especially in complex setups or when interfacing with other hosts or cloud services. The bootsync.sh hack is flexible and can be extended to other boot-time tasks as well!
Your inbox needs more DevOps articles.
Subscribe to get our latest content by email.
