10+ Docker Commands with Examples!



Docker has become an extremely popular technology for deploying and distributing applications. It reduces the number of physical servers needed, provides better utilisation of resources and makes the whole development process easier, faster, and cheaper for everyone.


For the reasons I mentioned above, Docker has gained tremendous amount of popularity in a very short period of time. However, if you are a beginner and have no familiarity with the container orchestration business, it might be quite daunting to get started.

In this post, I intend to share some important docker commands with examples along with some tips so that anyone can start building their own containers and deploy them. Let's get started!

Terminology

Let's get familiar with some terms first:

Container image: A package with all of the dependencies and information needed to create a container. Usually, an image derives from multiple base images that are layers stacked one atop the other to form the container's file system.

Container: An instance of a Docker image. A container represents a run-time for a single application, process, or service.

Tag: A label that you can apply to images so that different images or versions of the same image can be identified. 

Dockerfile: A text file that contains instructions for how to build a Docker image.

Build: The action of building a container image based on the information and context provided by its Dockerfile as well as additional files in the folder where the image is built.

Repository: A collection of related Docker images labelled with a tag that indicates the image version.

Registry: A service that provides access to repositories. The default registry for most public images is Docker Hub.

Cluster: A collection of Docker hosts exposed as if they were a single virtual Docker host so that the application can scale to multiple instances of the services spread across multiple hosts within the cluster. You can create Docker clusters by using Docker Swarm, Mesosphere DC/OS, Kubernetes, and Azure Service Fabric.

Orchestrator: A tool that simplifies management of clusters and Docker hosts. Using orchestrators, you can manage their images, containers, and hosts through a command line interface or a graphical user interface. You can manage container networking, configurations, load balancing, service discovery, high availability, Docker host configuration, and more. An orchestrator is responsible for running, distributing, scaling, and healing workloads across a collection of nodes.

Docker Commands

In order to be able to run docker commands, we need to have Docker installed on our machines. Use this link to install Docker on your machine:

Docker CREATE

Creates a new container. If the container image is not found locally, it will be downloaded from the Hub. This container is never started. It can be started at any time with docker start container_id. Container ID can be found using docker images command.

Usage:

docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
Example:

docker create hello-world

Docker START

Starts a stopped or newly created container.

Usage:

docker start [OPTIONS] CONTAINER [CONTAINER...]
Example:

docker start f2a91732366c

Docker RUN

Creates a new container and starts it. Equivalent of using docker create and then docker start
.
Usage:

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

docker run hello-world

Docker STOP

Stops a running container.

Usage:

docker stop [OPTIONS] CONTAINER [CONTAINER...]
Example:

docker stop f2a91732366c

Docker BUILD

Builds an image from a Dockerfile.

Usage:

docker build [OPTIONS] PATH | URL | -
Example(Mind the dot at the end, it represents the build context which is passed to the docker daemon.):

docker build -f dockerfiles/Dockerfile -t myapp .

Docker RM

Removes one or more containers from the available containers list. (Use -f flag to force remove.)

Usage:

docker rm [OPTIONS] CONTAINER [CONTAINER...]
Example:

docker rm -f mycontainer

Docker RMI

Removes one or more images from the images list. (Use -f flag to force remove.)

Usage:

docker rmi [OPTIONS] IMAGE [IMAGE...]
Example:

docker rmi -f hello-world

Docker TAG

Creates a tag TARGET_IMAGE that refers to SOURCE_IMAGE.

Usage:

docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Example: To tag a local image with ID “0e5574283393” into the “fedora” repository with “version1.0”:

docker tag 0e5574283393 fedora/httpd:version1.0

Docker PUSH

Pushes an image or a repository to a registry.

Usage:

docker push [OPTIONS] NAME[:TAG]
Example: First save the new image by finding the container ID (using docker ps) and then committing it to a new image name. Then, tag and push the image to the registry using the image ID.

docker commit c16378f943fe rhel-httpd

docker tag rhel-httpd registry-host:5000/myadmin/rhel-httpd

docker push registry-host:5000/myadmin/rhel-httpd

Docker EXEC

The exec command can be used to run a command in a container that already running.

Usage:

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Example: To execute an interactive bash shell on the container.

docker exec -it mycontainer bash

Docker IMAGES

Lists docker images

Usage:

docker images [OPTIONS] [REPOSITORY[:TAG]]
Example: To execute an interactive bash shell on the container.

docker images -a

Docker STATS

Displays a live stream of container(s) resource usage statistics.

Usage:

docker stats [OPTIONS] [CONTAINER...]
Example: To execute an interactive bash shell on the container.

docker stats

These several docker commands should be enough to get you started using Docker on the command line. Don't forget to sign up for a Docker hub account if you don’t have one and have a go at deploying your own app.

I strongly recommend using Golang for building applications due to its ease of use and cross-compilation support so that you can build your applications on whatever development environment you have first, and then place the executable inside your docker containers. This way you wouldn't have to put your source code or install large dependencies to run your application in a container. I also recommend using Alpine Linux container images due to their incredibly small size!

Once you get used to the process, you’ll never go back to your old deployment habits. Try playing around to get used to how things work! Once you have your containers ready, don't forget to check out Docker Swarm or Kubernetes for container orchestration to scale your application even further.  Thank you for reading and drop a comment below if you have any comments or questions!
Author:

Software Developer, Codemio Admin

Disqus Comments Loading..