Getting Started with Docker on Linux Allthinglinux.com, November 7, 2024November 7, 2024 Docker has revolutionized the way applications are built, shipped, and deployed by offering a standardized environment in which applications can run. As a containerization tool, Docker enables developers to package applications and their dependencies into portable containers, ensuring consistency across various computing environments. This guide covers the essentials of Docker, including its setup on Linux, core commands, and use cases, to help you start working with Docker efficiently. Using Vi and Nano: Text Editing on Linux Table of Contents What is Docker and Why Use It? Installing Docker on Linux Key Docker Concepts Working with Docker Images Running and Managing Docker Containers Docker Networking Basics Docker Volumes for Persistent Storage Docker Compose: Managing Multi-Container Applications Basic Docker Use Cases and Examples Conclusion 1. What is Docker and Why Use It? Docker is an open-source platform for building, shipping, and running applications inside isolated environments called containers. Unlike virtual machines, which require separate operating systems, Docker containers share the host OS’s kernel, making them lightweight, fast, and resource-efficient. Why Use Docker? Portability: Docker containers can run consistently across different environments, reducing “it works on my machine” issues. Isolation: Containers run in isolated environments, which prevents conflicts between dependencies. Efficiency: Docker containers are lightweight, making them more resource-efficient than virtual machines. Scalability: Containers can be easily scaled, deployed, and orchestrated using tools like Docker Compose, Docker Swarm, or Kubernetes. 2. Installing Docker on Linux Let’s start by installing Docker on a Linux system. Most Linux distributions support Docker, including Ubuntu, CentOS, Fedora, and Debian. Below are the installation steps for Ubuntu, one of the most popular Linux distributions. Installing Docker on Ubuntu Update Packages: sudo apt update Install Dependencies: sudo apt install apt-transport-https ca-certificates curl software-properties-common Add Docker GPG Key and Repository: curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg sudo add-apt-repository "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" Install Docker: sudo apt update sudo apt install docker-ce Verify Installation:Run the following command to check if Docker is installed correctly: sudo docker --version Enable and Start Docker: sudo systemctl enable docker sudo systemctl start docker Now, Docker should be installed and running on your Linux system. 3. Key Docker Concepts Before diving into commands, let’s go over a few essential Docker concepts. Image: A Docker image is a lightweight, stand-alone, executable package containing everything needed to run a piece of software. Container: A container is a runtime instance of a Docker image. It includes everything needed to run the application—code, dependencies, and environment configurations. Docker Hub: Docker Hub is a public registry for Docker images, where you can find and share images. Dockerfile: A Dockerfile is a text document containing all the commands needed to assemble an image. 4. Working with Docker Images Docker images are the foundation of Docker containers. Let’s go over some essential commands for working with Docker images. Pulling an Image Docker Hub hosts thousands of images that you can pull and use. To pull an image, use: sudo docker pull ubuntu:latest This command downloads the latest version of the Ubuntu image from Docker Hub. Listing Images To list all images on your system: sudo docker images Removing an Image To remove an image, use: sudo docker rmi image_name_or_id 5. Running and Managing Docker Containers With Docker images on hand, you can now run containers. Below are some basic commands to manage containers. Running a Container The following command starts a container from the Ubuntu image: sudo docker run -it ubuntu The -it option attaches an interactive terminal to the container, allowing you to enter commands directly. Listing Running Containers To view all active containers, use: sudo docker ps To see all containers (including stopped ones): sudo docker ps -a Stopping a Container To stop a container, use: sudo docker stop container_id Removing a Container To delete a container, use: sudo docker rm container_id 6. Docker Networking Basics Docker networking allows containers to communicate with each other and with external networks. Docker offers several network types, including: Bridge: The default network, used to allow containers on the same host to communicate. Host: Shares the host’s networking namespace, removing network isolation. None: Disables networking for the container. You can list all networks using: sudo docker network ls 7. Docker Volumes for Persistent Storage Docker containers are ephemeral, meaning data is lost once they stop. Docker volumes provide a way to persist data beyond the container’s lifecycle. Creating a Volume To create a volume, use: sudo docker volume create my_volume Mounting a Volume You can mount a volume when running a container to persist data: sudo docker run -d -v my_volume:/data ubuntu In this example, /data in the container will store data in my_volume on the host, ensuring it persists even if the container is removed. 8. Docker Compose: Managing Multi-Container Applications For applications that require multiple containers (such as web servers and databases), Docker Compose is a valuable tool. Docker Compose allows you to define and manage multiple containers in a docker-compose.yml file. Installing Docker Compose On most systems, you can install Docker Compose using: sudo apt install docker-compose Creating a Docker Compose File A typical docker-compose.yml file might look like this: version: '3' services: web: image: nginx ports: - "80:80" database: image: mysql environment: MYSQL_ROOT_PASSWORD: example Running Docker Compose With the docker-compose.yml file saved, you can start all defined services with: sudo docker-compose up -d This command runs Nginx as the web server and MySQL as the database service. 9. Basic Docker Use Cases and Examples Now that you understand Docker basics, let’s explore a few practical use cases. Example 1: Running a Web Server in Docker To deploy a simple Nginx web server in a Docker container: sudo docker run -d -p 80:80 nginx This command launches Nginx in detached mode (-d) and maps port 80 on the container to port 80 on the host. Example 2: Creating a Custom Docker Image You can create your own Docker images using a Dockerfile. Here’s an example Dockerfile: # Use the official Python image FROM python:3.8 # Set the working directory WORKDIR /app # Copy the current directory contents into the container COPY . /app # Install dependencies RUN pip install -r requirements.txt # Run the application CMD ["python", "app.py"] Build the image using: sudo docker build -t my_python_app . Example 3: Setting Up a WordPress Environment with Docker Compose With Docker Compose, you can set up WordPress with a MySQL database: version: '3' services: wordpress: image: wordpress ports: - "8080:80" environment: WORDPRESS_DB_HOST: db WORDPRESS_DB_USER: example_user WORDPRESS_DB_PASSWORD: example_pass WORDPRESS_DB_NAME: example_db db: image: mysql:5.7 environment: MYSQL_DATABASE: example_db MYSQL_USER: example_user MYSQL_PASSWORD: example_pass MYSQL_ROOT_PASSWORD: example_root_pass Run it with: sudo docker-compose up -d This setup will launch WordPress at http://localhost:8080 and a MySQL database in the backend. 10. Conclusion Docker on Linux is a powerful tool for developers and sysadmins, making it easier to manage applications, create consistent environments, and enable seamless scaling. By understanding Docker basics—from images and containers to volumes and networking—you can start building, deploying, and managing applications in a fast, reliable way. Whether you’re managing single applications or orchestrating complex multi-container environments, Docker’s flexibility and ease of use make it an essential part of the modern development toolkit. This article introduces Docker on Linux, equipping you with foundational knowledge and hands-on examples to get started effectively. Enjoy your journey into containerization! Linux Basics Docker
Linux Basics Basic Linux Networking Tools and Commands October 22, 2024October 22, 2024 Networking is one of the most critical aspects of modern computing. It facilitates communication between… Read More
Linux Basics Linux File System Mounting Explained November 8, 2024November 8, 2024 File System Mounting Linux is a highly flexible operating system that allows users to manage… Read More
Linux Basics Introduction to Shell Scripting in Linux October 20, 2024October 20, 2024 Linux has become one of the most powerful and widely used operating systems in the… Read More