Skip to content

Instantly share code, notes, and snippets.

@savishy
Last active June 13, 2017 10:59
Show Gist options
  • Save savishy/6e4b0bad63363931acbffe799cd248c7 to your computer and use it in GitHub Desktop.
Save savishy/6e4b0bad63363931acbffe799cd248c7 to your computer and use it in GitHub Desktop.
docker commands

Docker Images

list all docker dangling images

Dangling images are "no name, no tag" images <none>:<none> created during image building. These images are bad because they consume disk space. Reference.

docker images -f "dangling=true"

To list only image IDs:

docker images -f "dangling=true" -q

To remove these dangling images:

docker rmi $(docker images -f "dangling=true" -q)

Docker Containers

To list containers and status

docker ps -a

To list containers but only print container IDs

docker ps -aq

To remove all containers:

docker ps -aq | xargs docker rm

To restart a stopped (exited) container: ( reference )

docker start <container name or id> # supports tab completion
docker attach <container name or id>

To remove all images without a tag

docker images | grep "^<none>" | awk '{print $3}' | xargs docker rmi

To get docker container IP address

NOTE: Below format is for recent Docker versions (1.12+) (reference)[http://stackoverflow.com/a/20686101/682912]

docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' CONTAINER_NAME

Get all Docker containers (running or stopped) by name

(reference)[https://docs.docker.com/engine/reference/commandline/ps/]

docker ps -aq -f "name=container_name"

List all Docker containers run from a specific parent Docker Image (reference)[https://docs.docker.com/engine/reference/commandline/ps/]

docker ps -a -f "ancestor=docker-image"

top-like output listing containers and their memory consumption

docker stats

set the max memory allocated to a container

docker run ... -m 2048m

docker update -m 2048m --memory-swap 4096m docker-ssh-container

Docker Registry

Suppose I have a Private Docker Registry created at registry.somecompany.com:5000.

To push imagename Docker Image to it, I would tag the image as registry.somecompany.com:5000/imagename, then execute docker push.

This might fail with:

vagrant@ubuntu-14:~$ docker push registry.somecompany.com:5000/imagename
The push refers to a repository [registry.somecompany.com:5000/imagename]
Get https://registry.somecompany.com:5000/v1/_ping: http: server gave HTTP response to HTTPS client

Solution

The Docker daemon settings need to have the registry configuration set properly. For example, cat /etc/default/docker.

# Use DOCKER_OPTS to modify the daemon startup options.
DOCKER_OPTS="-H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock"
DOCKER_OPTS="$DOCKER_OPTS --insecure-registry=registry.company.com:5000"

However, in that case Docker might expect all images to be present in your registry, and pulling images might fail.

In such a case you might want to configure your Docker Registry as a Mirror. See this link.

Docker Registry: List all Repositories

You can use a Docker Registry Frontend UI, or use:

vvenu@ngvmpldevopsci1:~$ curl -X GET http://localhost:5000/v2/_catalog
{"repositories":["supplierapi"]}

Docker Networking: Make Docker container use host machine's /etc/hosts

Scenario:

  • I had an Ubuntu machine with a long, static list of host entries in /etc/hosts.
  • A Dockerized Tomcat application was running on this Ubuntu machine.

Problem

The Tomcat application tries to access an Oracle Database server by its hostname -- but this fails because of name resolution.

Caused by: java.net.UnknownHostException: longora02uatscan.hosting.travel.lcl

However accessing the bare IP address from within the Docker container works -- so clearly the host itself is accessible.

Cause

Since the problem was in resolving longora... to its IP address, this is a problem with name resolution. We need some way to make Docker Container use the host network.

Solution

reference.

While running the Docker Container, add --net=host.

docker run -d -p 8080:8080 --net=host bangvmpldevopsci1.sapient.com:5000/supplierapi:23

Gotcha

When you run containers with net=host, they are part of the host network, so you need to access containerized applications as http://localhost:[port].

https://stackoverflow.com/a/39549309/682912

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