Skip to content

Instantly share code, notes, and snippets.

@devarda
Created March 15, 2025 19:34
Show Gist options
  • Select an option

  • Save devarda/6eca82a4f9aa0e14c50a4287abf36ed2 to your computer and use it in GitHub Desktop.

Select an option

Save devarda/6eca82a4f9aa0e14c50a4287abf36ed2 to your computer and use it in GitHub Desktop.
Docker Core Commands Explained

Docker Core Commands Explained

Docker commands follow a pattern where different verbs represent different lifecycle actions:

Container Lifecycle Commands

  • docker run: Creates AND starts a new container from an image

    docker 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 it

    docker create --name my-container nginx

    This just prepares the container without starting it

  • docker start: Starts an existing (created or stopped) container

    docker start my-container

    This is used to run a container that already exists

  • docker stop: Gracefully stops a running container

    docker 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-container

    This sends a SIGKILL signal, forcing immediate termination

  • docker restart: Stops and then starts a container

    docker restart my-container

    This is equivalent to a stop followed by a start

  • docker rm: Removes a container (must be stopped first, unless forced with -f)

    docker rm my-container

    This deletes the container entirely

Image Management Commands

  • docker images or docker image ls: Lists all images

    docker images
  • docker rmi or docker image rm: Removes an image

    docker rmi nginx
  • docker pull: Downloads an image from a registry without creating a container

    docker pull nginx
  • docker build: Creates a new image from a Dockerfile

    docker build -t my-image .

Container Information Commands

  • docker ps: Shows running containers

    docker ps
  • docker ps -a: Shows all containers (running and stopped)

    docker ps -a
  • docker logs: Shows logs from a container

    docker logs my-container
  • docker inspect: Shows detailed information about a container

    docker inspect my-container

Getting Past "Image Name Already Exists" Error

The most direct solution is to:

  1. Find the container with the conflicting name:

    docker ps -a | grep your-container-name
  2. Remove it:

    docker rm your-container-name
  3. Then try your docker run command again

Or, you can use the force flag to automatically replace the existing container:

docker run --force --name your-container-name your-image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment