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 run command

The docker run command is used to create and start a new Docker container from an image. It is one of the most frequently used Docker commands, allowing you to launch containers with various configurations and options. In this tutorial, we'll cover the basics of using the docker run command.

Syntax:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  • OPTIONS: Additional options that can be used with the docker run command.
  • IMAGE: The name or ID of the image you want to run.
  • COMMAND: The command to run within the container (optional).
  • ARG: Arguments for the command (optional).

Common options:

  • --name: Assign a name to the container.
  • -d or --detach: Run the container in the background and print the container ID.
  • --rm: Automatically remove the container when it exits.
  • -p or --publish: Publish a container's port(s) to the host (format: hostPort:containerPort).
  • -e or --env: Set environment variables in the container (format: KEY=VALUE).
  • -v or --volume: Bind mount a volume (format: hostPath:containerPath[:<options>]).
  • --network: Connect the container to a specific network.

Examples:

  1. Run a container with the latest Ubuntu image:

    docker run -it ubuntu
    

    This command will create and start a container using the latest Ubuntu image, attaching your terminal to the container's standard input, output, and error streams.

  2. Run a container with a specific name:

    docker run --name my_container -it ubuntu
    

    This command will create and start a container named my_container using the latest Ubuntu image.

  3. Run a container in detached mode:

    docker run -d --name my_container ubuntu sleep 100
    

    This command will create and start a container named my_container, running the sleep 100 command in the background.

  4. Run a container and remove it automatically when it exits:

    docker run --rm -it ubuntu
    
  5. Run a container and publish a port:

    docker run -d -p 8080:80 myrepository/myimage
    

    This command will create and start a container using the myrepository/myimage image, publishing the container's port 80 to the host's port 8080.

  6. Run a container with environment variables:

    docker run -e "ENV_KEY=env_value" -it ubuntu
    
  7. Run a container with a bind-mounted volume:

    docker run -v /path/on/host:/path/in/container -it ubuntu
    

    This command will create and start a container using the latest Ubuntu image, with /path/on/host on the host machine mounted to /path/in/container inside the container.

  8. Run a container connected to a specific network:

    docker run --network my_network -it ubuntu
    

These examples should give you a basic understanding of how to use the docker run command to create and start Docker containers. You can combine different options to configure your containers as needed.

  1. How to Use Docker run Command:

    • Description: The docker run command is used to create and start Docker containers from images.

    • Code Example:

      docker run image_name
      
  2. Launching Containers with Docker run:

    • Description: docker run creates and starts a container based on a specified image.

    • Code Example:

      docker run ubuntu
      
  3. Docker run Command Options and Flags:

    • Description: docker run has various options and flags for configuring container behavior, such as specifying environment variables or mounting volumes.

    • Code Example:

      docker run -e VAR=value -v /host/path:/container/path image_name
      
  4. Running Detached Containers with Docker run:

    • Description: The -d flag runs containers in detached mode, allowing the terminal to be available for other commands.

    • Code Example:

      docker run -d image_name
      
  5. Docker run and Container Names:

    • Description: Assign a name to a container using the --name flag for easier identification.

    • Code Example:

      docker run --name my_container image_name
      
  6. Mounting Volumes with Docker run:

    • Description: Use the -v flag to mount volumes between the host and the container.

    • Code Example:

      docker run -v /host/path:/container/path image_name
      
  7. Specifying Ports with Docker run:

    • Description: Use the -p flag to map host ports to container ports.

    • Code Example:

      docker run -p 8080:80 image_name
      
  8. Environment Variables in Docker run:

    • Description: Set environment variables within the container using the -e flag.

    • Code Example:

      docker run -e VAR=value image_name
      
  9. Interactive Mode with Docker run:

    • Description: Run containers in interactive mode using the -it flags, allowing interaction with the container's terminal.

    • Code Example:

      docker run -it image_name /bin/bash
      
  10. Detached Mode vs Interactive Mode in Docker run:

    • Description: Detached mode (-d) runs containers in the background, while interactive mode (-it) allows direct interaction.

    • Code Example (Detached):

      docker run -d image_name
      
    • Code Example (Interactive):

      docker run -it image_name /bin/bash
      
  11. Docker run and Image Tags:

    • Description: Specify a particular image version or tag using the :tag notation.

    • Code Example:

      docker run image_name:latest
      
  12. Docker run and Container Networking:

    • Description: Docker automatically creates a bridge network for containers, allowing communication between them.

    • Code Example:

      docker run --network=my_network image_name
      
  13. Security Considerations with Docker run:

    • Description: Be mindful of security practices, avoid running containers as root, and restrict unnecessary capabilities.

    • Code Example:

      docker run --user=1000 image_name
      
  14. Automating Container Deployment with Docker run:

    • Description: Automate container deployment by scripting docker run commands for consistency.

    • Code Example (Script):

      # deploy_container.sh
      docker run -d -p 8080:80 --name my_container image_name