Here’s a complete tutorial on docker create
, including its purpose, how it differs from docker run
, and a comprehensive list of examples.
What is docker create
?
docker create
is used to create a new container from a specified image without starting it immediately. This command is useful when you want to configure or inspect a container before running it.
Key Features:
- Only creates the container; does not run it.
- Useful for preparing containers with custom configurations.
- Returns the container ID after creation.
- Works with the same options as
docker run
.
Basic Syntax
docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
Common Options:
--name
: Assign a custom name to the container.-p
: Map ports from the container to the host.-v
: Mount a volume or bind a host directory.-e
: Set environment variables.--restart
: Define the container’s restart policy.-it
: Allocate a pseudo-TTY and keep STDIN open (interactive mode).
Examples of docker create
1. Create a Simple Container
docker create ubuntu
This creates a container from the ubuntu
image without running it.
2. Create a Container with a Custom Name
docker create --name my_ubuntu_container ubuntu
This creates a container named my_ubuntu_container
.
3. Create a Container with Port Mapping
docker create -p 8080:80 nginx
This creates a container with port 8080
on the host mapped to port 80
in the container.
4. Create a Container with Environment Variables
docker create -e APP_ENV=production -e DEBUG=false my_app
This creates a container with two environment variables: APP_ENV=production
and DEBUG=false
.
5. Create a Container with a Volume
docker create -v /host/data:/container/data my_app
This mounts the host directory /host/data
to /container/data
in the container.
6. Create a Container with Restart Policy
docker create --restart always redis
This creates a redis
container with a restart policy set to always
, meaning it will restart automatically if it stops.
7. Create a Detached Interactive Container
docker create -it ubuntu
The container is created in interactive mode but remains stopped until started.
8. Create a Container with a Specific Command
docker create ubuntu echo "Hello, Docker!"
The container is configured to run echo "Hello, Docker!"
when started.
9. Create a Container with Network Settings
docker create --network my_custom_network nginx
This creates a container attached to the my_custom_network
network.
10. Create a Container with a Hostname
docker create --hostname my_custom_host alpine
This creates an alpine
container with a custom hostname my_custom_host
.
Starting a Container Created with docker create
Once a container is created with docker create
, you can start it with:
docker start CONTAINER_ID
For example:
docker start my_ubuntu_container
Inspecting a Created Container
You can inspect the configuration of a created container using:
docker inspect CONTAINER_ID
Difference Between docker create
and docker run
docker create | docker run |
---|---|
Only creates the container | Creates and starts the container |
Returns the container ID | Returns the container’s output |
Used for pre-configuration | Used for running containers |
Requires docker start to run it | No need to start it manually |
List of Common docker create
Commands
Command | Description |
---|---|
docker create ubuntu | Create a simple Ubuntu container |
docker create --name my_app nginx | Create an NGINX container with a custom name |
docker create -p 8080:80 nginx | Create a container with port mapping |
docker create -e KEY=value alpine | Create a container with environment variables |
docker create -v /host:/container busybox | Create a container with a volume |
docker create --restart always redis | Create a container with a restart policy |
docker create --network custom_net alpine | Create a container on a custom network |
docker create ubuntu echo "Hello, world!" | Create a container with a specific command |
Best Practices for Using docker create
:
- Pre-configure containers with volumes, environment variables, and restart policies before starting them.
- Use
docker inspect
to review container settings before running it. - Use custom names (
--name
) for easier container management. - Combine with
docker start
anddocker exec
for full control.
Common Errors and Solutions
- “Error: No such image”
→ Ensure the image exists or pull it usingdocker pull
. - “Duplicate container name”
→ Use a different name or remove the existing container with the same name:docker rm existing_container_name
- Container does not start after creation
→ Usedocker start
to run it:docker start CONTAINER_ID
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND