101

I've just installed ubuntu docker image, when I execute "ifconfig" it says there's no such command, I tried apt-get install by there's no package named "ifconfig"(I can install some other images).

So how to do this? Thanks.

Hind Forsum
  • 9,717
  • 13
  • 63
  • 119
  • 4
    http://serverfault.com/a/614972 - within your Dockerfile `RUN apt-get install -y net-tools` – Brian Mar 18 '17 at 03:16
  • 1
    iproute2 is the Linux networking toolkit that replaced net-tools (ifconfig, route, arp etc.) see https://baturin.org/docs/iproute2/ and also see https://lwn.net/Articles/710533/ – Scott Stensland May 15 '18 at 18:18
  • [Can I ask questions about installation in SO?](https://meta.stackoverflow.com/questions/338203/can-i-ask-questions-about-installation-in-so) Questions about software tools commonly used by programmers must be **a practical, answerable problem unique to software development.** – Trenton McKinney Oct 13 '19 at 20:34

10 Answers10

207

On a fresh ubuntu docker image, run

apt-get update
apt-get install net-tools

These can be executed by logging into the docker container or add this to your dockerfile to build an image with the same.

rinogo
  • 8,491
  • 12
  • 61
  • 102
vishnu narayanan
  • 3,813
  • 2
  • 24
  • 28
  • 8
    net-tools is now obsolete ... its replacement : iproute2 is the Linux networking toolkit that replaced net-tools (ifconfig, route, arp etc.) see https://baturin.org/docs/iproute2/ and also see https://lwn.net/Articles/710533/ – Scott Stensland May 15 '18 at 18:18
  • can not do this because apt is not installed on a container. – eddy147 Oct 17 '21 at 06:58
  • @ScottStensland `iproute2` didn't work for me on a Debian-based Docker image (MySQL 5.7). `net-tools` did. ‍♂️ – rinogo Oct 23 '21 at 15:42
  • you don't need install commands in the image. just install it on outer OS, and run it in container's network namespace: nsenter -n -t $(docker inspect -f "{{.State.Pid}}" b2154c66061e) ; then run cmd you need , command 'exit' to return. – tinyhare Nov 23 '22 at 18:35
13

You could also consider:

RUN apt-get update && apt-get install -y iputils-ping

(as Contango comments: you must first run apt-get update, to avoid error with missing repository).

See "Replacing ifconfig with ip"

it is most often recommended to move forward with the command that has replaced ifconfig. That command is ip, and it does a great job of stepping in for the out-of-date ifconfig.

But as seen in "Getting a Docker container's IP address from the host", using docker inspect can be more useful depending on your use case.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
3

I came here because I was trying to use ifconfig on the container to find its IPAaddress and there was no ifconfig. If you really need ifconfig on the container go with @vishnu-narayanan answer above, however you may be able to get the information you need by using docker inspect on the host:

docker inspect <containerid>

There is lots of good stuff in the output including IPAddress of container:

"Networks": {
    "bridge": {
        "IPAMConfig": null,
        "Links": null,
        "Aliases": null,
        "NetworkID": "12345FAKEID",
        "EndpointID": "12345FAKEENDPOINTID",
        "Gateway": "172.17.0.1",
        "IPAddress": "172.17.0.3",
        "IPPrefixLen": 16,
        "IPv6Gateway": "",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "MacAddress": "01:02:03:04:05:06",
        "DriverOpts": null
    }
}
Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44
mancocapac
  • 812
  • 6
  • 23
2

Please use the below command to get the IP address of the running container.

$ ip addr

Example-:

root@4c712d05922b:/# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
247: eth0@if248: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:ac:11:00:06 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.6/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:6/64 scope link
       valid_lft forever preferred_lft forever
2

From within a Dockerfile something like the following should do the trick:

RUN apt-get update && \
     apt-get install -y net-tools

From memory it's best practice to combine the update and the package installation lines to prevent docker caching the update step which can result in out-dated packages being installed.

Installing it via the CLI or a shell script:

apt-get update && apt-get install net-tools

Brian
  • 2,822
  • 1
  • 16
  • 19
2

write

sudo apt-get install net-tools

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
1

In case you want to use the Docker image as a "regular" Ubuntu installation, you can also run unminimize. This will install a lot more than ifconfig, so this might not be what you want.

László van den Hoek
  • 3,955
  • 1
  • 23
  • 28
1

If Ubuntu Docker image isn't recognizing 'ifconfig' inside of GNS3, you'll need to open Ubuntu docker image on your host.

Assuming you already have docker on your host pc and ubuntu pull'd from docker images. Enter these commands in your host OS (Linux, CentOS, etc.) CLI.

$docker images

$docker run -it ubuntu

$apt-get update

$apt-get install net-tools

(side note: you can add whatever other tools and services that you would like to add now, but for now this is just to get ifconfig to work.)

$exit

Now you will commit these changes to Docker. This link for committing changes is the best summary and works (skip to Step 4):

https://phoenixnap.com/kb/how-to-commit-changes-to-docker-image#htoc-step-3-modify-the-container

When you re-open the docker image in GNS3 you should now have the ifconfig command usable and whatever other tools or services you added to the container.

Enjoy!

Sandeep Kumar
  • 2,397
  • 5
  • 30
  • 37
Insaini
  • 11
  • 1
1

This might have been sufficiently solved for some people but none of the answers worked right away for me I was trying to build an ubuntu image so this is the dockerfile i used that worked and isntalled ifcondig with the net-tools:

FROM ubuntu:latest 
RUN apt-get update && apt-get -qq -y install curl

RUN apt-get update && apt-get install -y apt-utils
RUN apt-get update && apt-get install net-tools

COPY . /app
WORKDIR /app
KZiovas
  • 3,491
  • 3
  • 26
  • 47
-1

sudo apt-get install iproute2 then run ip addr show

it works..

  • 2
    Hi, welcome to Stack Overflow. As this is an old question with an extremely popular accepted answer, please edit your answer to explain what it provides that the existing answers do not. Thanks. – MandyShaw Mar 31 '19 at 17:34