This tutorial is a cheat sheet for most commonly used docker commands. You can download docker from here.

Create and Run a Container from an Image

run command create and run a container from an image. If image is not found locally, then it tries to search the remote repository and pull the latest image.

~ hpatel$ docker run <image-name>
~ hpatel$ docker run hello-world

You can pass commands to execute inside the container.

# docker run <image-nmae> <command>

# echo message word
~ hpatel$ docker run busybox echo message 

# list all folders inside container
~ hpatel$ docker run busybox ls

docker run command is a combination of docker create and docker start command.
docker create creates a container by copying necessary files on the hard drive.
docker start -a command runs the container using those information. -a argument watch for the output from container and prints it on the terminal.

# docker create <image-name>

~ hpatel$ docker create hello-world  
7989abdsdas8sa8sa98saas89

# docker start -a <container-id>
~ hpatel$ docker start -a 7989abdsdas8sa8sa98saas89

List running containers

ps command list all the containers on the system.

# list all containers which are currently running
~ hpatel$ docker ps 

# list all containers created/stopped/destroyed/running on system anytime.
~ hpatel$ docker ps --all 

Removing stopped containers

Stopped containers uses the disk space on the computer. So, it is a good idea to remove the containers once it is not useful anymore.

~ hpatel$ docker system prune

Retrieve the logs from container

docker logs retrieve the logs emitted from the specific container id.

# docker logs <container-id>

~ hpatel$ docker logs 7989abdsdas8sa8sa98saas89

Stopping the container

docker stop command sends signal to the container and allows the graceful termination of primary process inside the container. If container does not stop within 10s (default time), then docker issues a kill command to the container.
docker kill commands sends a signal to the container to shutdown the primary process inside the container right away. Usually, kill command is used if stop command is not responding as expected.

# docker stop <container-id>
~ hpatel$ docker stop 7989abdsdas8sa8sa98saas89

# docker kill <container-id>
~ hpatel$ docker kill 7989abdsdas8sa8sa98saas89

Run commands inside the container

Mongodb is a NoSQL database. You can run mongodb inside docker container and perform db operations on it. If you are unfamiliar with MongoDB, then click here to know getting started commands of MongoDB.

docker exec command is used to run commands inside the docker container. -i flag keeps the STDIN channel open even if not attached.-t flag allocats the pseudo-TTY.

# Usage Format
# docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

# Run mongo db inside container
~ hpatel$ docker run mongo 

# open new tab and get container id 
# which is running mongo.
~ hpatel$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
782374debbcc        mongo               "docker-entrypoint.s…"   About a minute ago   Up About a minute   27017/tcp           magical_snyder

# Execute mongo commands on that container
~ hpatel$ docker exec -it 782374debbcc mongo
> use demo
switched to db demo
> db.createCollection("cars");
{ "ok" : 1 }
> show dbs
admin   0.000GB
config  0.000GB
demo    0.000GB
local   0.000GB

# List the folder system inside container
~ hpatel$ docker exec -it 782374debbcc sh
# ls
bin  boot  data  dev  docker-entrypoint-initdb.d  etc  home  js

References

Docker Get Started