DEV Community

Subham Nandi
Subham Nandi

Posted on • Edited on

Docker: Day 1 - Introduction

Docker: Service Containers

Why Are Containers Required?

Containers are essential for modern application deployment and management because they provide:

  • Efficiency: Containers consume fewer resources compared to virtual machines.
  • Consistency: Ensures that applications behave the same way across different environments.
  • Portability: Simplifies application migration between development, testing, and production environments.
  • Scalability: Enables easy scaling of applications to meet demand.

What Is a Container Image?

A container image is a lightweight, standalone package that includes everything needed to run an application:

  • Code
  • Runtime
  • System Tools
  • Libraries

Key Points:

  • A container is a running instance of a container image.
  • You can run multiple containers from the same image, enabling horizontal scaling and environment replication.

Docker Central Repository: Docker Hub

Docker Hub is a central repository where container images are stored. Developers can:

  • Download pre-built images.
  • Publish custom images.
  • Manage private and public container images.

Starting an Nginx Web Server in Docker

Start Nginx Web Server:

To run an open-source Nginx web server:

docker container run --publish <host_port:container_port> <image_name>
Enter fullscreen mode Exit fullscreen mode

Example:

docker container run --publish 8080:80 nginx
Enter fullscreen mode Exit fullscreen mode

Stop Container Foreground Process:

Use the keyboard shortcut:

Ctrl + C
Enter fullscreen mode Exit fullscreen mode

Start Container in Background (Detach Mode):

Run the container in the background:

docker container run --publish <host_port:container_port> --detach <image_name>
Enter fullscreen mode Exit fullscreen mode

Example:

docker container run --publish 8080:80 --detach nginx
Enter fullscreen mode Exit fullscreen mode

Listing and Managing Containers

List Running Containers:

  • Modern command:
  docker container ls
Enter fullscreen mode Exit fullscreen mode
  • Old way:
  docker ps
Enter fullscreen mode Exit fullscreen mode

List All Containers (Running and Stopped):

docker container ls -a
Enter fullscreen mode Exit fullscreen mode

Stop a Running Container:

docker container stop <container_id>
Enter fullscreen mode Exit fullscreen mode

Difference Between run and start Commands

run:

  • Always starts a new container.

start:

  • Starts an existing container that has been stopped.

Naming Containers

Assigning a specific name to a container:

docker container run --publish 80:80 --detach --name <name> <image_name>
Enter fullscreen mode Exit fullscreen mode

Example:

docker container run --publish 80:80 --detach --name my-nginx nginx
Enter fullscreen mode Exit fullscreen mode

Viewing Logs and Processes in Containers

View Logs of a Specific Container:

docker container logs <container_name>/<container_id>
Enter fullscreen mode Exit fullscreen mode

View Running Processes Inside a Container:

docker container top <container_id>
Enter fullscreen mode Exit fullscreen mode

Removing Unused Containers

Remove unused containers by specifying their IDs:

docker container rm <space_separated_container_ids>
Enter fullscreen mode Exit fullscreen mode

Containers vs. Virtual Machines

Feature Containers Virtual Machines (VMs)
Virtualization Layer Operating System (OS) Hardware
Resource Allocation Lightweight and portable Resource-intensive
Portability Highly portable Limited portability

Key Takeaway:

  • Containers virtualize the OS, while VMs virtualize the hardware.
  • Containers are ideal for modern, scalable applications.

Resource Management and Monitoring

View Resource Consumption of Containers:

docker stats [container_name or container_id]
Enter fullscreen mode Exit fullscreen mode

Get Detailed Information About a Container:

docker inspect [container_name or container_id]
Enter fullscreen mode Exit fullscreen mode

Interactive Containers

Start a Container in Interactive Mode:

docker run -it [image_name or image_id] [command]
Enter fullscreen mode Exit fullscreen mode

Options:

  • -i: Keeps STDIN open even if not attached.
  • -t: Allocates a pseudo-terminal (TTY).

Example:

docker run -it ubuntu bash
Enter fullscreen mode Exit fullscreen mode

Running Commands in Running Containers

Execute commands inside a running container:

docker exec [options] [container_name or container_id] [command]
Enter fullscreen mode Exit fullscreen mode

Parameters:

  • [options]: Additional options (e.g., -it for interactive mode).
  • [container_name or container_id]: The name or ID of the running container.
  • [command]: The command to execute inside the container.

Example:

docker exec -it my-nginx bash
Enter fullscreen mode Exit fullscreen mode

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more