How to Create a Container in Docker: A Practical Step-by-Step Guide
If you’re new to Docker or just getting started with containerization, one of the first commands you need to master is how to create and run a container. Containers are lightweight, standalone executable packages that include everything needed to run a piece of software—code, runtime, system tools, libraries, and settings.
Today, I’ll walk you through the exact Docker command that helps you create containers effortlessly, with clear examples and explanations so you can start experimenting right away.
What is a Docker Container?
Before jumping into commands, let’s briefly clarify what a container is. Think of containers as isolated mini-systems running on top of your host OS kernel but with their own filesystem and processes. Unlike virtual machines, they don’t require booting an OS each time — they start instantly!
The Core Command: docker run
The easiest way to both create and start a container is by using:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
IMAGE
: The Docker image you want to create the container from (like a template).COMMAND
(optional): What command to run inside the container once it starts.OPTIONS
: Flags to customize behavior (e.g., ports, volumes).
Example #1: Run Hello World Container
To verify Docker is working and get comfortable with the syntax:
docker run hello-world
This pulls the small official hello-world
image from Docker Hub (if not already present), creates a temporary container from it, runs it which prints a friendly greeting message, then terminates.
Creating a Container Without Starting It Immediately
Sometimes you want to create a container but not start it right away. Use:
docker create IMAGE
Example:
docker create busybox
This command will create an instance of busybox
but leave it stopped so you can configure it or inspect it before running.
Running Containers in Interactive Mode
A very common use case for beginners is starting containers interactively so you can play inside them:
docker run -it ubuntu /bin/bash
Explanation:
-i
: Keep STDIN open.-t
: Allocate a pseudo-TTY.ubuntu
: The image name./bin/bash
: Command to run inside container.
This will drop you into the Ubuntu container’s bash shell prompt where you can execute Linux commands interactively.
Naming Your Containers
By default, Docker assigns random names like “friendly_morse.” To keep track more easily:
docker run --name mycontainer alpine echo "This is my alpine container"
You created a new container named mycontainer
that runs an Alpine Linux echo command and exits.
Mapping Ports for Web Apps
If your container hosts an app listening on some port (say 80), expose ports by mapping between host and container:
docker run -d -p 8080:80 nginx
Explanation:
-d
: Run detached (in background).-p 8080:80
: Maps port 8080 on your machine to port 80 in nginx container.
Now access http://localhost:8080 in your browser!
Summary of Commonly Used Flags When Creating Containers
Flag | Purpose |
---|---|
-d | Detached mode; runs container in background |
--name | Assign custom name |
-p host:cont | Port mapping |
-v host:cont | Volume mount for data persistence |
-it | Interactive/full terminal access |
Wrapping Up
Creating containers in Docker centers primarily around the fundamental docker run
command—with plenty of options based on what you want to achieve. Whether running short scripts inside ephemeral containers or spinning up production-ready detached services, mastering this command unlocks endless possibilities with Docker.
Here’s what you can try next:
- Create your own containers from different images (
mysql
,node
, etc.). - Play with options like environment variables (
-e
) or mounting volumes. - Combine multiple flags for more sophisticated scenarios.
Docker’s simplicity lies within its powerful CLI — once comfortable creating containers, you’ll quickly advance towards orchestrating complex deployments confidently.
Happy Dockering! 🚢🐳
Useful Links:
If this post helped you or if you have questions about containers or Docker in general — feel free to drop a comment below!