Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Docker commands Guide – docker create with examples

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 createdocker run
Only creates the containerCreates and starts the container
Returns the container IDReturns the container’s output
Used for pre-configurationUsed for running containers
Requires docker start to run itNo need to start it manually

List of Common docker create Commands

CommandDescription
docker create ubuntuCreate a simple Ubuntu container
docker create --name my_app nginxCreate an NGINX container with a custom name
docker create -p 8080:80 nginxCreate a container with port mapping
docker create -e KEY=value alpineCreate a container with environment variables
docker create -v /host:/container busyboxCreate a container with a volume
docker create --restart always redisCreate a container with a restart policy
docker create --network custom_net alpineCreate 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:

  1. Pre-configure containers with volumes, environment variables, and restart policies before starting them.
  2. Use docker inspect to review container settings before running it.
  3. Use custom names (--name) for easier container management.
  4. Combine with docker start and docker exec for full control.

Common Errors and Solutions

  1. “Error: No such image”
    → Ensure the image exists or pull it using docker pull.
  2. “Duplicate container name”
    → Use a different name or remove the existing container with the same name: docker rm existing_container_name
  3. Container does not start after creation
    → Use docker start to run it: docker start CONTAINER_ID

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x