Docker Tutorial

Docker Installation

How to use Docker

Docker Instance

Docker Container Lifecycle Command

Docker Container Operation Command

Docker Container rootfs Command

Docker Mirror Repository Command

Docker Local Image Management Command

Docker info|version Command

Docker install CentOS

In this tutorial, you'll learn how to create a Docker container with CentOS, a popular Linux distribution. We'll use the official CentOS image provided by the Docker community.

  1. Prerequisites:

    • Install Docker on your system
  2. Pull the official CentOS image:

    To pull the official CentOS image, run the following command:

    docker pull centos
    

    This will download the latest version of the CentOS image to your system.

  3. Create a new container with CentOS:

    To create a new Docker container running CentOS, use the docker run command. We'll run the container in interactive mode with a TTY attached to it:

    docker run -it --name centos-container centos
    

    This command creates a new container named centos-container and starts a bash session inside the container. You should now see the container's command prompt.

  4. Explore the CentOS container:

    With the bash session running inside the container, you can explore the CentOS environment and run commands as you would on a regular CentOS system. For example, you can check the CentOS version by running:

    cat /etc/centos-release
    

    You can also update the system packages using yum:

    yum update -y
    

    And install new packages:

    yum install -y package_name
    

    Replace package_name with the name of the package you want to install.

  5. Exit the CentOS container:

    When you're done exploring the CentOS container, you can exit the bash session by typing exit or pressing Ctrl+D. This will stop the container.

  6. Start the CentOS container again:

    If you want to start the stopped CentOS container again, use the docker start command followed by the docker attach command:

    docker start centos-container
    docker attach centos-container
    

    This will start the centos-container and attach your terminal to the running container, allowing you to interact with the container again.

In this tutorial, we covered how to create a Docker container with CentOS, interact with the container using a bash session, and manage the CentOS environment inside the container. You can now use this containerized CentOS environment for development, testing, or deployment purposes.

  1. Running CentOS in Docker Container:

    • Description: Run CentOS in a Docker container to create isolated and portable environments.
    • Code Example:
      docker run -it --name my-centos-container centos:latest
      
  2. How to Create a Docker Image with CentOS:

    • Description: Create a custom Docker image with CentOS installed using a Dockerfile.
    • Code Example (Dockerfile):
      FROM centos:latest
      # Add custom configurations or packages
      
  3. Docker-Compose CentOS Installation:

    • Description: Use Docker Compose to define and run multi-container Docker applications, including CentOS.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        centos:
          image: centos:latest
      
  4. CentOS Dockerfile Example:

    • Description: A Dockerfile example for creating a custom CentOS image with additional configurations or software.
    • Code Example (Dockerfile):
      FROM centos:latest
      RUN yum install -y my-package
      
  5. Setting Up CentOS in a Docker Environment:

    • Description: Configure CentOS settings, such as environment variables or user accounts, within a Docker environment.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        centos:
          image: centos:latest
          environment:
            - MY_VARIABLE=value
      
  6. Configuring CentOS in Docker-Compose:

    • Description: Customize CentOS configurations using Docker Compose for a multi-container environment.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        centos:
          image: centos:latest
          volumes:
            - ./my-config:/etc/my-config
      
  7. Docker CentOS Image Customization:

    • Description: Customize the CentOS image by installing additional packages or configurations using a Dockerfile.
    • Code Example (Dockerfile):
      FROM centos:latest
      RUN yum install -y my-package
      
  8. Docker CentOS Volume Mounts:

    • Description: Use Docker volumes to mount external directories or configuration files into CentOS containers for persistence or customization.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        centos:
          image: centos:latest
          volumes:
            - ./my-data:/data
      
  9. Custom CentOS Image Creation in Docker:

    • Description: Create a custom CentOS image with specific configurations and software installations tailored to your application.
    • Code Example (Dockerfile):
      FROM centos:latest
      RUN yum install -y my-package
      COPY my-config /etc/my-config
      
  10. Docker Swarm and CentOS Service:

    • Description: Deploy CentOS as a service in a Docker Swarm to scale and manage multiple CentOS containers.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        centos:
          image: centos:latest
          deploy:
            replicas: 3
            # Other Swarm deployment configurations
      
  11. Troubleshooting CentOS in Docker:

    • Description: Troubleshoot CentOS-related issues in Docker by examining logs, checking configurations, and verifying container settings.
    • Code Example:
      docker logs <centos_container_id_or_name>