Django add new package in docker
To add a new package to a Django project in a Docker container, you'll need to follow these steps:
Step 1: Create a requirements file
In your Django project directory, create a new file named requirements.txt
. This file will contain the list of packages you want to install.
Step 2: Add the new package to the requirements file
Open the requirements.txt
file and add the new package you want to install. For example, if you want to install the django-debug-toolbar
package, add the following line:
django-debug-toolbar==2.2
Step 3: Update the Dockerfile
In your Dockerfile, you need to update the RUN
command to install the new package. You can do this by adding the following line:
RUN pip install -r requirements.txt
This command tells Docker to install all the packages listed in the requirements.txt
file.
Step 4: Rebuild the Docker image
Run the following command to rebuild the Docker image:
docker build -t <your-image-name>.
Replace <your-image-name>
with the name you want to give your Docker image.
Step 5: Run the Docker container
Run the following command to start a new Docker container from the updated image:
docker run -p 8000:8000 <your-image-name>
This command maps port 8000 on your local machine to port 8000 in the Docker container.
Step 6: Verify the package installation
Once the container is running, you can verify that the new package has been installed by running the following command:
docker exec -it <container-id> python -c "import django; print(django.get_version())"
Replace <container-id>
with the ID of the running container. This command should print the version of Django, including the new package.
That's it! You should now have the new package installed in your Django project running in a Docker container.