Useful Docker Commands and Configurations
This document is a collection of useful commands and configuration snippets for managing Docker and Docker Compose.
Docker Daemon Configuration
You can customize the Docker daemon’s behavior by editing its configuration file, typically located at /etc/docker/daemon.json on Linux.
Example: Set Default IP Address Pools
This helps prevent IP conflicts with other networks in your environment.
-
Create or edit the configuration file:
sudo mkdir -p /etc/docker sudo nano /etc/docker/daemon.json -
Add the desired configuration. After saving, you must restart the Docker daemon (
sudo systemctl restart docker).{ "default-address-pools": [ { "base": "10.10.0.0/16", "size": 24 } ] }
Container Management
Running Containers with Environment Variables
You can pass environment variables to your container in two main ways:
# Pass variables from a .env file
docker run --rm -it --env-file .env your-image /bin/bash
# Pass a single environment variable
docker run --rm -it -e GITHUB_TOKEN=<GITHUB_TOKEN> your-image /bin/bashListing Container Ports
Quickly see which ports are mapped for all containers (running and stopped).
docker ps -a --format "table {{.Names}}\t{{.Ports}}"Keeping a Container Alive for Debugging
If a container’s main process finishes, the container stops. To keep it running for debugging purposes, you can use a command that doesn’t exit.
Add this to your Dockerfile:
# This command will keep the container running indefinitely
ENTRYPOINT ["tail", "-f", "/dev/null"]Checking Container Logs
Follow the logs of a running container.
docker logs -f <container_name_or_id>For HTTP scanner, proxy, or API-client testing, see mendhak/http-https-echo. It runs a simple HTTP/HTTPS echo server and prints incoming request details in the container logs.
Stopping All Running Containers
A quick way to stop all active containers.
docker ps -aq | xargs docker stopNetwork Management
Creating a Custom Docker Network
Creating custom bridge networks is a best practice for enabling communication between containers while providing better isolation.
sudo docker network create \
--driver=bridge \
--subnet=172.28.0.0/24 \
--ip-range=172.28.0.0/24 \
--gateway=172.28.0.254 \
my_custom_networkDocker Compose Snippets
Using an External Network
If you have a pre-existing Docker network, you can connect a Docker Compose service to it.
---
version: "3.8"
services:
app:
image: exampleuser/tor-proxy:latest
container_name: tor-proxy
restart: unless-stopped
networks:
- wan_net
# ...
networks:
wan_net:
external:
name: WAN # This must be the name of an existing Docker networkManaging Volumes in Docker Compose
Docker Compose provides several ways to manage persistent data.
# ...
services:
my_app:
image: my_app_image
volumes:
# Bind Mount: Maps a host path to a container path
- ./html:/var/www/html
# Named Volume: Docker manages the volume
- app_data:/app/data
# Named Volume with NFS driver
- nfs_share:/mnt/nfs
volumes:
# Basic named volume
app_data:
# Named volume pointing to a specific location on the host (bind mount)
test_data:
driver: local
driver_opts:
type: 'none'
o: 'bind'
device: '/home/user/managed_network/test'
# Named volume using an NFS share
# NOTE: The device path is the path on the NFS server itself.
nfs_share:
driver: local
driver_opts:
type: "nfs"
o: "addr=10.0.0.20,rw,nolock"
device: ":/docker_containers/portainer/storage"To find available NFS shares, you can use showmount:
showmount -e 10.0.0.20System Maintenance
Prune Unused Resources
Reclaim disk space by removing stopped containers, dangling images, and unused networks and volumes.
docker system prune --all