# Mastering Docker: Understanding Docker Engine and Docker Images

This guide takes an in-depth exploration of the fundamental elements of [Docker](https://code.visualstudio.com/docs/containers/ssh), offering practical examples, valuable tips, and expert insights to help you fully realize the immense potential that [Docker](https://docs.orbstack.dev/docker/) has to offer.

## [Docker Engine](https://docs.docker.com/engine/)

The [Docker Engine](https://docs.docker.com/engine/reference/commandline/container/) is a crucial [Docker](https://docs.docker.com/engine/install/) platform component responsible for constructing, operating, and maintaining containers, providing a runtime environment and enabling user interaction via the [Docker](https://docs.docker.com/engine/install/ubuntu/) command-line interface.

### **Docker Daemon (dotckerd)**

The [Docker](https://www.edureka.co/blog/docker-for-windows/) Daemon, also known as `dockerd`, is a background service on the host system that manages [Docker](https://www.reddit.com/r/docker/comments/xe4u9n/where_is_stated_that_docker_engine_not_docker/) containers and [images](https://hub.docker.com/_/docker), listening to [Docker](https://geekflare.com/docker-architecture/) API requests.

[Docker Engine](https://docs.docker.com/engine/install/centos/)'s server, manages container lifecycle operations, scheduling, and interaction with the host operating system's kernel, running as a daemon process in the background.

**Example:** To start the [Docker](https://devopscube.com/what-is-docker/) Daemon, you typically don't need to interact with it directly. It runs in the background when you start [Docker](https://dockerlabs.collabnix.com/advanced/enterprise/what-is-docker-enterprise.html) on your system.

### **APIs (Application Programming Interfaces)**

[Docker Engine](https://www.techtarget.com/searchitoperations/definition/Docker-Engine) provides APIs for communication between client applications and [Docker](https://www.sitepoint.com/docker-windows-10-home/) daemon, enabling developers to control containers, [images](https://docs.docker.com/engine/reference/commandline/image/), networks, and volumes, integrating [Docker](https://www.docker.com/products/container-runtime/) functionality into workflows.

### [**Docker**](https://docs.docker.com/get-started/overview/) **Command-Line Interface (CLI)**

The [Docker](https://en.wikipedia.org/wiki/Docker_(software)) CLI is a user-friendly command-line tool that enables interaction with [Docker Engine](https://github.com/docker/engine), allowing users to build container [images](https://docs.docker.com/engine/reference/commandline/images/), manage networks, and inspect container states.

**Example:** To run a simple Nginx web server container, you can use the following command.

### Running an Nginx Container

Step-by-step instructions to run a [Nginx](https://www.cloudbees.com/blog/docker-image) web server in a [Docker](https://discourse.ubuntu.com/t/install-and-configure-docker-engine-on-ubuntu/26182) container, use the official [Nginx](https://support.atlassian.com/bitbucket-cloud/docs/build-and-push-a-docker-image-to-a-container-registry/) [image](https://www.techtarget.com/searchitoperations/definition/Docker-image) from [Docker](https://www.knowledgehut.com/blog/devops/docker-containers-docker-hub) Hub.

* **Step 1: Pull the** [**Nginx**](https://www.nginx.com/) [**Image**](https://hub.docker.com/)
    
* **Step 2: Run the** [**Nginx**](https://nginx.org/) **Container**
    
* **Step 3: Access** [**Nginx**](https://airflow.apache.org/docs/docker-stack/index.html) **in a Web Browser**
    

**Step 1: Pull the** [**Nginx**](https://nginx.org/) [**Image**](https://www.geeksforgeeks.org/what-is-docker-images/)

```bash
docker pull nginx
```

**Step 2: Run the** [**Nginx**](https://nginx.org/en/) **Container**

Use the `docker pull` command to download the official [Nginx](https://en.wikipedia.org/wiki/Nginx) [image](https://jfrog.com/devops-tools/article/understanding-and-building-docker-images/) from [Docker](https://phoenixnap.com/kb/what-is-docker) Hub

```bash
docker run -d -p 80:80 --name my-nginx nginx
```

* '**\-d**': Detaches the container and runs it in the background.
    
* '**\-p 80:80**': Maps port 80 on your host to port 80 in the container, allowing you to access the [Nginx](https://kinsta.com/knowledgebase/what-is-nginx/) web server from the web browser on your host.
    
* '**\--name my-nginx**': Gives the container a unique name ("**<mark>my-nginx</mark>**").
    

**Step 3: Access** [**Nginx**](https://hub.docker.com/_/nginx) **in a Web Browser**

Enter your host machine's IP address in a web browser to access the default [Nginx](https://www.javatpoint.com/nginx-introduction) welcome page on your local machine or the server's IP address on a remote server.

* [Docker](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-22-04) on your local machine, you can access it at [http://localhost\`](http://localhost%60).
    
* [Docker](https://k21academy.com/docker-kubernetes/docker-architecture-docker-engine-components-container-lifecycle/) on a remote server, replace "[localhost](http://localhost)" with the server's IP address.
    

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Customize the Nginx configuration by mounting a local configuration file into the container or by creating a custom <a target="_blank" rel="noopener noreferrer nofollow" href="https://kinsta.com/blog/install-docker-ubuntu/" style="pointer-events: none">Docker</a> <a target="_blank" rel="noopener noreferrer nofollow" href="https://medium.com/containers-101/building-docker-images-with-dockerfiles-361d1d0a4047" style="pointer-events: none">image</a> with your configuration changes.</div>
</div>

The CLI allows users to interact with [Docker](https://www.jetbrains.com/help/idea/settings-docker.html), build, run, manage, and troubleshoot containers using commands like `docker run`, `docker build`, and `docker ps`.

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">This command instructs Docker to launch a Nginx container in detached mode ('<strong>-d</strong>') and to map host port <strong>80</strong> to container port <strong>80</strong>.</div>
</div>

## Docker Objects

[Docker Engine](https://www.quora.com/What-is-Docker-Engine) is in charge of building and maintaining numerous [Docker](https://www.upwork.com/resources/install-docker-engine) objects, such as

* [**Docker**](https://developer.hashicorp.com/terraform/enterprise/replicated/requirements/docker_engine) [**Image**](https://docker-curriculum.com/)
    
* **Containers**
    
* **Networks**
    
* **Storage**
    
* **Security**
    
* **Monitoring**
    
* **Services**
    

### **Docker** [**Image**](https://www.tutorialspoint.com/docker/docker_images.htm)

[Docker Engine](https://community.chocolatey.org/packages/docker-engine) offers tools for creating and managing [Docker](https://www.asustor.com/en/app_central/app_detail?id=754&type=) [images](https://circleci.com/blog/docker-image-vs-container/), allowing users to create or modify existing [images](https://k21academy.com/docker-kubernetes/docker-image-and-layer-overview-for-beginners/), store them in a local cache, and access them from Docker registries.

**Example**: To build a custom [Docker](https://www.howtogeek.com/devops/how-to-get-started-using-the-docker-engine-api/) [image](https://intellipaat.com/blog/docker-image/) from a [Dockerfile](https://www.ionos.com/digitalguide/server/configuration/docker-tutorial-installation-and-first-steps/), create a file named `Dockerfile` with the following content

```dockerfile
FROM ubuntu:20.04
RUN apt-get update && apt-get install -y curl
```

Build the [image](https://www.simplilearn.com/tutorials/docker-tutorial/docker-images) using this command

```bash
docker build -t my-ubuntu .
```

This creates a [Docker](https://stackoverflow.com/questions/60527336/what-is-the-difference-between-docker-daemon-and-docker-engine) [image](https://aws.amazon.com/docker/) named `my-ubuntu` based on **<mark>Ubuntu 20.04</mark>** with the `curl` package installed.

### **Container**

[Docker Engine](https://www.virtuozzo.com/application-platform-docs/docker-engine-deployment/) uses a container runtime, typically [Docker](https://opensource.com/resources/what-docker)'s containers, but can also use other runtimes like **<mark>rkt</mark>** or containers for container execution on the host system.

**Example**: When you run a container, [Docker Engine](https://opensource.com/resources/what-docker) uses its runtime to create an isolated environment. For instance, if you run a Python application, it ensures that the Python interpreter and necessary libraries are available within the container.

### **Networks**

[Docker Engine](https://pypi.org/project/docker/) offers built-in networking for containers to communicate with each other and external networks, allowing users to create custom networks for better isolation and control.

**Example**: Create a custom [Docker](https://en.opensuse.org/Docker) network

```bash
docker network create my-network
```

Run two containers on this network and allow them to communicate

```bash
docker run -d --network my-network --name container1 ubuntu
docker run -d --network my-network --name container2 ubuntu
```

`container1` and `container2` can communicate over the `my-network` bridge network.

### **Storage**

The [Docker Engine](https://www.simplilearn.com/tutorials/docker-tutorial/install-docker-on-windows) offers built-in networking capabilities for containers to communicate with each other and external networks, allowing users to create custom networks for better isolation and control.

**Example:** When you create a container, [Docker Engine](https://www.aquasec.com/cloud-native-academy/docker-container/docker-architecture/) creates a read-only layer from the [image](https://stackify.com/docker-build-a-beginners-guide-to-building-docker-images/) and a writable layer for the container. Any changes made within the container are stored in this writable layer, preserving the [image](https://en.wikipedia.org/wiki/Docker_(software))'s original state.

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><a target="_blank" rel="noopener noreferrer nofollow" href="https://community.chocolatey.org/packages/docker-engine" style="pointer-events: none">Docker Engine</a> manages container storage using layered file systems.</div>
</div>

### **Security**

[Docker Engine](https://endoflife.date/docker-engine) offers security features like container isolation, resource usage control, and security scanning, ensuring the protection of containers and the host system.

**Example:** [Docker](https://docs.rockylinux.org/gemstones/docker/) uses namespaces and c-groups to isolate containers. For example, containers cannot see or interfere with processes running outside their namespace. This enhances security and isolation.

### Monitoring

[Docker Engine](https://docs.oracle.com/en/operating-systems/oracle-linux/docker/docker-ManagingtheDockerEngineService.html#docker-service) provides logging and monitoring features for users to track container behavior, troubleshoot issues, and integrate with third-party monitoring and logging solutions.

**Example:** You may use the following command to inspect the logs of a running container

```bash
docker logs <container_id>
```

### Services

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Services are a higher-level abstraction in <a target="_blank" rel="noopener noreferrer nofollow" href="https://learn.microsoft.com/en-us/virtualization/windowscontainers/manage-docker/configure-docker-daemon" style="pointer-events: none">Docker</a> Swarm.</div>
</div>

[Docker Engine](https://www.ibm.com/topics/docker)'s Swarm Mode is a native clustering and orchestration feature that simplifies container application deployment, enabling users to manage a swarm of [Docker](https://kubernetes.io/docs/setup/production-environment/container-runtimes/) nodes to scale multi-container applications.

**Example:** To create a [Docker](https://www.youtube.com/watch?v=YeF7ObTnDwc&ab_channel=BretFisherDockerandDevOps) Swarm and deploy services, you would initialize a Swarm on one node, add more nodes as workers or managers, and then deploy services using [Docker](https://blog.avenuecode.com/running-docker-engine-on-wsl-2) Compose or the [Docker](https://waytoeasylearn.com/learn/docker-engine/) CLI.

## Docker [Image](https://phoenixnap.com/kb/docker-image-vs-container)

[Docker](https://aws.amazon.com/docker/) [images](https://github.com/docker-library/official-images) are lightweight, executable packages containing code, runtime environment, libraries, and system tools, used to create [Docker](https://0xn3va.gitbook.io/cheat-sheets/container/overview/docker-engine) containers, portable environments for consistent application running across different systems.

### Creating [Docker](https://wiki.archlinux.org/title/docker) [Images](https://www.stacksimplify.com/aws-eks/docker-basics/build-docker-image/)

A [Dockerfile](https://pkg.go.dev/github.com/docker/docker/client), which contains a collection of instructions for generating the [image](https://chtc.cs.wisc.edu/uw-research-computing/docker-build), is generally used to produce [Docker](https://www.opc-router.com/what-is-docker/) [images](https://docs.sevenbridges.com/docs/upload-your-docker-image-1). The steps involved in creating a [Docker](https://0xn3va.gitbook.io/cheat-sheets/container/overview/docker-engine) [image](https://www.oracle.com/in/cloud/cloud-native/container-registry/what-is-docker/)

* Selecting a base [image](https://www.ionos.com/digitalguide/server/know-how/docker-images/) (**Linux distribution**)
    
* Installing dependencies
    
* Application code copying
    
* Configuring environment variables
    
* Setting up runtime commands
    

### Selecting a base [image](https://www.toolsqa.com/docker/docker-images/)

* A base [image](https://www.jetbrains.com/help/idea/docker-images.html) serves as the foundation for your [Docker](https://docs.gitlab.com/ee/install/docker.html) [image](https://devopscube.com/build-docker-image/).
    
* It includes the necessary operating system and libraries.
    
* Official Linux distribution [images](https://www.pluralsight.com/guides/create-docker-images-docker-hub) (**Ubuntu, Alpine, CentOS**) or customized [images](https://blog.packagecloud.io/what-is-a-docker-image/) suited to certain use cases (**Python, Node.js**) are popular options.
    

Using a smaller base [image](https://blog.packagecloud.io/what-is-a-docker-image/), such as **Alpine Linux**, can reduce [image](https://stackoverflow.com/questions/23735149/what-is-the-difference-between-a-docker-image-and-a-container) size and enhance efficiency, which is especially important for production use.

```dockerfile
# Use Alpine Linux as the base image
FROM alpine:3.14
```

### Installing dependencies

* Installing dependencies ensures that your application has access to the required runtime resources.
    
* Using package managers like '**apt**'(for Debian-based systems) or '**apk**' (for Alpine Linux), install additional software or libraries for your application after creating a base [image](https://cloud.google.com/build/docs/build-push-docker-image).
    

```dockerfile
# Install Python and required packages using the package manager
RUN apk add --no-cache python3 py3-pip
```

### Application code copying

* Use the '**<mark>COPY</mark>**' instruction to add your application code to the [Docker](https://docker-py.readthedocs.io/en/stable/) [image](https://grafana.com/docs/grafana/latest/setup-grafana/installation/docker/). This phase moves files from your local directory into the file system of the container.
    
* To reduce picture size and increase construction performance, transfer only the essential files.
    

```dockerfile
# Copy the application code into the container
COPY . /app
```

### Configuring environment variables

* Environment variables are used to configure your application's runtime behavior. They can be specified directly in the [Dockerfile](https://docker-py.readthedocs.io/en/stable/) or as parameters supplied to the container during runtime.
    
* Environment variables can be used to configure database connections, API keys, and application settings.
    

```dockerfile
# Set an environment variable
ENV API_KEY=myapikey
```

### Setting up runtime commands

* The command to be run when the container starts is specified by the '**CMD**' or '**<mark>ENTRYPOINT</mark>**' directive. This might be a shell command, a script, or the executable for the main program.
    
* When executing the container, use '**<mark>CMD</mark>**' to specify a command that can be overridden. To establish a fixed entry point with parameters that may be appended at runtime, use '**<mark>ENTRYPOINT</mark>**'.
    

```dockerfile
# Define the command to run when the container starts
CMD ["python", "app.py"]
```

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><a target="_blank" rel="noopener noreferrer nofollow" href="https://registry.terraform.io/providers/kreuzwerker/docker/latest/docs/resources/image" style="pointer-events: none">Docker images</a> enable developers to package applications into portable units, offering isolation, portability, and resource efficiency. They create consistent containers across different environments with your application, dependencies, and configurations.</div>
</div>

to be continued...

<details data-node-type="hn-details-summary"><summary>Summary</summary><div data-type="detailsContent"><a target="_blank" rel="noopener noreferrer nofollow" href="https://www.spiceworks.com/tech/big-data/articles/what-is-docker/" style="pointer-events: none">Docker Engine </a>is the core <a target="_blank" rel="noopener noreferrer nofollow" href="https://handbook.vanillaos.org/2022/02/05/install-docker.html" style="pointer-events: none">Docker</a> component, enabling container creation, management, and management, and offering a user-friendly CLI, resource management, networking, storage, security, and monitoring features. <a target="_blank" rel="noopener noreferrer nofollow" href="https://www.baeldung.com/ops/docker-images-vs-containers" style="pointer-events: none">Docker image</a> creation involves a <a target="_blank" rel="noopener noreferrer nofollow" href="https://kodekloud.com/blog/docker-create-image-from-container/" style="pointer-events: none">Dockerfile</a>, which includes choosing a base <a target="_blank" rel="noopener noreferrer nofollow" href="https://www.mend.io/free-developer-tools/blog/update-docker-images/" style="pointer-events: none">image</a>, installing dependencies, copying application code, configuring environment variables, and setting up runtime commands. This process creates a self-contained package for a consistent application running across different systems. <a target="_blank" rel="noopener noreferrer nofollow" href="https://learn.microsoft.com/en-us/dotnet/architecture/microservices/net-core-net-framework-containers/official-net-docker-images" style="pointer-events: none">Docker images</a> offer advantages like isolation, portability, and resource efficiency, but require careful configuration and management.</div></details>

"**Mastering** [**Docker**](https://www.howtogeek.com/devops/how-to-get-started-using-the-docker-engine-api/)**: Understanding** [**Docker**](https://k21academy.com/docker-kubernetes/docker-architecture-docker-engine-components-container-lifecycle/) **Engine and** [**Docker**](https://handbook.vanillaos.org/2022/02/05/install-docker.html) [**Images**](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/compare-Docker-image-vs-container-difference-compare)" is a comprehensive and invaluable resource tailored for developers, DevOps engineers, and system administrators. We hope you find this guide insightful and useful in your journey with [Docker](https://www.freecodecamp.org/news/the-docker-handbook/).

Stay tuned for the upcoming articles in the series, where we'll discuss more interesting topics related to [Docker](https://docs.appsmith.com/getting-started/setup/installation-guides/docker). Subscribe to our channel to ensure you don't miss any part of this enlightening journey!

Thank you for reading our blog. Our top priority is your success and satisfaction. We are ready to assist with any questions or additional help.

Warm regards,

[**Kamilla Preeti Samuel,**](https://www.linkedin.com/in/preeti-samuel-kamilla-5a247962/)

**Content Editor**

[**ByteScrum Technologies Private Limited!**](https://www.bytescrum.com/) 🙏
