Cloud.WorksCNCFDockerApril 4, 20190Docker 101: Part-4

Docker-101 Part-4 would enable you to run various commands using Docker CLI.

Docker CLI

To access Docker CLI you would have to execute a command docker in your command prompt.

$ docker
Usage: docker [OPTIONS] COMMAND [ARG...]
       docker [ --help | -v | --version ]

A self-sufficient runtime for containers.

      --config string      Location of client config files (default "/root/.docker")
  -D, --debug              Enable debug mode
      --help               Print usage
  -H, --host value         Daemon socket(s) to connect to (default [])
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

    attach    Attach to a running container
    # […]

Docker Help

To list the help on any command just execute the command, followed by the –help option.

$ docker run --help

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container

      --add-host value             Add a custom host-to-IP mapping (host:ip) (default [])
  -a, --attach value               Attach to STDIN, STDOUT or STDERR (default [])

Docker Image

To run any container locally on your docker host you would need Docker Images available on the host. You can use the following commands to push or pull images from the docker registry

To list the images on a docker host

$ docker image ls

To pull the images from the docker registry

$ docker image pull centos:latest

To push the images to the Docker registry from your local docker host

$ docker image push NAME[:TAG]

Here NAME should be your repository name either on Docker registry or your private registry, TAG should represent the version information about your image.

To remove any of the images available on your docker host

$ docker image rm NAME[:TAG]
$ docker rmi NAME[:TAG]

Docker Run

To run any docker container this command is used. General usage of this as follows


Some examples:

  • To run a centos docker container
    $ docker run -it centos:latest

    -i is for interactive
    -t is for allocating a pseudo-tty.

  • To run a container with exposing a specific port
    $ docker run --name some-nginx -d -p 8080:80 some-content-nginx

    –name to give a specific name to your container
    -p to do a port mapping

  • To run a container passing some environment variables
    $ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

    -e is used to pass on any environment variables in the container

  • To run a container mounting a persistent volume
    $ docker run --name some-nginx -v /some/content:/usr/share/nginx/html:ro -d nginx

    -v is used to mounting volumes to Docker containers

Accessing Docker containers

To access docker containers we need to use docker exec

General Usage


So if we need to access CentOS container, an example would be something like this

$ docker exec -it [container_name or container_id] /bin/bash(Shell)

Operations on Docker containers

To list all the running containers

$ docker ps

To list all the containers despite their status

$ docker ps -a

To stop a running container

$ docker stop [container_name or container_id]

To start a container that is in STOP state

$ docker start [container_name or container_id]

To remove a container from the docker host

$ docker rm [container_name or container_id]

Container has to be in stop state before you can remove it

To check the stats of running containers

$ docker stats

To check the logs of a specific container

$ docker logs [container_name or container_id]

This concludes our series on Docker containers, I hope the information would enable you to be able to learn the basics of Docker and start utilizing the power of containers for your development environment.

Please comment down below to let us know your views, feedbacks, and also what should be the next series which you would like us to start?

Before we leave, Here’s a cheat sheet for running Docker containers.



Leave a Reply

Your email address will not be published. Required fields are marked *