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.
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).
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.
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.
# 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
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"]
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.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.To see a list of currently running containers, use the docker ps
command:
docker ps
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>
Once a container is stopped, you can remove it using the docker rm
command:
docker rm <container_id>
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>
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>
python:3.9 - slim
for a Python application as shown in the example above. These images are well - maintained and optimized.--user
option in the docker run
command to specify a non - root user.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
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.