-
Docker is a platform that enables developers to build, ship, and run applications in isolated containers.
-
It simplifies software deployment by packaging applications and their dependencies into lightweight, portable units.
What is Docker?
-
Containerization Platform.
-
Lightweight & Portable.
-
Simplifies Deployment.
Why go for Docker?
-
Consistency Across Environments
-
Scalability & Efficiency
-
Simplified CI/CD & Automation
-
Rapid Deployment & Rollback
-
Security & Isolation
How to create a docker file and usage of each command?
FROM node:latest
RUN apt update && apt install apache2 -y
WORKDIR /usr/src/app
COPY . .
ADD file.tar.gz .
EXPOSE 80
ENTRYPOINT["node","server.js"]
CMD ["apache2ctl","-D","FORGROUND"]
COMMANDS |
USAGE |
FROM | Specifies the base image to build the container. |
RUN | It updates package |
WORKDIR | Sets the working directory inside the container |
COPY | Copies files and directories from the local system to the container’s |
EXPOSE | Declares the port |
ADD | Download the URL and save/Copies and automatically extracts 'tar.gz' |
Feature |
CMD |
ENTRYPOINT |
Defines default command | ✅ Yes | ✅ Yes |
Can be overridden with docker run | ✅ Yes | ❌ No (unless --entrypoint is used) |
Best for | Default commands with flexibility | Fixed commands like daemons or script |
Here I have listed all the basic commands to work with docker.
Category |
Command |
Description |
Build & Images | sudo docker build -t myimage . | Build a Docker image from the Dockerfile. |
sudo docker images | List all available Docker images. | |
sudo docker rmi myimage | Remove a specific Docker image. | |
sudo docker image prune -a | Remove all unused images. | |
Containers | sudo docker run -d --name mycontainer -p 8080:80 myimage | Run a container in detached mode with port mapping. |
sudo docker ps | List all running containers. | |
sudo docker ps -a | List all containers (including stopped ones). | |
sudo docker stop mycontainer | Stop a running container. | |
sudo docker rm mycontainer | Remove a stopped container. | |
sudo docker container prune | Remove all stopped containers. | |
sudo docker stop $(sudo docker ps -aq) && sudo docker rm $(sudo docker ps -aq) | Stop & remove all containers. | |
Container Management | sudo docker commit mycontainer myimage | Convert a running container into an image. |
sudo docker logs mycontainer | View logs of a running container. | |
sudo docker stats | Monitor live resource usage of containers. | |
sudo docker top mycontainer | Show running processes inside a container. | |
sudo docker system df -v | Show disk usage of Docker system. | |
Inspect & Attach | sudo docker inspect mycontainer | View detailed information about a container. |
sudo docker exec -it mycontainer bash | Open a bash shell inside a running container. | |
sudo docker attach mycontainer | Attach to a running container. | |
Copy Files | sudo docker cp file.txt mycontainer:/app/ | Copy a file from the host to the container. |
sudo docker cp mycontainer:/file.txt /home/user/ | Copy a file from the container to the host. | |
Volumes | sudo docker volume create myvolume | Create a new Docker volume. |
sudo docker volume ls | List all Docker volumes. | |
sudo docker volume inspect myvolume | View details of a specific volume. | |
Networks | sudo docker network create mynetwork | Create a custom Docker network. |
sudo docker network ls | List all Docker networks. | |
sudo docker network inspect mynetwork | Inspect details of a Docker network. | |
Docker Compose | sudo docker-compose up -d | Start multiple containers using docker-compose.yml. |
sudo docker-compose down | Shut down and remove all containers & networks. | |
sudo docker-compose up --build -d | Rebuild images and restart services. | |
Cleanup | sudo docker system prune -a --volumes | Remove all containers, images, networks, and volumes. |