This is a list of the basic docker commands which I hope will help everyone that is just starting in the docker containers world.

Assuming that you have a basic knowledge of what containers are and what they do, this post can act as a quick reference to some of the commands you will be using most often.

Help and information

$ docker help <command>

​Retrieves help for the designated command

$ docker version

​Returns information about the docker client and server

Starting a container

$ docker run ubuntu /bin/echo “Hello world”
  • Creates and starts a container based on the ubuntu image
  • Runs the /bin/echo “Hello world” command
  • Exits
$ docker run –i –t ubuntu /bin/bash
  • Creates a container based on the ubuntu image
  • Starts it in interactive mode, accepting input from the STDIN
  • Attaches a pseudo terminal 
  • Run the /bin/bash shell, giving in essence terminal access to the container
$ docker run -d ubuntu top -b
  • Creates a container based on the ubuntu image
  • Starts the container in the background
  • Runs a non terminating command (i.e. a daemon, webserver, etc)
  • Exits without terminating the container
$ docker start <running container name>

​ Starts a previously stopped container. Particularly useful, when you do not want to create a new container every time you come back to where you stopped before.

Logging

$ docker logs <container name|container ID>

​ Returns the latest snapshot of the output of a running container. If you execute it in the top -b daemon created above, it will show you the latest top command output.

$ docker logs –f <running container name|running container ID>

​ Same as before but follows the log output, just like the tail -f command.

$ docker top <running container name|running container ID>

​ Lists the processes running on the container.

$ docker exec -it <running container name|running container id> <command>

Interacting with a running container

  • Attaches to the designated running container
  • Executes the command
  • Exits without terminating the container
$ docker attach <running container name>
  • Attaches to the designated running container

To exit without stopping the container you need to press <CTRL-P><CTRL-Q>

$ docker stop <running container name>

​Terminates the designated running container.

Checking your images and containers

$ docker ps

​ Lists the running containers.

$ docker ps -a

​ Lists all the containers, including the stopped ones

$ docker images

​Lists all the locally existing images

Working with repositories

$ docker pull <image name>

​Pulls the designated image from the docker hub without creating a container.

$ docker search <image name>

​Searches the docker hub for the designated image.

$ docker diff <container name|container ID>

​Outputs the differences of the container to the image it was created from 

$ docker commit <container name|container ID> <image tag>

​Commits the designated container with the given image tag.

Deleting

$ docker rm <container name|container ID>

​Deletes the designated container. The container must be a stopped one.

$ docker rmi <image ID>

​I will upload a new post, with some more involved stuff, like working with docker files, mounting volumes and networking, so stay tuned.
Thanks!