Arshad Mehmood

Search the blog

    Today I Learned

    Multiple Ways to Run a Docker Container

    docker

    Docker provides several methods to run containers. Here are some common ways:

    Using docker run

    The docker run command is the most straightforward way to start a container.

    docker run -d --name my_container my_image

    This command runs a Docker container in detached mode. Here’s a breakdown of the options used:

    • docker run: This is the command to create and start a new Docker container.
    • -d: This flag runs the container in detached mode, meaning it runs in the background and does not block your terminal. The terminal can be used for something else.

    Example usage:

    docker run -d --name web_server nginx

    This would run an Nginx web server container in the background with the name web_server.

    Using Docker Compose

    Docker Compose allows you to define and run multi-container Docker applications.

    version: '3'
    services:
        web:
            image: my_image
            ports:
                - "5000:5000"

    Run the application with:

    docker-compose up -d

    Using Docker Desktop: Pulling an Image and Running a Container

    Docker Desktop provides a user-friendly interface to manage your Docker containers and images. To pull an image and run a container using Docker Desktop:

    1. Open Docker Desktop: Launch the Docker Desktop application on your machine.
    2. Navigate to the Images tab: This tab allows you to search for and pull Docker images.
    3. Pull an image:
      • Click on the Pull button.
      • Enter the image name (e.g., my_image) and click Pull.
    4. Navigate to the Containers/Apps tab: This tab allows you to view and manage your running containers.
    5. Run a container:
      • Click on the + button or Run button.
      • Select the image you pulled (e.g., my_image).
      • Configure any additional settings.
      • Click Run to start the container.

    These steps guide you through pulling an image and running a container using Docker Desktop’s graphical interface.

    Using Kubernetes

    Kubernetes can manage containerized applications across a cluster of machines.

    apiVersion: v1
    kind: Pod
    metadata:
        name: my-pod
    spec:
        containers:
        - name: my-container
            image: my_image

    Apply the configuration with:

    kubectl apply -f pod.yaml