Skip to main content

Command Palette

Search for a command to run...

Containers

Updated
6 min read
A

I am a Student, who finds beauty in simple things. I like to teach sometimes.

Containers provide a standardized way to package and run applications, bundling an application's code with all its dependencies, such as libraries and configuration files. This packaging ensures that the application runs consistently across different computing environments.

Docker is an open-source platform that automates the deployment, scaling, and management of applications within containers.1 It provides a command-line interface (CLI) and a daemon (the Docker Engine) to build, ship, and run containerized applications. Docker utilizes OS-level virtualization, meaning containers share the host system's kernel but run in isolated user spaces. This makes containers lightweight and faster to start compared to traditional virtual machines, which require a full guest operating system.

Key Docker concepts include:

  • Image: A read-only template with instructions for creating a Docker container. Images are often based on other images, with additional customization. They are built from a Dockerfile, which is a text file containing a series of commands.

  • Container: A runnable instance of an image. Multiple containers can be created from the same image. Each container is isolated from others and from the host system.

  • Dockerfile: A script containing a sequence of commands that Docker uses to build an image. Commands include specifying a base image, adding files, running commands, and setting environment variables.

  • Registry: A storage system for Docker images. Docker Hub is a public registry, but private registries can also be used.

  • Docker Engine: The core of Docker, a client-server application with a daemon process (the dockerd command), a REST API that specifies interfaces for interacting with the daemon, and a command-line interface (CLI) client (the docker command).

Basic Container Usage

Interacting with Docker containers typically involves several core commands:

docker run: This command is used to create and start a new container from a specified image. If the image is not present locally, Docker will attempt to pull it from a configured registry (by default, Docker Hub).

The basic syntax is docker run [OPTIONS] IMAGE [COMMAND] [ARG...].

Some common options include:

  • -d or --detach: Runs the container in the background (detached mode) and prints the container ID.

  • -p HOST_PORT:CONTAINER_PORT or --publish HOST_PORT:CONTAINER_PORT: Publishes a container's port(s) to the host. This allows network traffic to be directed to the container. For example, -p 8080:80 maps port 80 in the container to port 8080 on the host.

  • -v HOST_PATH:CONTAINER_PATH or --volume HOST_PATH:CONTAINER_PATH: Mounts a volume from the host into the container. This is useful for persisting data generated by the container or providing data to it.

  • --name CONTAINER_NAME: Assigns a specific name to the container. If not specified, Docker generates a random name.

  • -it: A combination of -i (--interactive) which keeps STDIN open even if not attached, and -t (--tty) which allocates a pseudo-TTY. This is commonly used to get an interactive shell inside the container.

  • --rm: Automatically removes the container when it exits. This is useful for short-lived tasks.

  • -e KEY=VALUE or --env KEY=VALUE: Sets environment variables inside the container.

For example, to run an Nginx web server container in detached mode, map port 80 of the container to port 8080 on the host, and name it my_web_server, the command would be:

docker run -d -p 8080:80 --name my_web_server nginx

docker ps: This command lists running containers.

Common options include:

  • -a or --all: Shows all containers (default shows just running). This includes containers that have exited.

  • -q or --quiet: Only displays container IDs.

  • -s or --size: Displays total file sizes.

  • --filter KEY=VALUE: Filters output based on conditions. For example, docker ps --filter "status=exited" lists all containers that have exited.

Executing docker ps will show details like container ID, image name, command being run, creation time, status, ports, and names of the running containers. docker ps -a provides a more comprehensive list including stopped containers.

docker exec: This command allows you to run a command inside a running container. This is frequently used to inspect a container's environment, access logs, or perform administrative tasks.

The basic syntax is docker exec [OPTIONS] CONTAINER COMMAND [ARG...].

Common options include:

  • -d or --detach: Runs the command in the background inside the container.

  • -i or --interactive: Keeps STDIN open even if not attached.

  • -t or --tty: Allocates a pseudo-TTY.

  • -u or --user USERNAME|UID: Runs the command as a specific user or UID inside the container.

For instance, to get an interactive bash shell inside the my_web_server container (started in the previous docker run example), the command would be:

docker exec -it my_web_server bash

Once inside the container via this shell, you can execute commands as if you were in a terminal session within that container's isolated environment.

When and Why to Use Containers

Containers offer several advantages, making them suitable for a wide range of applications and scenarios:

  1. Portability and Consistency: Containers package an application and its dependencies. This ensures that the application behaves the same way regardless of where the container is run – on a developer's laptop, a test server, or a production cloud environment. This consistency eliminates the "it works on my machine" problem.

  2. Isolation: Containers provide process-level isolation. Applications running in different containers are isolated from each other and from the host system. This improves security and stability, as issues in one container are less likely to affect others or the host. Resource constraints (CPU, memory) can also be applied to individual containers.

  3. Resource Efficiency and Speed: Compared to traditional virtual machines (VMs), containers are significantly more lightweight. They do not require a separate operating system kernel for each instance. Instead, they share the host OS kernel. This results in faster startup times (seconds or even milliseconds) and lower resource consumption (CPU, RAM, disk space), allowing for higher density of applications on a single host.

  4. Scalability and Orchestration: Containers are designed for scalability. Multiple instances of a containerized application can be easily created and managed. Container orchestration platforms like Kubernetes, Docker Swarm, or Amazon ECS automate the deployment, scaling, load balancing, and management of containerized applications across clusters of machines.

  5. Microservices Architecture: Containers are well-suited for building and deploying microservices. Each microservice can be packaged as a separate container, allowing independent development, deployment, and scaling of services. This promotes modularity and agility in application development.

  6. Development and CI/CD Pipelines: Containers streamline development workflows. Developers can build and test applications in consistent environments. In Continuous Integration/Continuous Deployment (CI/CD) pipelines, containers allow for faster and more reliable builds, tests, and deployments across different stages.

  7. Dependency Management: Containers encapsulate all dependencies, including specific versions of libraries and runtimes. This avoids conflicts between different applications or different versions of the same application that might arise if they were installed directly on the same host system.

In summary, containers, and Docker as a leading platform, provide a powerful and efficient way to develop, ship, and run applications. Their ability to ensure consistency, isolate processes, optimize resource usage, and facilitate scaling makes them a fundamental technology in modern software development and operations.

More from this blog

Aman Pathak

58 posts

Things I would speak if the person in front of me is me