Building and Managing Containers Using the Linux Command Line

Containers have revolutionized the way applications are developed, deployed, and managed. They offer a lightweight, isolated environment that packages an application and its dependencies together, ensuring consistency across different environments. In the Linux ecosystem, the command - line interface provides a powerful and efficient way to build and manage containers. This blog will guide you through the process of building and managing containers using the Linux command line, covering key concepts, usage methods, common practices, and best practices.

Table of Contents

  1. Fundamental Concepts of Containers
  2. Prerequisites for Container Management on Linux
  3. Building Containers
  4. Managing Containers
  5. Common Practices and Best Practices
  6. Conclusion
  7. References

Fundamental Concepts of Containers

What are Containers?

Containers are a form of operating - system - level virtualization. Unlike traditional virtual machines, which virtualize an entire operating system, containers share the host operating system’s kernel. This makes containers more lightweight and resource - efficient. A container packages an application and all its dependencies (such as libraries, binaries, and configuration files) into a single unit, allowing it to run consistently across different environments.

Container Images

A container image is a read - only template that contains all the files and configurations needed to run a container. It serves as a blueprint for creating containers. Images are built from a set of instructions defined in a Dockerfile (a common tool for container image creation).

Container Runtimes

A container runtime is responsible for running container images. Docker is one of the most popular container runtimes, but there are others like containerd and CRI - O. The runtime takes an image and creates a running container instance.

Prerequisites for Container Management on Linux

Before you start building and managing containers on the Linux command line, you need to have a container runtime installed. For this blog, we’ll use Docker as an example. You can install Docker on most Linux distributions using the package manager.

Installing Docker on Ubuntu

# Update the package list
sudo apt update
# Install Docker dependencies
sudo apt install apt-transport-https ca-certificates curl software-properties-common
# Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# Add the Docker repository
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Update the package list again
sudo apt update
# Install Docker
sudo apt install docker-ce docker-ce-cli containerd.io
# Start and enable Docker service
sudo systemctl start docker
sudo systemctl enable docker

Building Containers

Writing a Dockerfile

A Dockerfile is a text file that contains a series of instructions for building a Docker image. Here is a simple example of a Dockerfile for a Python Flask application:

# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY. /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

Building the Docker Image

Once you have a Dockerfile, you can build a Docker image using the docker build command. Assume the Dockerfile and your application code (including requirements.txt and app.py) are in the current directory:

docker build -t my-python-app:1.0 .
  • -t: This option tags the image with a name and version. In this case, the image is named my - python - app with version 1.0.
  • .: The . at the end indicates the build context, which is the current directory.

Managing Containers

Creating and Starting a Container

To create and start a container from the image we just built, use the docker run command:

docker run -p 5000:5000 my-python-app:1.0
  • -p 5000:5000: This option maps port 5000 on the host to port 5000 in the container, allowing you to access the application running in the container from the host.

Listing Running Containers

To see a list of currently running containers, use the docker ps command:

docker ps

Stopping a Container

To stop a running container, you first need to get the container ID from the docker ps output. Then use the docker stop command:

docker stop <container_id>

Removing a Container

Once a container is stopped, you can remove it using the docker rm command:

docker rm <container_id>

Inspecting a Container

You can inspect a container to view detailed information about its configuration, such as its IP address, mounts, and environment variables using the docker inspect command:

docker inspect <container_id>

Pausing and Unpausing a Container

You can pause a running container to temporarily halt its processes using the docker pause command and unpause it later using the docker unpause command:

docker pause <container_id>
docker unpause <container_id>

Common Practices and Best Practices

Common Practices

  • Use Official Base Images: When creating a Dockerfile, start with official base images provided by the container registry. For example, use python:3.9 - slim for a Python application as shown in the example above. These images are well - maintained and optimized.
  • Separate Build and Run Stages: In a multi - stage build, separate the build environment from the runtime environment. This helps reduce the size of the final container image.

Best Practices

  • Limit Container Privileges: Run containers with the least amount of privileges necessary. Avoid running containers as the root user inside the container. You can use the --user option in the docker run command to specify a non - root user.
  • Keep Images Small: Minimize the number of layers in your Docker image. Combine RUN commands in the Dockerfile to reduce the number of intermediate layers. For example, instead of multiple RUN commands for installing packages, use a single RUN command with all the package installations.
RUN apt-get update && apt-get install -y package1 package2 package3

Conclusion

Building and managing containers using the Linux command line is a powerful skill for developers and system administrators. Through this blog, we have covered the fundamental concepts of containers, the installation of Docker, the process of building a container image using a Dockerfile, and how to manage running containers. By following common practices and best practices, you can create efficient, secure, and lightweight containers. With continued practice and exploration, you can master more advanced container management techniques and use containers to streamline your application development and deployment processes.

References