Standard Operating Procedure for Docker Setup and Usage

This SOP provides a comprehensive guide for setting up and using Docker across different operating systems.

1. Docker Installation

For Windows:

  1. Download and install Docker Desktop from Docker’s website
  2. Follow the installation wizard instructions
  3. Start Docker Desktop application after installation
  4. Verify the installation by opening Command Prompt or PowerShell and running: docker --version

For macOS:

  1. Download Docker Desktop for Mac from Docker’s website
  2. Open the downloaded .dmg file and drag Docker to your Applications folder
  3. Launch Docker from Applications
  4. Wait for Docker to start (you’ll see the Docker icon in your menu bar)
  5. Verify the installation by opening Terminal and running: docker --version

For Ubuntu/Linux:

  1. Update packages and install prerequisites:
    sudo apt update
    sudo apt install apt-transport-https ca-certificates curl software-properties-common
    
  2. Add Docker’s official GPG key:
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    
  3. Add the Docker repository:
    echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    
  4. Install Docker Engine:
    sudo apt update
    sudo apt install docker-ce docker-ce-cli containerd.io
    
  5. Add your user to the docker group (to run without sudo):
    sudo usermod -aG docker ${USER}
    

    (Log out and back in for this to take effect)

  6. Verify installation: docker run hello-world

2. Project Configuration

  1. Create a project directory and navigate to it
  2. Create a Dockerfile defining your containerized environment
  3. Add a .dockerignore file to exclude unnecessary files (similar to .gitignore)
  4. Set up environment variables:
    • Windows: Create .env file or use set VARIABLE=value in command line
    • macOS/Linux: Create .env file or use export VARIABLE=value in terminal

3. Building Docker Images

  1. Basic build command (same across all platforms):
    docker build -t project_name:tag .
    
  2. Build with build arguments:
    • Windows:
      set ARG_NAME=value
      docker build --build-arg ARG_NAME=%ARG_NAME% -t project_name:tag .
      
    • macOS/Linux:
      export ARG_NAME=value
      docker build --build-arg ARG_NAME=$ARG_NAME -t project_name:tag .
      
  3. Force a clean build (no cache):
    docker build --no-cache -t project_name:tag .
    
  4. Verify image creation:
    docker images
    

4. Running Containers

  1. Basic run command:
    docker run -d -p host_port:container_port --name container_name project_name:tag
    
  2. Run with volume mounting for development:
    • Windows:
      docker run -d -p host_port:container_port -v %cd%:/app --name container_name project_name:tag
      
    • macOS/Linux:
      docker run -d -p host_port:container_port -v $(pwd):/app --name container_name project_name:tag
      
  3. Run in interactive mode:
    docker run -it -p host_port:container_port --name container_name project_name:tag
    
  4. Run with environment variables:
    docker run -d -p host_port:container_port -e VAR_NAME=value --name container_name project_name:tag
    

5. Managing Containers

  1. View all running containers:
    docker ps
    
  2. View all containers (including stopped):
    docker ps -a
    
  3. Stop a container:
    docker stop container_name
    
  4. Start a stopped container:
    docker start container_name
    
  5. Remove a container:
    docker rm container_name
    

    (Container must be stopped first, or use docker rm -f container_name to force removal)

  6. View container logs:
    docker logs container_name
    
  7. Execute commands in a running container:
    docker exec -it container_name command
    

    Example: docker exec -it container_name bash

6. Managing Images

  1. List all images:
    docker images
    
  2. Remove an image:
    docker rmi image_name:tag
    
  3. Remove all unused images:
    docker image prune -a
    

7. Docker Compose (Multi-Container Applications)

  1. Install Docker Compose (already included with Docker Desktop for Windows/Mac):
    • Linux:
      sudo curl -L "https://github.com/docker/compose/releases/download/v2.23.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
      sudo chmod +x /usr/local/bin/docker-compose
      
  2. Create a docker-compose.yml file defining services, networks, and volumes

  3. Start all services:
    docker-compose up -d
    
  4. Stop all services:
    docker-compose down
    
  5. View logs for all services:
    docker-compose logs
    

8. System Cleanup

  1. Remove all stopped containers:
    docker container prune
    
  2. Remove all unused images:
    docker image prune
    
  3. Remove all unused volumes:
    docker volume prune
    
  4. Remove all build cache:
    docker builder prune
    
  5. Full system cleanup (use with caution):
    docker system prune -a
    

9. Common Issues and Troubleshooting

  1. Permission issues (Linux):
    • Ensure your user is in the Docker group
    • Alternatively, prefix commands with sudo
  2. Port conflicts:
    • Change the host port mapping in your docker run command
    • Check which process is using the port:
      • Windows: netstat -ano | findstr :PORT
      • macOS/Linux: lsof -i :PORT
  3. Container crashes/exits immediately:
    • Check logs: docker logs container_name
    • Run in interactive mode for debugging: docker run -it image_name bash
  4. Insufficient resources:
    • Adjust resource limits in Docker Desktop settings (Windows/Mac)
    • For Linux, check system resources and consider limiting container resources
  5. Network issues:
    • Create a custom bridge network: docker network create my_network
    • Run containers on the same network for easier communication

10. Best Practices

  1. Use specific image tags instead of latest
  2. Keep images small by using multi-stage builds and lightweight base images
  3. Use .dockerignore to exclude unnecessary files
  4. Don’t run containers as root when possible
  5. Store configuration in environment variables
  6. Use volumes for persistent data
  7. Regularly update base images for security patches
  8. Document your Docker setup in project README