Docker

Table of Contents

1. Docker Hello World

Launch a Docker container and run a simple “Hello World” command.

docker run hello-world

2. Build a Docker Image

Create a Docker image based on instructions in a Dockerfile.

docker build -t my-custom-image .

3. List Running Containers

View the containers currently active on your system.

docker ps

4. Stop a Container

Halt the execution of a specific Docker container.

docker stop container_id

5. Remove a Container

Permanently delete a stopped container from your system.

docker rm container_id

6. Inspect a Container

Retrieve detailed metadata about a specific Docker container.

docker inspect container_id

7. Pull Docker Image

Fetch a Docker image from a container registry like Docker Hub.

docker pull nginx:latest

8. Run Detached Container

Execute a Docker container in detached mode, allowing it to run in the background.

docker run -d my-custom-image

9. View Docker Logs

View the output and logs generated by a running Docker container.

docker logs container_id

10. Interactive Shell

Open an interactive shell (bash) inside a running Docker container.

docker exec -it container_id /bin/bash

11. Expose Container Ports

Specify port mapping to expose container services to the host.

docker run -p 8080:80 my-web-app

12. Volume Mounting

Attach a local directory to a container to persist data.

docker run -v /path/on/host:/path/in/container my-app

13. Docker Network

Create and manage Docker networks to facilitate communication between containers.

docker network create my-network

14. Remove All Containers

Forcefully remove all stopped containers in one command.

docker rm -f $(docker ps -aq)

15. Docker Compose

Use Docker Compose to manage multiple containers as a single application.

docker-compose up -d