Truck Signs API - Docker setup
This guide explains how the Truck Sign Shop Django application is containerised and run alongside a PostgreSQL database using Docker only. Each service runs in its own container, and communication is handled via a cutom Docker network.
Table of Contents
Prerequisites
- Docker
Quickstart
- Clone the Repository and Enter the Project Directory
Start by cloning the project repository and enter the main directory:
git clone https://github.com/e1pmiS/truck_signs_api.git
cd truck_signs_api
- Network configutration
Create a custom Docker network called "ts_net" or other name to enable communication between your containers:
docker network create ts_net
When running your containers, attach them to this network using the --network option. Containers on the same network can communicate with each other using their container names as hostnames.
- Set Up Environment Variables
The project uses environment variables for configuration. A example file example.env is provided. Copy the sample file to create your own .env file:
cp example.env .env
then open the .env file and update any variables as needed, such as database credentials or your Django SECRET_KEY.
💡 If you don’t have a SECRET_KEY, see section Generating a Django SECRET_KEY for instructions on how to create one securely.
- Start PostgreSQL Container
docker run -d \
--name trucksigns_db \
--network ts_net \
--env-file truck_signs_designs/settings/.env \
-p 5433:5432 \
postgres:15
This exposes PostgreSQL on port 5433 of the host machine.
- Build the Application Image
docker build -t trucksigns_app:latest .
- Run the Application Container
docker run -it --network ts_net --restart=on-failure -p 8020:8000 trucksigns_app:latest
After Step 4,the Truck Sign Shop application will be running inside a Docker container and accessible on the port 8020 of the host machine and the container will restart once an error accured.
Usage
Generating a Django SECRET_KEY
A secure SECRET_KEY is essential for the security of your Django application. You can generate one using Python directly from your terminal.
Run the following command:
python -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())'
This will output a secure, random string you can use as your SECRET_KEY.
Django Superuser Creation on Container Start
Upon starting the Django application container, a prompt will appear requesting the following details for superuser creation:
-
Username
-
Email address
-
Password
The container should be run as indicated in the Quickstart section, and the prompts in the container’s terminal should be followed to complete the admin user setup.
Logging into Django Admin and Managing Products
you can log in and start managing the shop.
- Open your browser and go to:
http://<host_ip>:8020/admin
-
Log in using the superuser credentials you just created.
-
In the Django admin dashboard, you can:
-
Add Categories
-
Add Products under specific categories
-
Edit or delete existing items
External Deployment
To publish the server externally, you need to configure Django to allow requests from your external IP address or domain.
Open your base.py file and find the ALLOWED_HOSTS setting. Add your server's IP address or domain name:
ALLOWED_HOSTS = [<your_server_ip_address>, 'localhost']