Docker WorkFlow
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"]
.
.
.
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 .
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
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
This command will display a list of all Docker images, along with their REPOSITORY, TAG, IMAGE ID, CREATED, and SIZE.
REPOSITORY TAG IMAGE ID CREATED SIZE my-docker-image latest d1a1f2e8f7b2 2 hours ago 450MB python 3.11 a2d3c4e5f6g7 5 days ago 800MB
.
.
.
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:
docker run -d --name my-docker-container -p 8001:8000 my-docker-image
After running this command, you can check if the container is running by using:
docker ps
This will list all the running containers along with their names, status, and port mappings.
You can now access your Django application by navigating to http://localhost:8001 in your web browser.
.
.
.
docker run -d --name my-docker-container -p 8001:8000 -v .:/app my-docker-image
The docker run command you provided is used to start a Docker container from a Docker image. Here's a breakdown of the command:
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!
.
.
.
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
To use this docker-compose.yml file, save it in your project directory and run:
docker-compose up
This command will start the container based on the configuration in the docker-compose.yml file.
[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.
The above is the detailed content of Docker Python Django initial configuration setup. For more information, please follow other related articles on the PHP Chinese website!