How to Start a Stopped Docker Container with a Different Command
TLDR
You can't directly change the command of a stopped Docker container when restarting it with docker start. Instead, create a new container from the same image with a different command or entrypoint. Use docker run --entrypoint or docker commit for advanced cases.
Why Can't You Change the Command with docker start?
When you create a container with docker run, the command and entrypoint are set at creation. docker start always uses the original command—you can't override it for an existing container.
Option 1: Create a New Container with a Different Command
The most reliable way is to start a new container from the same image, specifying the new command:
docker run -it my-image /bin/bash
Or, to override the entrypoint:
docker run -it --entrypoint /bin/sh my-image
This gives you a fresh container with your chosen command.
Option 2: Use docker commit to Save Changes (Advanced)
If you need to preserve changes made in the stopped container (like files or installed packages), you can commit it to a new image:
docker commit my-container my-temp-image
Then run a new container from that image with a different command:
docker run -it my-temp-image /bin/bash
Option 3: Use docker exec for Running Containers
If the container is running, you can start a new process inside it:
docker exec -it my-container /bin/bash
But this doesn't work if the container is stopped.
Best Practices
- Use
docker runwith a new command for most cases. - Use
docker commitonly if you need to preserve changes from the stopped container. - For debugging, override the entrypoint or command as needed.
- Clean up old containers and images to avoid clutter.
Conclusion
You can't change the command of a stopped Docker container when restarting it, but you can create a new container from the same image (or a committed image) with any command you need. This gives you flexibility for debugging, recovery, or running alternate workflows.
We earn commissions when you shop through the links below.
DigitalOcean
Cloud infrastructure for developers
Simple, reliable cloud computing designed for developers
DevDojo
Developer community & tools
Join a community of developers sharing knowledge and tools
Acronis
The most secure backup
Acronis: the most secure backup solution for your data
Pluralsight
Technology skills platform
Expert-led courses in software development, IT ops, data, and cybersecurity
Want to support DevOps Daily and reach thousands of developers?
Become a SponsorFound an issue?