Skip to content

Instantly share code, notes, and snippets.

@n2o
Last active February 23, 2024 16:37
Show Gist options
  • Save n2o/8d99fc1d612cf4c9f2b6db29515df616 to your computer and use it in GitHub Desktop.
Save n2o/8d99fc1d612cf4c9f2b6db29515df616 to your computer and use it in GitHub Desktop.
Show IP of your docker-container

Show IP of your Docker container

tested on Arch Linux x64 4.5.4-1-ARCH

Ever needed the IP of your running docker container? I found a script somewhere on Stackoverflow, packed it into a function and created an alias for it. This function is just a shorthand to get the IP by container ID or by the container's name:

Append this to your .bash_aliases or similar:

# ~/.bash_aliases
docker-ip() {
    docker inspect --format '{{ .NetworkSettings.IPAddress }}' "$@"
}

and load it into your current session:

$ source ~/.bash_aliases

Now you have access to the new command docker-ip and you can feed it with the container name or ID.

Example

$ docker ps
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS              PORTS                    NAMES
4f4622efdeb8        dockercomposephpmysql_web   "php -S 0.0.0.0:8000 "   10 minutes ago      Up 10 minutes       0.0.0.0:8000->8000/tcp   dockercomposephpmysql_web_1
bc1e298a9890        mysql                       "/entrypoint.sh mysql"   10 minutes ago      Up 10 minutes       0.0.0.0:3306->3306/tcp   dockercomposephpmysql_db_1

$ docker-ip 4f4622efdeb8
172.17.0.3
$ docker-ip dockercomposephpmysql_web_1
172.17.0.3

Now open your browser at http://172.17.0.3:8000.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment