Docker Commands (part-1)
Beginner Commands:
docker --versionCheck the Docker version installed on your system.
Example:
docker --version
docker pull <image>Download a Docker image from Docker Hub.
Example:
docker pull ubuntu
docker build -t <tag> <path>Build a Docker image from a Dockerfile located at a specified path.
Example:
docker build -t myimage .
docker imagesList all locally stored Docker images.
Example:
docker images
docker rmi <my_image/id>- Remove an image - Example:docker rmi ubuntudocker psList running containers.
Example:
docker ps
docker ps -aList all containers (including stopped ones).
Example: ``docker ps -a
docker run <image>Run a container from a specified image.
Example:
docker run ubuntu
docker run -d <image_nam/id>Run a container in detached mode (background). #usethis beacause without -d powershell getting error
Example: ``docker run -d ubuntu
docker run --name <container_name> <image_name>Run container with specific name
Example:
docker run --name mycontainer nginxIt will create container named mycontainer with base image of nginx
docker run -it --name mycont nginx bash- Runs container named mycont with base image nginx and also gives bash shell(command prompt inside container)
docker run -p <host_port>:<container_port> <image_name>Example:
docker run -p 8080:80 nginxThis runs an Nginx container and maps port
8080on the host to port80inside the container.
#bestway -best way to launch container of nginx or any other
docker run -d -p 8080:80 --name myweb nginx- run an Nginx container in detached mode and expose port
80from the container to port8080on the host.
- run an Nginx container in detached mode and expose port
`docker run -it -p 8080:80 --name myweb nginx bash
- This will start the Nginx container but override the default entry point (which is Nginx) with a
bashshell instead, allowing you to interact with the container's file system or run commands inside it. `
- This will start the Nginx container but override the default entry point (which is Nginx) with a
docker exec -it <container_id> <command>Run commands inside a running container.
Example:
docker exec -it mycontainer bashThis command open up interactive bash terminal inside container
docker stop <container_id>Stop a running container.
Example:
docker stop mycontainer
`docker stop
Stop multiple containers at once:
Example:
docker stop cont1 cont2 1233aec2(with name or id)
docker stop $(docker ps -q)docker ps -qlists all the running container IDs, anddocker stopstops them
docker rm $(docker ps -a -q)- Remove All Containers: Once the containers are stopped, you can remove them with this command
docker rm <container_id>Remove a stopped container.
Example:
docker rm mycontainer
docker rmi $(docker images -q)- To remove all Docker images from your system:
docker rmi -f <image_id_or_name>- To remove an image that is currently being used by a container, you would typically encounter an error because Docker prevents you from removing an image that’s being used.
docker rm -f <container_id_or_name>- remove a running container, you can't do so directly without stopping it first so we use -f/--force