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
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:
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.
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.
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.
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.
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.
How to Use Docker Create Command:
docker create
initializes a new container based on an image but does not start it.docker create
:docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
Creating Docker Containers with Docker Create:
docker create
to set up a new container from an image.docker create --name my_container nginx:latest
Docker Container Configuration with Create Command:
docker create --name my_container -e MY_ENV_VAR=value -p 8080:80 nginx:latest
Docker Create and Container ID:
docker create
command returns the container ID, which can be used for further operations.container_id=$(docker create nginx:latest)
Docker Create and Detached Mode:
-d
or --detach
flag to run the container in the background.docker create -d --name my_container nginx:latest
Setting Container Names with Docker Create:
--name
option.docker create --name my_custom_name nginx:latest
Docker Create and Networking Options:
--network
and --hostname
during container creation.docker create --network=my_network --hostname=my_host nginx:latest
Docker Create and Resource Constraints:
--cpus
and --memory
to set resource constraints during container creation.docker create --cpus=0.5 --memory=256m nginx:latest
Docker Create and Environment Variables:
-e
or --env
option.docker create -e MY_ENV_VAR=value nginx:latest
Docker Create and Volume Mounts:
-v
or --volume
option.docker create -v /host/path:/container/path nginx:latest
Docker Create and Port Bindings:
-p
or --publish
option.docker create -p 8080:80 nginx:latest