A comprehensive collection of Docker commands, networking concepts, remote API configuration, and TLS security setup. This repository serves as a quick reference for beginners as well as experienced DevOps engineers who want to learn and work with Docker efficiently.
# Check Docker version
docker --version
# Check system-wide Docker information
docker info# Search for an image
docker search <image_name>
# Pull an image from Docker Hub
docker pull <image_name>
# List all images
docker images
# Remove an image
docker rmi <image_id># Run a container
docker run <image_name>
# Run a container in detached mode
docker run -d <image_name>
# Run a container with an interactive shell
docker run -it <image_name> /bin/bash
# List running containers
docker ps
# List all containers (including stopped ones)
docker ps -a
# Stop a running container
docker stop <container_id>
# Start a stopped container
docker start <container_id>
# Restart a container
docker restart <container_id>
# Remove a container
docker rm <container_id># View logs of a container
docker logs <container_id>
# Show running processes in a container
docker top <container_id>
# Display resource usage statistics
docker stats# Create a volume
docker volume create <volume_name>
# List all volumes
docker volume ls
# Inspect a volume
docker volume inspect <volume_name>
# Remove a volume
docker volume rm <volume_name># List available networks
docker network ls
# Create a network
docker network create <network_name>
# Connect a container to a network
docker network connect <network_name> <container_id>
# Disconnect a container from a network
docker network disconnect <network_name> <container_id>
# Inspect a network
docker network inspect <network_name># Start services defined in docker-compose.yml
docker-compose up -d
# Stop services
docker-compose down
# Restart services
docker-compose restart
# Check the logs
docker-compose logs
# View running containers
docker-compose ps# Copy from host to container
docker cp <file_path> <container_id>:<destination_path>
# Copy from container to host
docker cp <container_id>:<file_path> <destination_path># Build an image from a Dockerfile
docker build -t <image_name> .
# Build an image with a specific tag
docker build -t <image_name>:<tag> .# Tag an image
docker tag <image_id> <repository>:<tag>
# Push an image to Docker Hub
docker push <repository>:<tag># Limit CPU usage
docker run --cpus="1.5" <image_name>
# Limit memory usage
docker run -m 512m <image_name># Scan an image for vulnerabilities
docker scan <image_name>
# Run a container with read-only filesystem
docker run --read-only <image_name># Initialize a swarm
docker swarm init
# Add a worker node
docker swarm join --token <token> <manager_ip>:2377
# List swarm nodes
docker node ls
# Deploy a service
docker service create --name <service_name> <image_name>
# List services
docker service ls# Remove all stopped containers
docker container prune
# Remove all unused images
docker image prune -a
# Remove all unused volumes
docker volume prune
# Remove all unused networks
docker network prune1. Sudo docker run -itd –rm –name thor busybox
2. Sudo docker run -itd –rm –name mjolnir busybox
3. Sudo docker run -itd –rm –name stromebreaker nginx
4. Bridge link
5. Docker inspect bridge
6. Docker exec -it thor sh
(ping each other and internet also in bridge)
7. Sudo docker run -itd –rm -p 80:80 –name stromebreaker nginx
(redeploy for accessing the data on host)Why? Isolation and Docker recommends user-defined bridges.
8. Sudo docker network create asgard
9. Ip address show
10. Network ls
11. Sudo docker run -itd –rm –network asgard –name loki busybox
12. Sudo docker run -itd –rm –network asgard –name odin busyboxBy default: You don't need to expose ports manually.
13. Sudo docker stop stromebreaker
14. Sudo docker run -itd –rm –name –network host stromebreaker nginx
(move to near to host)
15. sudo systemctl restart dockerContainer directly connects to the physical network (home/office network).
-o parent=enp6s18 ties the Macvlan network to the host network interface.
16. Sudo docker stop thor mjolnir
17. Sudo docker run -itd –rm –network newasgard \
--ip 192.168.0.61 \
--name thor busyboxWhen you do this then you need to enable promiscuous mode.
Your network may not allow multiple MAC addresses on one switch port.
18. Sudo ip link set enp6s18 promisc onAlso enable it from VirtualBox network settings.
19. Reboot systemSudo docker network create -d ipvlan \
--subnet 192.168.0.0/16 \
--gateway 192.168.0.1 \
-o parent=enp6s18 newasgard
Docker run -itd –rm \
--network newasgard \
--ip 192.168.0.61 \
--name thor busybox(Uses the same MAC address as the host.)
Pending (Update Soon Regarding This...)
Loopback Interface
vi /lib/systemd/system/docker.serviceFind the line starting with ExecStart and add -H=tcp://0.0.0.0:2375.
ExecStart=/usr/bin/dockerd -H=fd:// -H=tcp://0.0.0.0:2375systemctl daemon-reloadsudo service docker restartcurl http://localhost:2375/images/jsonIf everything is configured correctly, this command should return a JSON response.
Use the Docker Host PC name or IP address from another machine.
mkdir -p /etc/docker/certs
cd /etc/docker/certsopenssl genrsa -aes256 -out ca-key.pem 4096Note: This command creates a private key for your Certificate Authority (CA) with 4096 bits and encrypts it with AES-256.
openssl req -new -x509 -days 365 -key ca-key.pem -sha256 -out ca.pemNote: You'll be prompted to enter details like country, state, organization, etc. This creates a self-signed certificate valid for 365 days.
openssl genrsa -out server-key.pem 4096Note: This creates a private key for the Docker server with 4096 bits.
openssl req -new -key server-key.pem -out server.csrNote: You'll be prompted for details. This CSR will be signed by your CA to create the server certificate.
touch extfile.cnfPaste:
subjectAltName = IP:192.168.0.90
extendedKeyUsage = serverAuth
openssl x509 -req -days 365 -sha256 \
-in server.csr \
-CA ca.pem \
-CAkey ca-key.pem \
-CAcreateserial \
-out server-cert.pem \
-extfile extfile.cnfNote: This command creates a server certificate signed by your CA, valid for 365 days.
chmod -v 0400 ca-key.pem server-key.pem
chmod -v 0444 ca.pem server-cert.pemNote: These commands set appropriate permissions on your key and certificate files.
mkdir -p /etc/docker
cp server-cert.pem /etc/docker/
cp server-key.pem /etc/docker/
cp ca.pem /etc/docker/sudo nano /lib/systemd/system/docker.serviceModify the ExecStart line:
ExecStart=/usr/bin/dockerd \
--tlsverify \
--tlscacert=/etc/docker/ca.pem \
--tlscert=/etc/docker/server-cert.pem \
--tlskey=/etc/docker/server-key.pem \
-H=0.0.0.0:2376Note: Replace 0.0.0.0 with your server IP (192.168.0.90) if needed.
sudo systemctl daemon-reload
sudo systemctl restart dockerThis reloads the Docker service configuration and restarts Docker to apply the new settings.
scp user@192.168.0.90:/etc/docker/ca.pem ~/openssl genrsa -out client-key.pem 4096
openssl req -new -key client-key.pem -out client.csrNote: The first command creates a private key for the client, and the second generates a CSR.
mkdir extfile-client.cnfopenssl x509 -req -days 365 -sha256 \
-in client.csr \
-CA ca.pem \
-CAkey /etc/docker/certs/ca-key.pem \
-CAcreateserial \
-out client-cert.pem \
-extfile extfile-client.cnfThis creates a client certificate signed by your CA.
Run Docker commands from the client machine:
docker --tlsverify \
--tlscacert=ca.pem \
--tlscert=client-cert.pem \
--tlskey=client-key.pem \
-H=192.168.0.90:2376 info✅ Expected Result: If everything is configured correctly, the client should successfully communicate with the Docker daemon over TLS.