Skip to content

Running Containers

PotatoScript edited this page Feb 16, 2025 · 1 revision

Running Docker Containers Cheat Sheet

Overview

Once you've built or pulled a Docker image, you can run it in a container. Containers are lightweight, portable environments that isolate your application. Below are the essential commands and options for running, managing, and interacting with containers.


Running a Container

Command Description
docker run <image>:<tag> Run a container from the specified image. If no tag is specified, latest is used by default.
docker run --name <container_name> <image>:<tag> Assign a custom name to the container.
docker run -d <image>:<tag> Run the container in detached mode (in the background).
docker run -it <image>:<tag> Run the container interactively, useful for debugging or terminal-based applications.
docker run -p <host_port>:<container_port> <image>:<tag> Map a container's port to the host system (e.g., -p 8080:80).
docker run -v <host_path>:<container_path> <image>:<tag> Mount a volume to persist data or share files between the host and the container.
docker run --env <key>=<value> <image>:<tag> Pass environment variables to the container.

Starting and Stopping Containers

Command Description
docker start <container_name> Start a stopped container.
docker stop <container_name> Stop a running container.
docker restart <container_name> Restart a running container.
docker pause <container_name> Pause all processes in a container.
docker unpause <container_name> Resume a paused container.
docker rm <container_name> Remove a stopped container.

Listing Containers

Command Description
docker ps List all running containers.
docker ps -a List all containers, including stopped ones.
docker ps -q List only the container IDs of running containers.

Accessing a Running Container

Command Description
docker exec -it <container_name> <command> Run a command inside a running container (e.g., bash, sh, or other commands).
docker logs <container_name> View the logs of a container.
docker attach <container_name> Attach to a running container’s standard input, output, and error streams.
docker cp <container_name>:<container_path> <host_path> Copy files from a container to the host system.
docker cp <host_path> <container_name>:<container_path> Copy files from the host to a container.

Example: Running a Web Server

To run an Nginx web server container:

  1. Pull the Nginx image:
    docker pull nginx:latest
  2. Run the container and expose port 8080:
    docker run -d -p 8080:80 --name my-nginx nginx:latest
  3. Access the web server at http://localhost:8080.

Removing Containers

Command Description
docker rm <container_name> Remove a stopped container.
docker rm -f <container_name> Force remove a running container.
docker container prune Remove all stopped containers to clean up unused resources.

Inspecting Containers

Command Description
docker inspect <container_name> View detailed information about a container.
docker stats <container_name> Display real-time resource usage statistics of a container.

Managing Container Lifecycle

Command Description
docker update <container_name> --memory <value> Update resource limits (e.g., memory, CPU) for a running container.
docker commit <container_name> <new_image>:<tag> Create a new image from a container’s current state.

Best Practices for Running Containers

  1. Use Named Containers: Assign meaningful names to containers using the --name flag for easier management.
  2. Avoid Exposing All Ports: Only expose the ports that are needed, and restrict access using firewalls.
  3. Use Volumes: Persist important data using Docker volumes instead of relying on the container's filesystem.
  4. Resource Constraints: Limit CPU and memory usage using --memory and --cpus flags to prevent resource exhaustion.

Clone this wiki locally