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

The docker create command is used to create a new container from a given image without starting it. This allows you to configure the container's settings and prepare it for execution before actually running it. In this tutorial, we'll cover the basics of using the docker create command.

Syntax:

docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
  • OPTIONS: Optional arguments for the docker create command, such as container name, environment variables, and port mappings.
  • IMAGE: The name of the image from which to create the container.
  • COMMAND: (Optional) A command to be executed when the container is started.
  • ARG...: (Optional) Arguments to be passed to the command.

Examples:

  1. Create a container from an image:

    docker create my-image
    

    This command creates a new container from the my-image image. Docker will display the ID of the created container.

  2. Create a container with a custom name:

    docker create --name my-container my-image
    

    This command creates a new container with the specified name, my-container, using the my-image image.

  3. Create a container with environment variables:

    docker create -e SOME_ENV_VAR=value -e ANOTHER_ENV_VAR=value my-image
    

    This command creates a new container and sets the specified environment variables.

  4. Create a container with port mappings:

    docker create -p 8080:80 my-image
    

    This command creates a new container and maps port 8080 on the host to port 80 on the container.

  5. Create a container with a command and arguments:

    docker create my-image /bin/sh -c "echo 'Hello, World!'"
    

    This command creates a new container and specifies a command to be executed when the container is started.

After creating a container with the docker create command, you can start it with the docker start command, followed by the container's ID or name:

docker start my-container

This tutorial should give you a basic understanding of how to use the docker create command to create containers without starting them. This can be useful for configuring containers and preparing them for execution before actually running them.

  1. How to Use Docker Create Command:

    • Description: docker create initializes a new container based on an image but does not start it.
    • Code: Syntax of docker create:
      docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
      
  2. Creating Docker Containers with Docker Create:

    • Description: Use docker create to set up a new container from an image.
    • Code: Example of creating a container:
      docker create --name my_container nginx:latest
      
  3. Docker Container Configuration with Create Command:

    • Description: Configure container properties during creation using various options.
    • Code: Example of creating a container with specific options:
      docker create --name my_container -e MY_ENV_VAR=value -p 8080:80 nginx:latest
      
  4. Docker Create and Container ID:

    • Description: The docker create command returns the container ID, which can be used for further operations.
    • Code: Example of creating a container and retrieving its ID:
      container_id=$(docker create nginx:latest)
      
  5. Docker Create and Detached Mode:

    • Description: Use the -d or --detach flag to run the container in the background.
    • Code: Example of creating a detached container:
      docker create -d --name my_container nginx:latest
      
  6. Setting Container Names with Docker Create:

    • Description: Assign a custom name to the container using the --name option.
    • Code: Example of creating a container with a specific name:
      docker create --name my_custom_name nginx:latest
      
  7. Docker Create and Networking Options:

    • Description: Specify networking options like --network and --hostname during container creation.
    • Code: Example of creating a container with network options:
      docker create --network=my_network --hostname=my_host nginx:latest
      
  8. Docker Create and Resource Constraints:

    • Description: Use options like --cpus and --memory to set resource constraints during container creation.
    • Code: Example of creating a container with resource constraints:
      docker create --cpus=0.5 --memory=256m nginx:latest
      
  9. Docker Create and Environment Variables:

    • Description: Set environment variables within the container using the -e or --env option.
    • Code: Example of creating a container with environment variables:
      docker create -e MY_ENV_VAR=value nginx:latest
      
  10. Docker Create and Volume Mounts:

    • Description: Mount volumes into the container using the -v or --volume option.
    • Code: Example of creating a container with volume mounts:
      docker create -v /host/path:/container/path nginx:latest
      
  11. Docker Create and Port Bindings:

    • Description: Map host ports to container ports with the -p or --publish option.
    • Code: Example of creating a container with port bindings:
      docker create -p 8080:80 nginx:latest