The difference between `docker run` and `docker start` depends on whether you want to create a new container from an image or resume an already-created but stopped one. Use `docker start` when you have an existing container ID or name and simply need to restart it, while `docker run` launches a new container from an image if it doesn’t already exist.

Both commands are fundamental to managing Docker containers, but they serve distinct purposes. The `docker start` command is for quickly resuming paused or stopped containers, while `docker run` is the primary way to deploy new services because it also pulls images, sets up volumes, and assigns networking—unless you explicitly specify `--rm` or `--detach`. For production environments, `docker start` is often safer as it avoids image downloads or port conflicts.

What You'll Need

  • A Docker environment installed (Linux, macOS, or Windows with WSL 2 or Docker Desktop). Verify installation by running docker --version in your terminal.
  • A running Docker daemon (not just the CLI). If the daemon isn’t running, Docker commands will fail with errors like "Got permission denied while trying to connect to the Docker daemon."
  • Access to Docker images (public or private registries). If using private registries, ensure you’re logged in with docker login.
  • Basic familiarity with container IDs/names, which you can list with docker ps -a.

Step-by-Step

1. Check Existing Containers

Before using either command, confirm which containers are available. Run:

docker ps -a

This lists all containers with their statuses:

  • Up (running)
  • Exited (stopped)
  • Created (never started)

Note the CONTAINER ID or NAMES of containers you want to start.

2. Using `docker start`

To restart a stopped container, use:

docker start [container_id_or_name]

Example: If your container is named my_web_server, run:

docker start my_web_server

Key flags:

  • -a or --attach: Attach to the container’s logs (like docker attach). Useful for debugging.
  • -d or --detach: Prevents immediate attachment (default behavior).
  • --interactive (-i): Keeps STDIN open (useful for interactive sessions).

3. Using `docker run`

To create a new container from an image, use:

docker run [options] image_name

Example: Start a Redis server from the redis image with a container name redis_db:

docker run --name redis_db -d redis

Common flags for `docker run`:

  • -d or --detach: Run in detached mode (background).
  • --name: Assign a custom name to the container.
  • -p or --publish: Map host ports to container ports (e.g., -p 8080:80).
  • -v or --volume: Mount a host directory into the container (e.g., -v /host/path:/container/path).
  • --rm: Automatically remove the container after it exits.
  • --restart: Set restart policy (e.g., unless-stopped or always).

4. Verify the Container Is Running

After starting/running, check the container status:

docker ps

This shows only running containers. To see all containers (including stopped ones), use:

docker ps -a

Inspect logs with:

docker logs [container_id_or_name]

Common Mistakes

  • Using `docker start` on a non-existent container.

    Result: Error like Error response from daemon: Container X not found. Always verify the container exists with docker ps -a first.

  • Forgetting to detach (`-d`) when running services.

    Result: Your terminal session blocks, and you lose connection if the container crashes. Use docker run -d for background services.

  • Not specifying ports or volumes in `docker run`.

    Result: Your container runs but isn’t accessible. Always map ports (-p) and volumes (-v) for persistent data or external access.

  • Assuming `docker run` always pulls the latest image.

    Result: Slow deployment if the image isn’t cached locally. Specify a tag (e.g., redis:6.2) to avoid surprises.

  • Ignoring resource limits.

    Result: Containers consume excessive CPU/memory, slowing down your host. Use --memory and --cpus flags to set limits.

  • Running containers as root without best practices.

    Result: Security risks. Use a non-root user inside the container via USER in a Dockerfile or runtime flag.

Tips

  • Combine `docker start` with `docker attach` for debugging. Example: docker start my_container && docker attach my_container lets you interact with a paused container’s process.
  • Use `docker-compose` for complex setups. If managing multiple containers, docker-compose up handles dependencies automatically—avoid manually running each container.
  • Alias `docker ps -a` for quick checks. Add this to your shell config (~/.bashrc or ~/.zshrc):
    alias dl='docker ps -a'
    Then use dl to list all containers anytime.
  • Prefer `docker start` for production. Restarting an existing container is safer than recreating it, as it preserves volumes, networks, and running processes.
  • Use `docker events` to monitor container lifecycle. Example: docker events --filter 'event=start' streams real-time container start events.
  • Clean up stopped containers regularly. Use docker container prune to remove all stopped containers (and optionally unused images/volumes).

Troubleshooting

1. Docker Command Not Found

If you see docker: command not found, the Docker CLI isn’t in your PATH or the daemon isn’t running.

  1. Verify Docker Desktop (Windows/macOS) or the service (Linux) is running:
    • macOS/Linux: sudo systemctl status docker (enable with sudo systemctl enable --now docker if needed).
    • Windows: Open Docker Desktop and check the tray icon.
  2. Ensure your shell can find Docker. Add this to your shell config:
    export PATH=$PATH:/usr/local/bin/docker
    Then reload with source ~/.bashrc (or equivalent).
  3. Reinstall Docker Desktop if the issue persists.

