Docker commands follow a pattern where different verbs represent different lifecycle actions:
-
docker run: Creates AND starts a new container from an imagedocker run --name my-container nginx
This is like saying "create a container and start it running"
-
docker create: Creates a container but doesn't start itdocker create --name my-container nginx
This just prepares the container without starting it
-
docker start: Starts an existing (created or stopped) containerdocker start my-container
This is used to run a container that already exists
-
docker stop: Gracefully stops a running containerdocker stop my-container
This sends a SIGTERM signal, giving the container time to shut down cleanly
-
docker kill: Forcefully stops a container (like unplugging a computer)docker kill my-containerThis sends a SIGKILL signal, forcing immediate termination
-
docker restart: Stops and then starts a containerdocker restart my-container
This is equivalent to a
stopfollowed by astart -
docker rm: Removes a container (must be stopped first, unless forced with-f)docker rm my-container
This deletes the container entirely
-
docker imagesordocker image ls: Lists all imagesdocker images
-
docker rmiordocker image rm: Removes an imagedocker rmi nginx
-
docker pull: Downloads an image from a registry without creating a containerdocker pull nginx
-
docker build: Creates a new image from a Dockerfiledocker build -t my-image .
-
docker ps: Shows running containersdocker ps
-
docker ps -a: Shows all containers (running and stopped)docker ps -a
-
docker logs: Shows logs from a containerdocker logs my-container
-
docker inspect: Shows detailed information about a containerdocker inspect my-container
The most direct solution is to:
-
Find the container with the conflicting name:
docker ps -a | grep your-container-name -
Remove it:
docker rm your-container-name
-
Then try your
docker runcommand again
Or, you can use the force flag to automatically replace the existing container:
docker run --force --name your-container-name your-image