DEV Community

Pranav Bakare
Pranav Bakare

Posted on • Edited on

Docker Python Django initial configuration setup

Docker WorkFlow

1. Ensure Your Dockerfile is Correct

Make sure your Dockerfile is ready and located in your project's root directory. Based on the previous discussion, your Dockerfile might look like this:

FROM python:3.11

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

WORKDIR /app

COPY requirements.txt .

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

COPY . .

EXPOSE 8000

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Enter fullscreen mode Exit fullscreen mode

.
.
.

2. Build the Docker Image

Open your terminal and navigate to the directory where your Dockerfile is located. Then run the following command to build the Docker image and name it my-docker-image:

docker build -t my-docker-image .
Enter fullscreen mode Exit fullscreen mode

This command runs the container and maps port 8000 of the container to port 8000 on your local machine, allowing you to access the Django application via http://localhost:8000.

If you want to run the container in the background, add the -d option:

docker run -d -p 8000:8000 my-docker-image
Enter fullscreen mode Exit fullscreen mode

This will start the container in detached mode.

**docker images**

To check the Docker images available on your system, you can use the following command:

docker images
Enter fullscreen mode Exit fullscreen mode

This command will display a list of all Docker images, along with their REPOSITORY, TAG, IMAGE ID, CREATED, and SIZE.

Example Output:

REPOSITORY          TAG       IMAGE ID       CREATED        SIZE
my-docker-image     latest    d1a1f2e8f7b2   2 hours ago    450MB
python              3.11      a2d3c4e5f6g7   5 days ago     800MB
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • REPOSITORY: The name of the image, e.g., my-docker-image.
  • TAG: The tag of the image, often used to specify versions.
  • IMAGE ID: A unique identifier for the image.
  • CREATED: The time when the image was created.
  • SIZE: The size of the image.

.
.
.

3.Build Docker Container

The command you've provided will run a Docker container named my-docker-container in detached mode, mapping port 8001 on your local machine to port 8000 inside the container. Here’s what the command does:

Command:

docker run -d --name my-docker-container -p 8001:8000 my-docker-image
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • -d: Runs the container in detached mode, meaning it runs in the background.
  • --name my-docker-container: Assigns the name my-docker-container to the container.
  • -p 8001:8000: Maps port 8001 on your local machine to port 8000 inside the container. This allows you to access the Django application at http://localhost:8001.
  • my-docker-image: Specifies the Docker image to use for the container.

Verify the Container is Running

After running this command, you can check if the container is running by using:

docker ps
Enter fullscreen mode Exit fullscreen mode

This will list all the running containers along with their names, status, and port mappings.

Access the Application

You can now access your Django application by navigating to http://localhost:8001 in your web browser.

.
.
.

4.Docker Volume

docker run -d --name my-docker-container -p 8001:8000 -v .:/app my-docker-image

Enter fullscreen mode Exit fullscreen mode

The docker run command you provided is used to start a Docker container from a Docker image. Here's a breakdown of the command:

  • -d: Runs the container in detached mode (in the background).
  • --name my-docker-container: Assigns a name to the container (my-docker-container).
  • -p 8001:8000: Maps port 8000 inside the container to port 8001 on your host machine. This means you can access the service running in the container at localhost:8001 on your host machine.
  • -v .:/app: Mounts the current directory (.) from your host machine to the /app directory inside the container. This is useful for development when you want to see changes in real-time without rebuilding the image.
  • my-docker-image: Specifies the Docker image to use for the container.

So, this command will start a container in the background, with port 8000 inside the container accessible on port 8001 of your host machine, and it will mount the current directory to /app in the container. If you need any adjustments or further explanation, feel free to ask!

.
.
.

5.Docker-compose.yml

A docker-compose.yml file is used to define and run multi-container Docker applications. Here's a basic example of a docker-compose.yml file based on your docker run command:

version: '3.8'  # Specify the version of Docker Compose

services:
  my-service:
    image: my-docker-image  # The Docker image to use
    container_name: my-docker-container  # The name of the container
    ports:
      - "8001:8000"  # Map port 8000 in the container to port 8001 on the host
    volumes:
      - .:/app  # Mount the current directory to /app in the container
    # Optional: Add environment variables if needed
    # environment:
    #   - ENV_VAR_NAME=value
    # Optional: Specify any commands to run
    # command: python app.py

# Optional: Define networks or other configurations here
# networks:
#   default:
#     driver: bridge
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • version: Defines the version of Docker Compose file format. 3.8 is a common choice.
  • services: Lists all the containers you want to run.
    • my-service: The name of the service. You can use any name here.
    • image: Specifies the Docker image to use.
    • container_name: Assigns a name to the container.
    • ports: Maps container ports to host ports.
    • volumes: Mounts directories or files from the host to the container.
    • environment: (Optional) Defines environment variables inside the container.
    • command: (Optional) Overrides the default command specified in the Docker image.

To use this docker-compose.yml file, save it in your project directory and run:

docker-compose up
Enter fullscreen mode Exit fullscreen mode

This command will start the container based on the configuration in the docker-compose.yml file.

Image description

[Source - Mayank Ahuja ]

Let's understand the πƒπ¨πœπ€πžπ« π–π¨π«π€πŸπ₯𝐨𝐰 -

[1.] Develop
β—Ύ Write your application code.

[2.] Dockerfile
β—Ύ Create a Dockerfile that defines the environment and dependencies for your application.

[3.] Build Image
β—Ύ Use docker build to create a Docker image from your Dockerfile.

[4.] Run Container
β—Ύ Use docker run to launch a container from your image.
β—Ύ The container is an isolated instance of your application.

[5.] Test
β—Ύ Test your application within the container.
β—Ύ If you make changes, rebuild the image and recreate the container.

[6.] Push => This is Optional
β—Ύ Use docker push to share your image on a registry (e.g. Docker Hub).

[7.] Pull => This is Optional
β—Ύ Others can use docker pull to download your image and run your application in their own environments.

Top comments (0)