2. Container Starts but Doesn’t Run Properly

Symptoms include no logs, crashes immediately, or unresponsive services.

  1. Check logs:
    docker logs [container_id]
    Look for errors like missing files, permission issues, or port conflicts.
  2. Inspect the container:
    docker inspect [container_id]
    This shows network settings, mounted volumes, and exit codes.
  3. Run interactively for debugging:
    docker start -ai [container_id]
    The -a flag attaches to the container’s shell, and -i keeps STDIN open.
  4. Verify port mappings:
    docker port [container_id]
    Ensure the host port is correct (e.g., 0.0.0.0:8080 -> 80/tcp).
  5. Recreate the container with debug flags: Add -e DEBUG=1 or other environment variables to enable logging inside the container.

3. Container Stuck in "Created" or "Exited" State

If a container never starts or exits immediately (exit code 0 or 137), it might be:

  1. Waiting for a dependency (e.g., database). Check logs for timeouts or connection errors. Use --restart unless-stopped to auto-retry.
  2. Hitting resource limits. Run with --memory 512m --cpus 1 to set explicit limits.
  3. Blocked by a health check. Add --health-cmd="curl -f http://localhost" --health-interval=5s to customize health checks.
  4. Corrupted or misconfigured. Remove and recreate it:
    docker rm [container_id]
        docker run [options] image_name

4. Permission Denied When Starting Containers

If you see Got permission denied while trying to connect to the Docker daemon, your user isn’t in the docker group.

  1. Add your user to the docker group (Linux/macOS):
    sudo usermod -aG docker $USER
    Then log out and back in or restart your terminal.
  2. On Windows, ensure Docker Desktop is running and your account has admin rights.
  3. If using a non-root user, ensure the user has access to the Docker socket (e.g., via sudo or a configured dockerd group).

5. Port Conflicts

If you get bind: address already in use, another process or container is using the port.

  1. Find the conflicting process:
    sudo lsof -i :8080
    (Replace 8080 with your target port.)
  2. Either stop the conflicting service or choose a different host port in your docker run command.
  3. Use -p 0.0.0.0:8081:80 to bind to a different host port.

6. Image Not Found

If you get Error response from daemon: pull access denied or image not found, the image isn’t available.

  1. Verify the image name/tag is correct. Try pulling it explicitly:
    docker pull redis:latest
  2. If using a private registry, log in first:
    docker login registry.example.com
  3. Check your internet connection. Docker requires a network to pull images.
  4. Use a public image as a test (e.g., alpine):
  5. docker run -it alpine sh

Frequently Asked Questions

Can I start a container that was removed with `docker rm`?

No, once a container is removed with docker rm, it no longer exists. You’ll need to recreate it from the original image using docker run.

What’s the difference between `docker start` and `docker restart`?

docker start resumes a stopped container, while docker restart stops and starts it again (useful for applying changes without recreating the container). Use docker restart if you’ve updated the image or config and need a clean restart.

How do I start a container with custom environment variables?

Use the -e flag in both docker run and docker start. For example:

docker run -e DB_HOST=postgres -e DB_USER=admin nginx

Or to start an existing container with new variables:

docker start -e NEW_VAR=value my_container

Note: Some containers may ignore variables unless the process is restarted. For persistent changes, recreate the container with the new variables.

Why does my container exit immediately after starting?

This usually happens when the main process inside the container finishes (e.g., a script exits). Solutions:

  • Add a sleep command in your entrypoint script to keep the container alive for debugging:
    sleep infinity
  • Use --restart unless-stopped to auto-retry.
  • Check the container’s logs for exit codes (docker logs --tail 50 [container_id]).
  • For web servers, ensure the process listens on the correct port and doesn’t exit (e.g., Nginx runs indefinitely).

Can I start a container in a different network?

Yes, but you must specify a new network with --network. For example:

docker start --network my_custom_network my_container

If the container was never attached to the network, you’ll need to recreate it with the --network flag in docker run.

How do I ensure a container starts automatically on host reboot?

Use Docker’s restart policies. In docker run, add:

docker run --restart unless-stopped my_image

Options include:

  • no: Never auto-restart (default).
  • on-failure: Restart only if the container exits with a non-zero code.
  • always: Always restart, even if exited normally.
  • unless-stopped: Restart unless explicitly stopped by the user.

For existing containers, update the restart policy with:

docker update --restart unless-stopped [container_id]

Note: This only works if the container is already running or will be started once.