Inspecting Container Details
Docker provides powerful tools for inspecting the details of your containers. Let's explore them.
To get detailed information about a container, use the inspect
command:
docker inspect nginx-detached
This command outputs a JSON array with detailed information about the container. It can be overwhelming, so let's use a filter to get specific information.
To get the IP address of the container:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' nginx-detached
To see the current state of the container:
docker inspect -f '{{.State.Status}}' nginx-detached
These commands use Go templates to filter the output. The -f
flag allows us to specify a format template.
Let's also check the port mappings:
docker port nginx-detached
Port mapping allows containers to communicate with the host system or external networks. It maps a port on the host system to a port inside the container. This is crucial for accessing services running inside containers from outside the Docker environment.
If no ports are mapped, this command won't produce any output. In our case, we haven't explicitly mapped any ports for the nginx-detached container, so you likely won't see any output.
To demonstrate port mapping, let's create a new Nginx container with a port mapping:
docker run -d --name nginx-with-port -p 8080:80 nginx
This command maps port 8080 on the host to port 80 in the container. Now, if we check the port mappings:
docker port nginx-with-port
You should see output similar to:
80/tcp -> 0.0.0.0:8080
This means that traffic to port 8080 on your host machine will be forwarded to port 80 in the container, where Nginx is listening.