Getting Started with Docker on Linux: Basics

Getting started with Docker on Linux allows you to utilize containerization technology to package, distribute, and run applications efficiently. Here's a guide to help you understand the basics and start using Docker on Linux:

1. Installation:
   - Docker Engine is available for various Linux distributions. You can install Docker using your distribution's package manager or by downloading the official Docker package.
   - For Debian/Ubuntu-based systems:
     ```
     sudo apt update
     sudo apt install docker.io
     ```
   - For Fedora/RHEL-based systems:
     ```
     sudo yum install docker
     ```

2. Start Docker Service:
   - After installation, start the Docker service and enable it to run at boot time:
     ```
     sudo systemctl start docker
     sudo systemctl enable docker
     ```

3. Verify Installation:
   - Check the Docker version to ensure it's installed correctly:
     ```
     docker --version
     ```

4. Pulling Docker Images:
   - Docker images are templates for containers. You can pull images from Docker Hub, a public repository of Docker images, or other image registries.
   - For example, to pull the official Ubuntu image:
     ```
     docker pull ubuntu
     ```

5. Running Containers:
   - Start a container from a Docker image using the `docker run` command. For example, to run a basic Ubuntu container:
     ```
     docker run -it ubuntu
     ```
   - This command will start an interactive terminal session (-it) in an Ubuntu container.

6. Basic Container Operations:
   - Once inside a container, you can perform various operations, such as installing software, running commands, and modifying files.
   - To exit the container without stopping it, use the `exit` command.

7. Listing Containers:
   - View the list of running containers using the `docker ps` command. To see all containers (including stopped ones), add the `-a` flag:
     ```
     docker ps
     docker ps -a
     ```

8. Stopping and Removing Containers:
   - Stop a running container using its ID or name:
     ```
     docker stop
     ```
   - Remove a stopped container:
     ```
     docker rm
     ```

9. Creating Dockerfiles:
   - Dockerfiles are text files that contain instructions for building Docker images. You can create custom Docker images by writing Dockerfiles.
   - Start by creating a new directory and a Dockerfile inside it. Here's a simple example Dockerfile for a Node.js application:
     ```Dockerfile
     FROM node:latest
     WORKDIR /app
     COPY . .
     RUN npm install
     CMD ["node", "app.js"]
     ```

10. Building Custom Images:
    - Build a Docker image using the `docker build` command. Navigate to the directory containing the Dockerfile and run:
      ```
      docker build -t my-node-app .
      ```
    - This command will build an image named `my-node-app` using the Dockerfile in the current directory (`.`).

11. Running Containers from Custom Images:
    - Once you've built a custom image, you can run containers from it just like you did with pre-built images:
      ```
      docker run -d my-node-app
      ```

12. Publishing Images to Docker Hub (Optional):
    - If you've created a useful Docker image, you can publish it to Docker Hub or another image registry to share it with others:
      ```
      docker login
      docker tag my-node-app username/my-node-app
      docker push username/my-node-app
      ```

13. Exploring Docker Compose (Optional):
    - Docker Compose is a tool for defining and running multi-container Docker applications. It uses YAML files to configure the application's services and dependencies.
    - Install Docker Compose and use it to manage complex multi-container setups.

By following these steps, you can get started with Docker on Linux and begin leveraging containerization technology to simplify application deployment, management, and scaling. Experiment with different Docker features and workflows to become proficient in container-based development.


Was this article helpful?

mood_bad Dislike 0
mood Like 0
visibility Views: 1397