Table of contents
Docker is an open-source platform that allows developers to automate the deployment of applications inside lightweight, portable containers. Containers package an application with all its dependencies and configurations, ensuring that it runs consistently across different environments. This makes it easier to develop, ship, and run applications without worrying about compatibility issues.
Key features:
Portability: Containers can run on any system that supports Docker, from a developer's laptop to production servers.
Efficiency: Containers share the host system's kernel, making them more lightweight and efficient compared to traditional virtual machines.
Scalability: Docker makes it simple to scale applications up or down, accommodating changing demands.
Docker has revolutionized the way applications are developed, tested, and deployed, contributing to the rise of modern DevOps practices.
Docker Commands
IMAGES:
List all local images:
docker images
Delete an image:
docker rmi <image_name>
Remove unused images:
docker image prune
Build an image from a Dockerfile:
docker build -t <image_name>:<version> .
(version is optional)Build without cache:
docker build -t <image_name>:<version> . --no-cache
CONTAINER:
List all local containers (running & stopped):
docker ps -a
List all running containers:
docker ps
Create & run a new container:
docker run <image_name>
(if image not available locally, itāll be downloaded from DockerHub)Run container in background:
docker run -d <image_name>
Run container with custom name:
docker run --name <container_name> <image_name>
Port binding in container:
docker run -p <host_port>:<container_port> <image_name>
Set environment variables in a container:
docker run -e <var_name>=<var_value> <container_name>
(or <container_id>)Start or stop an existing container:
docker start|stop <container_name>
(or <container_id>)Inspect a running container:
docker inspect <container_name>
(or <container_id>)Delete a container:
docker rm <container_name>
(or <container_id>)
TROUBLESHOOT:
Fetch logs of a container:
docker logs <container_name>
(or <container_id>)Open shell inside running container:
docker exec -it <container_name> /bin/bash
ordocker exec -it <container_name> sh
DOCKER HUB:
Pull an image from DockerHub:
docker pull <image_name>
Publish an image to DockerHub:
docker push <username>/<image_name>
Login into DockerHub:
docker login -u <image_name>
ordocker login
(also,docker logout
to remove credentials)Search for an image on DockerHub:
docker search <image_name>
VOLUMES:
List all volumes:
docker volume ls
Create new named volume:
docker volume create <volume_name>
Delete a named volume:
docker volume rm <volume_name>
Mount named volume with running container:
docker run --volume <volume_name>:<mount_path>
(or using --mountdocker run --mount type=volume,src=<volume_name>,dest=<mount_path>
)Mount anonymous volume with running container:
docker run --volume <mount_path>
Create a bind mount:
docker run --volume <host_path>:<container_path>
(or using --mountdocker run --mount type=bind,src=<host_path>,dest=<container_path>
)Remove unused local volumes:
docker volume prune
(for anonymous volumes)
NETWORK:
List all networks:
docker network ls
Create a network:
docker network create <network_name>
Remove a network:
docker network rm <network_name>
Remove all unused networks:
docker network prune