Docker - DrAlzahraniProjects/csusb_fall2024_cse6550_team4 GitHub Wiki
- OS Requirements: Docker supports macOS, Linux, and Windows.
-
System Requirements:
- Minimum 4 GB RAM
- CPU: 64-bit processor
- Download Docker Desktop from Docker Hub.
- Open the
.dmg
file and drag the Docker icon to the Applications folder. - Launch Docker from Applications.
Verify Installation:
docker --version
Linux: Update your package index:
sudo apt-get update
Install Docker using APT:
sudo apt-get install docker-ce docker-ce-cli containerd.io
This installs Docker Engine, Docker CLI (command-line interface), and containerd, the service that manages containers.
Verify Installation:
docker --version
Windows: Download Docker Desktop for Windows from Docker Hub.
Follow the on-screen instructions during installation.
Once installed, check that Docker is functioning correctly by running:
Verify Installation:
docker --version
macOS and Windows:
Docker Desktop allows you to configure memory and CPU limits from the Preferences/Settings section.
Go to Preferences/Settings > Resources to allocate memory, CPU, and disk space as needed.
Linux:
Configure Docker daemon by editing the /etc/docker/daemon.json file. Example:
{
"log-level": "info",
"storage-driver": "overlay2",
"data-root": "/mnt/docker-data"
}
Restart Docker:
sudo systemctl restart docker
Network Configuration: By default, Docker uses a bridge network. You can also configure a custom network. Create a custom network:
docker network create my_network
Use the custom network in containers:
docker run --network my_network my_container
Creating a Dockerfile The Dockerfile defines how your application runs inside a container. Here's an example for a basic Python application:
# Use an official Python runtime as a base image
FROM python:3.9-slim
# Set the working directory
WORKDIR /app
# Copy the current directory contents into the container
COPY . .
# Install the dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Make port 80 available to the outside world
EXPOSE 5004
# Run the application
CMD ["python", "app.py"]
Key Elements in the Dockerfile:
FROM python:3.9-slim: This specifies the base image for the container, which in this case is Python 3.9.
WORKDIR /app: Sets the working directory inside the container to /app.
COPY . .: Copies all the files from the current directory on your computer to the container’s /app directory.
RUN pip install --no-cache-dir -r requirements.txt: Installs the required dependencies as listed in requirements.txt.
EXPOSE 5004: Tells Docker that this container will listen on port 5004.
CMD ["python", "app.py"]: Specifies the command to run when the container starts, which is to run app.py using Python.
Building and Running Containers Build the image:
docker build -t team4-app .
This command builds a Docker image from the Dockerfile and tags it as team4-app.
And if the image successfully built without any errors , it can be seen in images in docker desktop
Run the container:
docker run -d -p 5004:5004 team4-app
-d runs the container in detached mode (in the background).
-p 5004:5004 maps port 5004 of your local machine to port 5004 inside the container,
Can confirm the docker container running in docker app
making the application accessible via http://localhost:5004.
Docker Compose for Multi-Container Applications:
Docker Compose allows you to run multi-container applications using a single configuration file. For example, if you have a web server and a Redis cache, you can manage both with Docker Compose.
Sample docker-compose.yml:
version: '3'
services:
web:
build: .
ports:
- "5004:5004"
volumes:
- .:/code
depends_on:
- redis
redis:
image: "redis:alpine"
Run it with:
docker-compose up
Basic Commands: List running containers:
docker ps
Stop a container:
docker stop <container_id>
Remove a container:
docker rm <container_id>
List images:
docker images
Remove an image:
docker rmi <image_id>
Accessing a Running Container To access a shell inside a running container:
docker exec -it <container_id> /bin/bash
This opens an interactive terminal session inside the container, allowing you to run commands and explore the environment.
View logs of a running container:
docker logs <container_id>
Logs can also seen in docker app
Common Issues: Docker Daemon Not Starting:
Check if Docker is running:
sudo systemctl status docker
Start the Docker service:
sudo systemctl start docker
Permission Issues:
If you encounter permission issues when running Docker without sudo, add your user to the docker group:
sudo usermod -aG docker $USER
Restart your system after running the above command. Container Not Starting:
Check the logs for error messages:
docker logs <container_id>
Inspect the container for issues:
docker inspect <container_id>
Or one can check in docker desktop as well Network Issues:
If containers are unable to communicate, ensure they are on the same network:
docker network ls
docker network inspect <network_name>
Image Build Failing:
Verify the Dockerfile for syntax errors and ensure all dependencies are correctly specified. Build the image with verbose logging for more details:
docker build --progress=plain -t <image_name> .