-
Notifications
You must be signed in to change notification settings - Fork 0
Running Containers
PotatoScript edited this page Feb 16, 2025
·
1 revision
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.
| 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. |
| 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. |
| 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. |
| 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. |
To run an Nginx web server container:
- Pull the Nginx image:
docker pull nginx:latest
- Run the container and expose port 8080:
docker run -d -p 8080:80 --name my-nginx nginx:latest
- Access the web server at
http://localhost:8080.
| 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. |
| 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. |
| 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. |
-
Use Named Containers: Assign meaningful names to containers using the
--nameflag for easier management. - Avoid Exposing All Ports: Only expose the ports that are needed, and restrict access using firewalls.
- Use Volumes: Persist important data using Docker volumes instead of relying on the container's filesystem.
-
Resource Constraints: Limit CPU and memory usage using
--memoryand--cpusflags to prevent resource exhaustion.