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:
- Download and install Docker Desktop from Docker’s website
- Follow the installation wizard instructions
- Start Docker Desktop application after installation
- Verify the installation by opening Command Prompt or PowerShell and running:
docker --version
For macOS:
- Download Docker Desktop for Mac from Docker’s website
- Open the downloaded .dmg file and drag Docker to your Applications folder
- Launch Docker from Applications
- Wait for Docker to start (you’ll see the Docker icon in your menu bar)
- Verify the installation by opening Terminal and running:
docker --version
For Ubuntu/Linux:
- Update packages and install prerequisites:
sudo apt update sudo apt install apt-transport-https ca-certificates curl software-properties-common
- 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
- 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
- Install Docker Engine:
sudo apt update sudo apt install docker-ce docker-ce-cli containerd.io
- 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)
- Verify installation:
docker run hello-world
2. Project Configuration
- Create a project directory and navigate to it
- Create a
Dockerfile
defining your containerized environment - Add a
.dockerignore
file to exclude unnecessary files (similar to .gitignore) - Set up environment variables:
- Windows: Create
.env
file or useset VARIABLE=value
in command line - macOS/Linux: Create
.env
file or useexport VARIABLE=value
in terminal
- Windows: Create
3. Building Docker Images
- Basic build command (same across all platforms):
docker build -t project_name:tag .
- 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 .
- Windows:
- Force a clean build (no cache):
docker build --no-cache -t project_name:tag .
- Verify image creation:
docker images
4. Running Containers
- Basic run command:
docker run -d -p host_port:container_port --name container_name project_name:tag
- 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
- Windows:
- Run in interactive mode:
docker run -it -p host_port:container_port --name container_name project_name:tag
- 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
- View all running containers:
docker ps
- View all containers (including stopped):
docker ps -a
- Stop a container:
docker stop container_name
- Start a stopped container:
docker start container_name
- Remove a container:
docker rm container_name
(Container must be stopped first, or use
docker rm -f container_name
to force removal) - View container logs:
docker logs container_name
- Execute commands in a running container:
docker exec -it container_name command
Example:
docker exec -it container_name bash
6. Managing Images
- List all images:
docker images
- Remove an image:
docker rmi image_name:tag
- Remove all unused images:
docker image prune -a
7. Docker Compose (Multi-Container Applications)
- 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
- Linux:
Create a
docker-compose.yml
file defining services, networks, and volumes- Start all services:
docker-compose up -d
- Stop all services:
docker-compose down
- View logs for all services:
docker-compose logs
8. System Cleanup
- Remove all stopped containers:
docker container prune
- Remove all unused images:
docker image prune
- Remove all unused volumes:
docker volume prune
- Remove all build cache:
docker builder prune
- Full system cleanup (use with caution):
docker system prune -a
9. Common Issues and Troubleshooting
- Permission issues (Linux):
- Ensure your user is in the Docker group
- Alternatively, prefix commands with
sudo
- 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
- Windows:
- Change the host port mapping in your
- Container crashes/exits immediately:
- Check logs:
docker logs container_name
- Run in interactive mode for debugging:
docker run -it image_name bash
- Check logs:
- Insufficient resources:
- Adjust resource limits in Docker Desktop settings (Windows/Mac)
- For Linux, check system resources and consider limiting container resources
- Network issues:
- Create a custom bridge network:
docker network create my_network
- Run containers on the same network for easier communication
- Create a custom bridge network:
10. Best Practices
- Use specific image tags instead of
latest
- Keep images small by using multi-stage builds and lightweight base images
- Use
.dockerignore
to exclude unnecessary files - Don’t run containers as root when possible
- Store configuration in environment variables
- Use volumes for persistent data
- Regularly update base images for security patches
- Document your Docker setup in project README