Docker Cheat Sheet (Docker Commands + Free PDF)

Docker is an open-source containerization platform for building, running, and managing containers on the server and cloud. It is a de facto standard for creating and managing containerized applications. Docker was introduced in 2013 as an industry standard for orchestrating containers.

A container is a software unit that combines an application’s source code and operating system libraries and dependencies so that the application can work in any environment.

Today, most companies use Docker to manage and deliver distributed applications having complex requirements. So, the demand for Docker experts is also increasing at an alarming rate.

So, if you want to set your foot in this field, you must prepare with all the technical stuff for cracking your interview. We have created this Docker commands cheat sheet to make your life easier. You can refer to this Docker cheat sheet to brush up on your knowledge.

Why Docker?

Earlier, it was pretty challenging for developers to create complex applications with several dependencies and manage their runtime as per the underlying infrastructure. But with Docker , developers can simply break down the complex applications into smaller parts and put them into the container along with all the dependency files required to run that container, independent of the underlying infrastructure.

Developers can quickly run the Docker container on any OS-compatible host (Linux or Windows) with the Docker runtime installed.

Here are some remarkable features of Docker :

Believe it or not, it has benefitted several companies to speed up their SDLC process and improve performance by removing the dependency on the underlying infrastructure.

Prerequisites for Docker

The following are some major prerequisites of different operating systems to install docker:

The 3.10.x kernel is the minimum requirement for Docker.

10.8 “Mountain Lion” or newer is required.

Hyper-V must be enabled in BIOS.

VT-D must also be enabled if available (Intel Processors).

You should have Windows Server 2016 as the minimum version for installing docker and docker-compose. Some limitations come with this version, such as multiple virtual networks and Linux containers. It is recommended to have Windows Server 2019 and later versions for better compatibility.

Installing Docker

To install docker on your system, you can execute simple commands from your system’s command-line terminal, as shown below. If your specific OS steps are not mentioned below, you can explore them online.

Running the following command is the easiest way to install Docker on Linux OS quickly.

curl -sSL https://get.docker.com/ | sh

Download and install Docker Community Edition. For Homebrew-Cask, just type brew cask install docker. Or you can download and install Docker Toolbox. macOS

After installing the Docker Community Edition, click the docker icon in Launchpad to start up a container.

docker run hello-world

You can install Docker for Windows 10. After installing, run the Docker installer by double-clicking it. After completing the installation process, go to the whale icon in the notification indicating Docker is running, and you can access it via terminal. Windows 10

You can run the following command from the terminal line to check the installed Docker version.

docker version

With the help of the OneGet provider PowerShell module, you can easily install the Docker EE on Windows Server. First, you need to install the Docker-Microsoft PackageManagement Provider module from the PowerShell Gallery.

Install-Module -Name DockerMsftProvider -Repository PSGallery -Force

To view the installed package provider and the Docker package, type the below command:

Get-PackageProvider -ListAvailableget-packagesource -ProviderName DockerMsftProvider

To install the latest version of Docker, use the following command

Install-Package -Name docker -ProviderName DockerMsftProvider

To check the installed Docker version, run the following command:

Architecture of Docker

DevOps architecture has five major working entities, namely registry, image, container, daemon, and client.

Now, let us get started with the Docker commands list. We will first cover the Docker commands for its architecture's five entities mentioned above.

Docker Commands Cheat Sheet

Registry & Repository

Repository refers to the hosted collection of the images creating the file system for containers. Registry refers to the host containing the repositories and providing an HTTP API that helps manage the repositories.

Docker has its central registry with thousands of repositories. But before you use the images from this registry, make sure you verify them to avoid security issues.

Images

You can refer to images as the templates for the Docker containers. You can run the following commands to work with the images:

Container

Containers are the isolated Docker process that contains the code to be executed.

Usually, a container will start and stop immediately if you run it without any option.

Dockerfile Cheat Sheet

It is a config file that will set up a Docker container whenever you run a docker build on it. To create docker files, you can use any of the following text editors and their syntax highlighting modules.

The following are some instructions that you can use while working with Dockerfile:

Networks

Docker has a featured network, allowing the containers to connect. You can create three network interfaces with Docker, namely bridge, host, and none.

By default, the new container is launched into the bridge network. To establish communication among several containers, you need a new network for launching containers in it. It lets the containers communicate while being isolated from other containers not connected to the network.

Volumes

Docker has volumes that are free-floating filesystems. So there is no need to be connected to a particular container. You can use volumes mounted from data-only containers for portability. As per Docker 1.9.0, it comes with the named volumes that replace data-only containers.

