When working with Docker 1.1.2 on Ubuntu 12.04.4, many developers encounter issues with container naming. The command structure appears correct, yet containers still get randomized IDs. Let's examine why this happens and how to solve it.
The fundamental misunderstanding arises from confusing container naming with image IDs. The --name
flag assigns a human-readable name to your container instance, not to the base image. The image ID remains unchanged in the Docker registry.
# This names the container instance, not the image
sudo docker run -i -t --name=container1 ubuntu date
For proper container naming in Docker 1.1.2, consider these approaches:
# Basic named container
sudo docker run --name my_web_server -d nginx
# Named container with hostname
sudo docker run --name db_container -h db_host -d postgres
# Verify named containers
sudo docker ps -a --format "table {{.Names}}\t{{.Image}}\t{{.Status}}"
The "randomized" IDs you're seeing are actually the container IDs (not image IDs) that Docker generates when you don't specify a --name
. These appear in docker ps -a
output. The image ID remains constant in the background.
For more control over your Docker environment:
# Named container with auto-removal
sudo docker run --rm --name temp_container busybox echo "Hello"
# Named container with specific hostname
sudo docker run --name api_container -h api.example.com -d my_custom_image
# Linking named containers
sudo docker run --name web --link db:db -d web_app
- Always check for name conflicts with
docker ps -a
- Remember container names must be unique per Docker host
- Use
docker rename
to change names of existing containers - Combine with
--hostname
for complete naming control
When running Docker containers, many developers face confusion between setting the container name versus the image ID. The key distinction:
# This creates a container named "container1" from the ubuntu image
sudo docker run -i -t --name=container1 ubuntu date
# This would attempt to use an image with ID "container1" (which doesn't exist)
sudo docker run -i -t container1 ubuntu date
The apparent "randomization" occurs because:
- Each container gets a unique hexadecimal ID by default
--name
assigns a human-readable alias to this ID- The ID itself isn't meant to be user-controlled
For production environments, consider these patterns:
# Named container with explicit cleanup
docker run --name web-server -d nginx
docker stop web-server
docker rm web-server
# Named container with auto-removal
docker run --rm --name temp-container alpine echo "Hello"
# Using name prefixes for organization
docker run --name prod-db-01 -d postgres
docker run --name dev-cache-01 -d redis
Problem: "The container name is already in use"
# Solution 1: Remove the existing container
docker rm container1
# Solution 2: Use --rm for automatic cleanup
docker run --rm --name container1 ubuntu date
Problem: "I need to reference the container ID in scripts"
# Capture the ID programmatically
CONTAINER_ID=$(docker run -d --name my-container nginx)
echo "Container ID: $CONTAINER_ID"
For complex deployments:
# Using labels with names
docker run \
--name api-gateway \
-l com.mycompany.component=gateway \
-l com.mycompany.environment=production \
-d nginx
# Finding containers by name pattern
docker ps -a --filter="name=gateway"