1. Docker
- Command:
docker
- Explanation: Displays help and lists all available Docker commands. This is useful for getting a quick overview of Docker’s functionality and syntax.
docker
2. Docker Images
- Command:
docker images
- Explanation: Lists all Docker images currently available on your system. This includes image names, tags (versions), and IDs.
docker images
3. Docker Image Pull
- Command:
docker pull containername
ordocker pull containername:version
- Explanation: Downloads a Docker image from the Docker registry (e.g., Docker Hub) to your local machine. You can optionally specify a version or tag of the image to pull a specific version.
docker pull nginx
docker pull nginx:1.21 //version (tag) mentioned
Without tag latest version is download. tag means version.
4. Docker Container Run
- Command:
docker run containername
ordocker run containername:version
- Explanation: Starts a new container from the specified image. You can also specify a version to run a particular version of the container.
docker run nginx
docker run nginx:1.21
5. Docker Running Container Status
- Command:
docker ps
- Explanation: Lists all currently running Docker containers, showing details like container ID, image name, and the command being executed.
docker ps
*ps - Process Status
6. Docker Running & Stopped Container Status
- Command:
docker ps -a
- Explanation: Lists all Docker containers on the system, including both running and stopped ones. This is helpful for seeing the history of all container executions.
docker ps -a
*-a - All Containers
7. Docker Image Remove
- Command:
docker rmi imageid
- Explanation: Removes a Docker image from your local system by its image ID. Be careful, as removing images can cause containers depending on that image to fail.
docker rmi 123456789abc
*rmi - Remove Image
8. Docker Container Size
- Command:
docker ps --size
- Explanation: Lists all running Docker containers along with their size information (including the size of the writable layer). This is useful for monitoring container resource usage.
docker ps --size
9. Docker Detached Mode
- Command:
docker run -d containername
- Explanation: Runs a Docker container in the background (detached mode). The container continues to run after the command prompt returns, allowing you to keep the container running without being attached to it interactively.
docker run -d nginx
*-d - Detached Mode
10. Port Mapping
- Command:
docker run -d -p machineport:containerport image:tag
- Explanation:
- The
-p
option maps a port from the host machine to a port in the container. This allows services running inside the container to be accessed from the outside. machineport
is the port on your local machine, andcontainerport
is the port inside the container.
- The
docker run -d -p 8080:80 nginx:latest
*-p - Port
This maps port 8080 on the host to port 80 in the container running the nginx
image.
- Port Checking in Linux:
- You can check if a port is in use with:
ps -ef | grep portNumber
- You can check if a port is in use with:
11. Logs
docker logs containerid
- Retrieves logs from a running or stopped container.
docker logs -f containerid
*-f - Follow
The -f
option (follow) keeps the log output streaming in real-time, similar to tail -f
.
- Save Logs to File:
- To save container logs to a file:
docker logs containerid > filename.log
12. Docker Inspect
- Command:
docker inspect containerid
- Explanation:
- Provides detailed low-level information about a container or image in JSON format. This includes configuration, state, networking, and more.
docker inspect 123456789abc
13. Docker Exec (Interactive Terminal)
- Command:
docker exec -it containerid bash
- Explanation:
- The
exec
command allows you to run a command inside a running container. The-it
options are for interactive terminal access, allowing you to runbash
to interact with the container’s shell.
- The
docker exec -it 123456789abc bash
*-it -Interactive Terminal
Opens a Bash shell inside the running container.
14. Delete Container
docker rm containerid
*rm - remove
- Removes a stopped container. The container must be stopped before it can be removed.
docker rm -f containerid
-f - force
- The
-f
option forces the removal of a running container by first stopping it.
docker stop containerid
-
Stops a running container.
-
Delete All Containers:
docker rm -f $(docker ps -aq)
*-aq - All Container Id
- This command forcefully removes all containers (running or stopped).
15. Delete Image
docker rmi imageid
*rmi - remove image
-
Removes a Docker image from the system.
-
Delete All Images:
docker rmi -f $(docker image -q)
- Forcefully removes all Docker images from the system.
16. Container Name Change
- Command:
docker run --name newName imageName:tag
- Explanation:
- When running a container, you can assign a custom name to it using the
--name
option. This makes it easier to reference the container by name instead of its ID.
- When running a container, you can assign a custom name to it using the
docker run --name my_nginx nginx:latest
Runs the nginx
container with the name my_nginx
.
17. Docker Build Image
- Command:
docker build -t imageName:tag
- Explanation:
- This command builds a Docker image from the Dockerfile in the current directory. The
-t
option is used to tag the image with a name and optional version (tag
). If no tag is specified, it defaults tolatest
.
- This command builds a Docker image from the Dockerfile in the current directory. The
docker build -t myapp:1.0 .
Builds the Docker image from the current directory's Dockerfile and tags it as myapp
with version 1.0
.
18. Environment Variable Overwrite
- Command:
docker run --env NAME="Value" imageName:tag
- Explanation:
- The
--env
or-e
option allows you to pass environment variables to the Docker container at runtime. This overwrites or sets environment variables for the container.
- The
docker run --env ENV_VAR="production" myapp:1.0
Runs the container from the myapp:1.0
image and sets the environment variable ENV_VAR
to "production"
inside the container.
19. ARG
Command (Build-Time Variables)
The ARG
instruction defines a build-time variable in Docker that can be used in the Dockerfile and overridden during the image-building process.
-
Syntax:
ARG VARIABLE_NAME=VALUE
-
Example:
ARG JAVA_VERSION=21 FROM openjdk:${JAVA_VERSION}-slim
- Explanation:
- The
ARG
command allows you to define a variable, likeJAVA_VERSION
, and assign it a default value (21
in this case). - During the Docker build process, you can overwrite the value using the
--build-arg
option.
- The
- Explanation:
-
Overwrite Example:
- If you want to build an image with Java version 22 instead:
docker build --build-arg JAVA_VERSION=22 -t welcome-greeting:v1.0 .
- This would replace
JAVA_VERSION=21
with22
during the build.
- If you want to build an image with Java version 22 instead:
20. Docker Volume
Volumes in Docker are used to persist data between container restarts and allow sharing data between the host machine and the container.
Types of Volumes:
-
Binded Volume
- Purpose: A bind mount directly links a directory or file from the host machine to a directory inside the container.
- Command:
docker run -v /path/to/localfolder:/path/to/containerfolder imagename:tag
- Example:
docker run -v /home/user/data:/app/data myapp:latest
- In this case, the
data
folder on your host will be mapped to/app/data
in the container.
- In this case, the
-
Unbinded Volume
-
Purpose: Docker-managed volumes are created and managed by Docker itself and do not map to a specific location on the host system.
-
Commands:
- Create an unbound volume:
docker volume create foldername
- List Docker volumes:
docker volume ls
- Inspect a volume:
docker inspect foldername
- Create an unbound volume:
-
How to Use Unbinded Volumes:
docker run -it -v foldername:/app/data imagename:tag
- This command mounts the Docker-managed volume (
foldername
) to/app/data
inside the container.
- This command mounts the Docker-managed volume (
-
-
Filtering by Volume
- Purpose: You can filter running containers based on the volumes they are using.
- Command:
docker ps -a --filter volume=foldername
-
Unbinding a Docker Volume
- Purpose: Detach a volume from a container.
- Command:
You can stop the container and remove it, which will detach the volume.docker rm containerid
21. Docker Login
- Purpose: Use the
docker login
command to authenticate yourself with Docker Hub (or another Docker registry) using your username and password. - Command:
docker login -u username
- This prompts for your password after entering your username. Once logged in, you can push images to your Docker Hub account.
22. Docker Push
- Purpose: Once you’ve logged in and tagged your image, you can push it to Docker Hub or another registry using the
docker push
command. - Command:
docker push imagename:tag
- Example:
docker push myusername/myimage:latest
- This pushes the local image
myusername/myimage:latest
to Docker Hub.
- This pushes the local image
- Example: