Docker Refresh

🚢 Docker Basics: Overview

Docker is a platform that enables you to package applications and their dependencies into containers — lightweight, portable, and consistent environments for running software.


🧱 1. Images and Containers

CommandPurpose
docker build -t <name> .Builds a Docker image from a Dockerfile in the current directory.
docker pull <image>Downloads an image from Docker Hub or a registry.
docker imagesLists all images on the local system.
docker rmi <image>Removes an image from the local system.
docker run <image>Creates and starts a container from an image.
docker run -it <image> /bin/bashStarts a container interactively with terminal access.
docker psLists running containers.
docker ps -aLists all containers (running and stopped).
docker stop <container>Gracefully stops a running container.
docker kill <container>Forcefully stops a container.
docker rm <container>Removes a stopped container.
docker exec -it <container> <command>Runs a command inside a running container.
docker logs <container>Shows logs from a container’s stdout/stderr.

🧰 2. Volumes and Persistent Storage

CommandPurpose
docker volume create <name>Creates a named volume.
docker volume lsLists volumes.
docker volume rm <name>Deletes a volume.
docker run -v <volume>:/path/in/container <image>Mounts a volume inside a container.

🌐 3. Networks and Communication

CommandPurpose
docker network lsLists available Docker networks.
docker network create <name>Creates a custom network.
docker run –network=<network> <image>Attaches a container to a specified network.

🧾 4. Docker Compose (Multi-Container Apps)

CommandPurpose
docker-compose upBuilds (if needed) and starts services defined in docker-compose.yml.
docker-compose up -dSame as above, but runs in detached mode.
docker-compose downStops and removes all resources created by up.
docker-compose buildBuilds the images defined in the compose file.
docker-compose psLists containers managed by Compose.
docker-compose exec <service> <cmd>Runs a command in a running service container.

🧼 5. Clean-up and Maintenance

CommandPurpose
docker system dfShows disk usage of images, containers, volumes.
docker system pruneRemoves all unused data (containers, images, networks, volumes).
docker image pruneRemoves unused images.
docker container pruneRemoves stopped containers.

🧪 6. Miscellaneous

CommandPurpose
docker infoShows general Docker system information.
docker versionShows Docker version (client and server).
`docker inspect <containerimage>`
docker tag <src> <repository:tag>Tags an image with a new name and optionally a version.
docker push <repository:tag>Pushes an image to a remote registry.

🧭 Example Workflow

# Build an image from a Dockerfile
docker build -t myapp .

# Run a container from the image
docker run -d -p 8080:80 --name myapp-container myapp

# View logs
docker logs myapp-container

# Execute a shell inside the container
docker exec -it myapp-container /bin/bash

# Stop and remove the container
docker stop myapp-container
docker rm myapp-container