Orchestrate

Orchestration manages the container’s life cycle, especially in dynamic environments. You can use it for controlling and automating several tasks for containers.

Among a long list of Docker orchestration tools, the most commonly used orchestration tools are Docker Swarm, Kubernetes, and Mesos. In this Docker cheat sheet, we are using Docker Swarm commands.

Interaction with container

You can use the following commands to interact with the container.

Build

You can use the following commands to build the images from a Docker file.

Cleanup

To optimize the usage of the resources, you need to clean up the resources frequently to maintain the performance. You can run the following commands to clean up resources.

Services

Let’s now take a sneak peek at the commands used to view the running services, run the services, view all service logs, and scale the services.

Docker-compose Cheat Sheet

Compose is a tool that helps you to define and run multi-container Docker applications. With Compose, you get to work with a YAML file to configure your application services. With the help of the following commands, you can simply create and start all the services from your configuration.

Basic example

# docker-compose.yml version: '2' services: web: build: . # build from Dockerfile context: ./Path dockerfile: Dockerfile ports: - "5000:5000" volumes: - .:/code redis: image: redis
Building web: # build using the Dockerfile build: . # build using the custom Dockerfile build: context: ./dir dockerfile: Dockerfile.dev # build from image image: ubuntu image: ubuntu:14.04 image: tutum/influxdb image: example-registry:4000/postgresql image: a4bc65fd
 ports: - "3000" - "8000:80" # guest:host # expose ports to linked services (not to host) expose: ["3000"] Commands # commands to execute command: bundle exec thin -p 3000 command: [bundle, exec, thin, -p, 3000] # overriding the entrypoint entrypoint: /app/start.sh entrypoint: [php, -d, vendor/bin/phpunit]
# environment variables environment: RACK_ENV: development environment: - RACK_ENV=development # environment vars from the specified file env_file: .env env_file: [.env, .development.env]
 # makes the `db` service available as the hostname `database` # (implies depends_on) links: - db:database - redis # make sure `db` is alive before starting depends_on: - db
# make this service extend another extends: file: common.yml # optional service: web app volumes: - /var/lib/mysql - ./_data:/var/lib/mysql

Conclusion

Docker is a software offering platform-as-a-service services for creating and deploying applications by encapsulating the software within containers.

Containers are lightweight, portable entities that can be easily shared without depending on the underlying infrastructure or worrying about their compatibility with several systems. Due to its features, many companies are now adopting docker containers for creating complex applications.

Docker comes with a wide range of terminology related to its services, such as Dockerfiles, images, containers, and other Docker-specific words. Everything can be handled using Docker commands. In this Docker Cheat Sheet PDF, you will get a list of all the Docker commands you can refer to anytime.

Docker FAQs

What are all the Docker commands?

What is the ENV command in docker?

You can use ENV for providing the default values for your future environment variables within the container. You cannot change the ENV variables via the command line, but you can use the ARG variables if you want.

In the following example of the Dockerfile, we have created two variables, one as ARG and one as ENV.

Using ARG Value in ENV Variable

FROM alpine:3.7 ARG VARIABLE_1=5 ENV VARIABLE_2=$VARIABLE_1 RUN echo "print variable value:" $VARIABLE_1 RUN echo " print ENV variable : " $VARIABLE_2

Does Dockerfile need CMD?

CMD instruction allows the developers to set a default command, which will be executed only when running a container without specifying a command. If you run a Docker container with a command, it will ignore the default command. If Dockerfile has more than one CMD instruction, all the last CMD instructions are ignored.

CMD has three forms:

How do I learn Docker commands?

There are several online and offline resources that you can consider for learning Docker commands. For that, you need to have strong knowledge of the command line. Also, you can go through this Docker commands cheat sheet and brush up on your skills for a quick reference.

Is Docker CLI still free?

You can still enjoy Docker freely for personal/student/small businesses. Small businesses have less than 250 employees and an annual turnover of less than $10m.

Click here to view full pricing details.

How do I execute a docker container?

docker run -d --name container-name alpine watch "date >> /var/log/date.log"

This command creates a new Docker container from the official alpine image.

docker ps
docker rename container-name new-name
docker exec -it container-name sh

It will run the sh shell in the specified container, giving you a basic shell prompt. To exit the container, run the following command.

Exit
docker exec --workdir /tmp container-name pwd
docker exec --user guest container-name whoami
docker exec -e TEST=sammy container-name env
nano .env

We have used the .env as the filename to manage information outside of version control.

TEST=sammy ENVIRONMENT=prod

docker exec --env-file .env container-name env

People are also reading: