Skip to main content

Baby Tools Shop - Docker setup

This guide walks you through the steps required to containerize a Django application using Docker. It covers setting up the Docker environment, writing Dockerfile, building the image, and running the container.


Table of Contents​


Prerequisites​

  • Docker

Quickstart​

Follow these steps to quickly run the Baby Tools Shop app using Docker:

  1. Clone the project (or copy files into a folder)
git clone https://github.com/e1pmiS/baby-tools-shop.git
cd baby-tools-shop/babyshop_app
  1. Build the Docker image
docker build -t baby_shop:latest .
  1. Run the container
docker run -it -p 8025:8000 baby_shop:latest

After Step 3, the Baby Tools Shop Django application will be running inside a Docker container, and it will be accessible externally on port 8025 of your host machine.

Usage​

Creating a Django Superuser​

To manage products and categories on the admin interface, you need a Django superuser.

Follow these steps:

  1. List running containers to find your Django container ID:
docker container list -a
  1. Access the running container's shell:
docker exec -it <container_id> bash
  1. Run the Django management command:
python3 manage.py createsuperuser

Follow the prompt and once complete, a superuser account will be created in the database.

Logging into Django Admin and Managing Products​

After creating a superuser, you can log in and start managing your shop.

  1. Open your browser and go to:
http://localhost:8025/admin
  1. Log in using the superuser credentials you just created.

  2. In the Django admin dashboard, you can:

  • Add Categories (e.g., Strollers, Toys, Clothing)

  • 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 settings.py file and find the ALLOWED_HOSTS setting. Add your server's IP address or domain name:

ALLOWED_HOSTS = ['your.server.ip.address', 'localhost']

Explanation​

This section explains how to create a Dockerfile to containerize a Django application.


Dockerfile Setup​

Create a file named Dockerfile in the root directory.

Choose a base image that provides Python 3.10-slim to ensure the right runtime environment with minimal installations:

FROM python:3.10-slim

Set environment variables to prevent Python from creating .pyc files and to ensure output logs show immediately:

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

Define a working directory inside the container where all commands will be run and files copied:

WORKDIR /babyshop_app

Copy your Python dependencies file so you can install them inside the container:

COPY requirements.txt .

Upgrade pip and install the Python packages listed in requirements.txt:

RUN pip install --upgrade pip && pip install -r requirements.txt

Copy all your project files into the container’s working directory:

COPY . $WORKDIR

Expose port 8000 to allow external access to the Django development server running inside the container:

EXPOSE 8000

Set the default command to run database migrations and then start the Django development server, making it accessible from any IP:

CMD python3 manage.py migrate && python3 manage.py runserver 0.0.0.0:8000