Useful Docker Commands to troubleshoot a container
Inspect container details:
Provides detailed configuration and state information about the container, including network settings, mounts, and environment variables.
docker inspect <container_name>Find where the volume is stored:
docker volume inspect <volume_name>Check logs:
Displays the log of the container.
docker logs <container_name>View real-time logs:
docker logs -f <container_name>View environment variables:
docker exec <container_name> printenvAccess into a container directly:
Use the following command to access a container's shell interactively:
docker exec -it <container name> shReplace <container_name> with the name or ID of the container
Ping to outside from container:
(Check if the container network can access to outside)
docker exec -it <container name> ping <ip address>Test if a container have open port:
To test if a specific port is open in a container, use this command:
docker exec -it <container name> netstat -tuln | grep <port number>Replace <container_name> with the name or ID of the container, port number with the port number
Command Breakdown:
netstat -tuln: list all listening TCP and UDP ports.grep <port number>: filters out the output to check if the specific<port number>is actively listening
Test if container can access to other ip:port
docker exec -it <container name> nc -zv <ip> <port>Replace <container_name> with the name or ID of the container, ip port with the ip address and port number
Command Breakdown:
nc: (Netcat) A networking utility for reading from and writing to network connections-z: Checks for open ports without sending data.-v: Provides verbose output.
Add current user to the docker group:
This is useful when you need permission to manage Docker resources, such as editing files within Docker containers or executing Docker commands without requiring sudo.
sudo usermod -aG docker $USERCommand Breakdown:
sudo: Runs the command with superuser (root) privileges, necessary for modifying user groups.usermod: A Linux utility to modify user account settings.-aG:-a(append): Ensures the user is added to the specified group without being removed from other groups.-G(groups): Specifies the group(s) the user should be added to.
docker: The group to which the user is being added. Thedockergroup allows members to execute Docker commands without needingsudo.$USER: A shell variable representing the username of the currently logged-in user.
Log out and log back in for the group membership to take effect:
newgrp docker
These commands are useful for debugging container network configurations and verifying connectivity.
Do you enjoy this blog post?