I am creating a Django application and now I am creating a docker container for that application. I am using the MySQL database with Django. So after creating my entire project when It was successfully running, I wanted to containerize it. I created the Dockerfile and docker-compose.yml file. Here is the content of my file.
Dockerfile
FROM python:3.10
ENV DockerHOME=/home/app/webapp
RUN mkdir -p $DockerHOME
WORKDIR $DockerHOME
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN pip install --upgrade pip
COPY . $DockerHOME
RUN pip install -r requirements.txt
# Add Django migrations and apply them
RUN python manage.py makemigrations
RUN python manage.py migrate
EXPOSE 8080
CMD ["python", "manage.py", "runserver", "0.0.0.0:8080"]
and here is my docker-compose.yml
version: '3'
services:
web:
image: blogs:latest
container_name: blogs_web
ports:
- "8080:8080"
volumes:
- .:/home/app/webapp
environment:
- PYTHONDONTWRITEBYTECODE=1
- PYTHONUNBUFFERED=1
- SECRET_KEY=${SECRET_KEY}
- DEBUG=${DEBUG}
- DATABASE_NAME=${DATABASE_NAME}
- DATABASE_USER=${DATABASE_USER}
- DATABASE_PASSWORD=${DATABASE_PASSWORD}
- DATABASE_HOST=${DATABASE_HOST}
- DATABASE_PORT=${DATABASE_PORT}
depends_on:
- db
db:
image: data:latest
container_name: blogs_db
environment:
MYSQL_DATABASE: ${DATABASE_NAME}
MYSQL_USER: ${DATABASE_USER}
MYSQL_PASSWORD: ${DATABASE_PASSWORD}
MYSQL_ROOT_PASSWORD: ${DATABASE_ROOT_PASSWORD}
ports:
- "3306:3306"
command: --default-authentication-plugin=mysql_native_password
When I run
docker-compose build
it gives me this response
(venv) PS C:UsersDellDownloadsBlogs> docker-compose build
[+] Building 0.0s (0/0)
The build isn’t right. and when I do
docker-compose up
here is my error
(venv) PS C:UsersDellDownloadsBlogs> docker-compose up
[+] Running 2/2
✘ web Error 3.8s
✘ db Error 3.8s
Error response from daemon: pull access denied for blogs, the repository does not exist or may require 'docker login': denied: requested access to the resource is denied```
I am using Windows. I don’t know what I am doing wrong. I would greatly appreciate your help.
2
This is how I fixed all the issues, cause I had issues with my docker-compose.yml file.
It resolved all my issues like:
- Not using ‘root’ user
- Not using ‘localhost’ as Database_HOST
- Writing a line in Dockerfile to handle static content of the Django application.
- DATABASE_ROOT_PASSWORD should always be there in docker-compose.yml file
version: '3'
services:
db:
image: mysql:5.7
container_name: blogs_mysql
restart: always
volumes:
- /opt/noorblogs/mysql_data:/var/lib/mysql
- /tmp/noorblogs/mysqld:/var/run/mysqld
environment:
MYSQL_ROOT_PASSWORD: ${DATABASE_PASSWORD}
MYSQL_DATABASE: ${DATABASE_NAME}
MYSQL_USER: ${DATABASE_USER}
MYSQL_PASSWORD: ${DATABASE_PASSWORD}
ports:
- "3307:3306"
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "--password=${DATABASE_PASSWORD}"]
timeout: 20s
retries: 10
backend:
build:
context: .
dockerfile: Dockerfile
container_name: blogs
command: sh -c "python3 manage.py migrate --noinput && python3 manage.py collectstatic --noinput && python manage.py runserver 0.0.0.0:8080"
restart: always
volumes:
- .:/app
ports:
- "8080:8080"
env_file:
- .env
depends_on:
db:
condition: service_healthy
and this is my Dockerfile
FROM python:3.10
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
WORKDIR /app
COPY requirements.txt /app/
COPY ./static/ /app/static/
RUN pip install -r requirements.txt
COPY . /app/
.env file
DATABASE_PASSWORD=*****
DATABASE_USER=mysql
DATABASE_NAME=noorblogs
DATABASE_HOST=host.docker.internal
DATABASE_PORT=3306
DEBUG=True
SECRET_KEY=*****
DATABASE_ROOT_PASSWORD=******