{"id":1438,"date":"2024-11-09T03:57:19","date_gmt":"2024-11-09T03:57:19","guid":{"rendered":"https:\/\/kb.lagonet.vn\/?p=1438"},"modified":"2024-11-09T09:17:59","modified_gmt":"2024-11-09T09:17:59","slug":"to-get-ip-address-of-docker-container","status":"publish","type":"post","link":"https:\/\/kb.lagonet.vn\/?p=1438","title":{"rendered":"To get IP address of Docker container"},"content":{"rendered":"\n<p>To get the IP address of a Docker container running Nginx (or any other application) from within the container, you might encounter issues if some networking tools, like <code>ip<\/code>, are not available. Here are some methods to get the container\u2019s IP address, depending on what is available and your setup.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Method 1: Install <code>iproute2<\/code> (if not available in the container)<\/h3>\n\n\n\n<p>If you can\u2019t run <code>ip<\/code> inside the container because it\u2019s not installed, you can install the <code>iproute2<\/code> package, which contains the <code>ip<\/code> command.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Attach to your Nginx container with an interactive shell:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   docker exec -it &lt;container_id_or_name&gt; \/bin\/sh<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>Install <code>iproute2<\/code>. This step varies depending on the base image (e.g., Alpine, Debian, or Ubuntu). For Alpine Linux (commonly used for lightweight Nginx images):<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   apk add iproute2<\/code><\/pre>\n\n\n\n<p>For Debian or Ubuntu-based images:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   apt update\n   apt install -y iproute2<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li>Now you can use <code>ip -o -4 addr<\/code> to get the IPv4 address:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   ip -o -4 addr<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Method 2: Use <code>hostname -I<\/code> (if available)<\/h3>\n\n\n\n<p>Some containers may have the <code>hostname<\/code> command, which can directly give you the IP address:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>hostname -I<\/code><\/pre>\n\n\n\n<p>This command outputs the IP addresses associated with the container\u2019s network interfaces.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Method 3: Use Docker\u2019s Inspect Command from the Host<\/h3>\n\n\n\n<p>If you only need the IP address from the host system (outside the container), you can use <code>docker inspect<\/code>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Run the following command from the host to get the container\u2019s IP address:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' &lt;container_id_or_name&gt;<\/code><\/pre>\n\n\n\n<p>This command fetches the IP address from the container\u2019s network settings.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my_nginx_container<\/code><\/pre>\n\n\n\n<p>This command is particularly useful if you\u2019re accessing the container\u2019s IP address for configuration or diagnostic purposes from outside the container.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To get the IP address of a Docker container running Nginx (or any other application) from within the container, you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[141,142],"tags":[],"class_list":["post-1438","post","type-post","status-publish","format-standard","hentry","category-devops","category-docker"],"_links":{"self":[{"href":"https:\/\/kb.lagonet.vn\/index.php?rest_route=\/wp\/v2\/posts\/1438","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kb.lagonet.vn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kb.lagonet.vn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kb.lagonet.vn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kb.lagonet.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1438"}],"version-history":[{"count":1,"href":"https:\/\/kb.lagonet.vn\/index.php?rest_route=\/wp\/v2\/posts\/1438\/revisions"}],"predecessor-version":[{"id":1439,"href":"https:\/\/kb.lagonet.vn\/index.php?rest_route=\/wp\/v2\/posts\/1438\/revisions\/1439"}],"wp:attachment":[{"href":"https:\/\/kb.lagonet.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kb.lagonet.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kb.lagonet.vn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}