Docker on Linux enables you to use containerization technology to package, distribute, and run applications efficiently. This guide covers the essential steps to install Docker, work with containers and images, and create your own custom Docker images.

How to Get Started

  1. Step 1: Install Docker Engine

    Docker Engine is available for various Linux distributions. Install it using your distribution's package manager.

    For Debian/Ubuntu-based systems:

    sudo apt update
    sudo apt install docker.io

    For Fedora/RHEL-based systems:

    sudo yum install docker
  2. Step 2: Start and Enable 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. Step 3: Verify Installation

    Check the Docker version to ensure it's installed correctly:

    docker --version
  4. Step 4: Pull 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. Step 5: Run Your First Container

    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. Once inside, 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.

  6. Step 6: List Running 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
  7. Step 7: Stop and Remove Containers

    Stop a running container using its ID or name:

    docker stop <container_id_or_name>

    Remove a stopped container:

    docker rm <container_id_or_name>
  8. Step 8: Create a Dockerfile

    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:

    FROM node:latest
    WORKDIR /app
    COPY . .
    RUN npm install
    CMD ["node", "app.js"]
  9. Step 9: Build 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 (.).

  10. Step 10: Run 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
  11. Step 11: Publish 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
  12. Step 12: Explore 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.