19
loading...
This website collects cookies to deliver better user experience
django-admin startproject dockerapp
python manage.py startapp sampleapp
INSTALLED_APPS,
and let's get started with dockerizing.Dockerfile
(no extension), and add the following codes.# pull the official base image
FROM python:3
# set environment variables
ENV PYTHONUNBUFFERED 1
# set work directory
WORKDIR /app
ADD . /app
COPY . /requirements.txt /app/requirements.txt
# install dependencies
RUN pip install -r requirements.txt
# copy project
COPY . /app
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
FROM python:3 sets the base image from which it will create the Docker container.
WORKDIR /app sets the working directory inside the container to /app.
ENV PYTHONUNBUFFERED 1 ensures that Python output is reported to the terminal, allowing real-time monitoring of Django logs.
COPY ./requirements.txt /app/requirements.txt copies the requirements.txt file to the container's work directory.
RUN pip install -r requirements.txt installs all the required modules for our application to run in the container.
EXPOSE 8000 allows port 8000 to be accessible from other applications.
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"] sets the container's executable commands.
docker build --tag dockerapp:latest
docker run --name dockerapp -d -p 8000:8000 dockerapp:latest