Docker commands with example

Use these Docker commands with an example for your reference.

Container – Docker Containers are what docker is built on. They encapsulate an application and all of its libraries and dependencies, so it can be run anywhere Docker is installed.

Image – A Docker Image is a file that is essentially a snapshot of a container. You can create a container by running a Docker Image.

Layer – a set of read-only files to provision the system. Think of a layer as a read only snapshot of the filesystem.

Registry / Hub is the central place where all publicly published images live. You can search it, upload your images there and when you pull a docker image, it comes the repository/hub.  You can also create private docker registry in your own cloud.

Docker machine is a VM within which you can run Docker containers. On Linux you can run docker containers natively, but on OSX and Windows you need a layer of abstraction. A docker machine will spin a very lightweight virtual machine that integrates with the docker command line utilities really well.

Docker compose is a utility to run multiple containers as a system of containers. It will take care of making them aware of each other and ensure they’re properly connected to each other. This means you can run your application in one container and your database in a different container, and your analytics application in a different container, and so on. This is the ultimate isolation and it means that your applications are independent and are run in development in a very similar way to how the system might work in production.

docker commands with example


Docker Cheat Sheet examples

Create and start container, run command

docker run -it --name <container_name> <image> -d

Start, stop and restart the container

docker [start|stop|restart] <container_name> or <container-id>

Use docker attach to attach to a running container using the container’s ID or name, either to view its ongoing output or to control it interactively.

docker attach <container_name>

Kill all running containers

docker kill $(docker ps -q)

Delete dangling images the ones that are not tagged properly and are hanging around.

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

Remove all stopped containers, this will actually try to remove all the containers, but will fail to do so with the running ones, so only stopped containers will be gone after that.

docker rm $(docker ps -a -q)

Interacting with a container

Run a command in the container

docker exec -it <container_name>

Show the container logs (use -f option)

docker logs <option> <container_name>

Create an image from a running container

docker commit -m "commit message" -a "author" <container_name> username/image_name:tag

Copy files from the container to the host

docker cp <container_name>:/home/foo.txt <path>

Copy files to the container from host.

docker cp foo.txt <container_name>:/home

or if you want copy all large files, use below command

tar -cv * | docker exec -i <container_name> tar x -C <destination-folder-inside-container>

Mount the directory in host to a Container.

docker run -v /opt/test:/home/test <image> -d

Docker port mapping example
80 – host_port
8080 – container_port

docker run -d -p 80:8080 --name test_container <image>

Docker port mapping to specific IP

docker run -d -p --name test_container <image>

Set an environment variable in docker container

docker run -d -p -e --name test_container <image>

List the running containers. With -a option, it shows running and stopped Containers.

docker ps

Show container information like IP address.

docker inspect <container_name>

See the top process in container

docker top <container_name>

Docker Images command line

Create an image with Dockerfile.

echo -e "FROM centos:7\nRUN yum install -y openssh-server\nRUN systemctl enable sshd.service\nCMD ["/bin/bash"]" > Dockerfile
docker build -t <image> .

Login to the docker image

docker run -it <image> bash

Login to the docker container

docker exec -it <container_name> bash

Pull docker images (example is for the default centos image, you can specify the custom image name)

docker pull centos

Push docker images

Login docker hub ( docker login )

docker push gopkris2000/example

Delete a docker image

docker rmi gopkris2000/example

List all the images ( use -a to list all )

docker images

To show docker image information

docker inspect gopkris2000/example

To show command history of an image

docker history gopkris2000/example

Remove all untagged /none images

docker rmi $(docker images -a|awk '/none/ { print $3 }')



  • Another useful command to know any troubleshooting issue in the docker server it’s the events with flag since or filter: e.g.

    Gets events last ten minutes:
    $ docker events –since ’10m’

    Gets events stopped containers
    $ docker events –filter ‘event=stop’