Categories
Software SysAdmin

Setting NIC IP addresses as environment variables

An easy and reliable method for applying your NIC IP addresses to your environment and scripts.

Example: Nginx with Docker

Save the following contents to /etc/systemd/system/docker-nginx.service

[Unit]
Description=Docker Nginx example.
Requires=docker.service setup-network-environment.service
After=docker.service setup-network-environment.service

[Service]
EnvironmentFile=/etc/network-environment
TimeoutStartSec=0
Restart=always
ExecStartPre=-/usr/bin/docker stop %n
ExecStartPre=-/usr/bin/docker rm %n
ExecStartPre=/usr/bin/docker pull nginx
ExecStart=/usr/bin/docker run \
    --rm \
    --name=%n \
    --publish ${DEFAULT_IPV4}:80:80 \
    nginx

[Install]
WantedBy=multi-user.target

Create an environment variable to use for the next set of commands:

sysDserviceSaveFile=/etc/systemd/system/docker-nginx.service

Run the following set of commands together, including the curly braces:

{

if $? -eq 0; then echo "Working on ${sysDserviceSaveFile}"; else echo "Unknown variable"; exit; fi

# Set permissions
chmod -v 644 ${sysDserviceSaveFile}

# Reload management parameters
systemctl daemon-reload

# Enable system service
systemctl enable "${sysDserviceSaveFile}"

# Start system service
systemctl start "${sysDserviceSaveFile}"

# Check status
systemctl status "${sysDserviceSaveFile}"

}

Leave a Reply

Your email address will not be published. Required fields are marked *