Skip to content

Instantly share code, notes, and snippets.

@boseji
Last active June 20, 2017 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boseji/63422574fae24fa0e12d13fb95e8a8d8 to your computer and use it in GitHub Desktop.
Save boseji/63422574fae24fa0e12d13fb95e8a8d8 to your computer and use it in GitHub Desktop.
Snippets about running things with docker

Docker Tips & Tricks

How to Check if Docker is Working ?

docker run hello-world

This would print out


Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

That means you are good to go shipping Containers !

Check Running Containers

docker ps -a

The -a flag here helps to make sure that even non-working Containers are shown.

Some times the list is too broad and it becomes a little like

CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS
     PORTS               NAMES
58b381f52ae7        hello-world         "/hello"            About a minute ago   Exited (0) About a minute a
go                       inspiring_newton
16330d600aa6        hello-world         "/hello"            2 minutes ago        Exited (0) 2 minutes ago
                         elated_dubinsky

Then you need to use

docker ps -aq

Here the aq flag helps to get all the Container IDs only.

This can be really useful as we see in the next section for terminating containers.

How to Stop / Kill Containers

In general Containers can operate with 3 diffrent modes:

  • Daemon - Like in the background
  • Interactive - Like execuring commands / Shells etc
  • Exited - They have completed the task/command ( In this state some containers can be restarted for use )
docker ps -a

This command would give the full detail of the Containers , their images and Status.

The Status is what we just talked above.

Ideally any container first needs to be STOPPED and then TERMINATED or kill.

But the containers which have already EXITED they can directly be TERMINATED or kill.

Stopping running Containers

The command to STOP the container

$ docker stop 5eb8fb91c7f0
5eb8fb91c7f0

Here the big number 5eb8fb91c7f0 is actually the Container ID

One can also terminate all the working containers in one go by this command:

$ docker ps -aq | xargs docker stop
5eb8fb91c7f0
58b381f52ae7
16330d600aa6

In this case 3 containers got stopped.

In case the container is still not Stopping then use the command

$docker kill 5eb8fb91c7f0

Terminating Containers

Finally after we have ensured that all conainers are stopped, we can atempt to terminate them.

$ docker rm 5eb8fb91c7f0
5eb8fb91c7f0

This remove one particuar container by its ID.

For all containers:

$ docker ps -aq | xargs docker rm
58b381f52ae7
16330d600aa6

Here two containers got terminated.

In case the containers did not get terminated one can use the following:

$docker rm -fv 16330d600aa6

The flag -f forces the close the container.

Also the flag -v make sure that in case any volumes are connected, they are duly removed.

Stop Boot2Docker

Generally in Windows one uses the Docker VM called boot2docker.

This VM is hosted with Virtual Box.

An interesting thing is that this VM is created, operated and destroyed by Head Less interface of Virtual Box

That's Intimidating !

Hence its not strait forward to close the terminal on Windows and imagin that your Head Less VM has been terminated.

Use the following command after closing the Boot2Docker shell in another Windows CMD terminal prompt:

docker-machine stop

This would shutdown the Head Less VM safely.

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