To create dockerfile in your project then run the following command touch dockerfile
Or
Always use this command for best and recommanded practice is docker init
. This command creates necessory files for docker.
Run the following command to build an docker image
docker build -t <name_of_your_image> .
Run the following command to run container docker run --name <your_container_name> -p <port_number_of_which_you_want_to_run_container>:<your_application_port> <your_image_name>
**
Example :- docker run --name my-container -p 5055:5055 my-docker-image
**
if you want to make versions of your container and image then run the below command
docker run --name <your_container_name> -p <port_number_of_which_you_want_to_run_container>:<your_application_port> <your_image_name>:v<version_number>
**
Example :- docker run --name my-container -p 5055:5055 my-docker-image:v2
**
if you want to make versions of your container and image with volumes then run the below command
docker run --name <your_container_name> -p <port_number_of_which_you_want_to_run_container>:<your_application_port> --rm -v <absolute_path_of_your_current_dir>:<directory_you_have_specifed> <your_image_name>:v<version_number>
**
Example :- docker run --name my-container -p 5055:5055 --rm -v D:\xampp\htdocs\learn-docker:/app my-docker-image:v2
**
Note :- The last dot means current directory. you can change current directory like this ../ or ../../ etc.
-t
- flag is for naming you docker image.
-p
- flag is for defining / exposing port numbers.
--name
- flag is for naming you docker container.
--rm
- flag is for removing containers and images.
-v
- flag is for creating volums to sync changes.
.dockerignore
- file use for ignore files and folders from docker project to docker image.
If you are using 0
port number means the docker container takes port number randomly.
To see all list of images in command line then run docker images
To see all list of containers in command line then run docker ps
use docker --help
for more cli commands.
To delete docker unused image then run the following commnad docker image rm <your_image_name>
.
To delete docker "In use" running... image forcefully then run the following commnad docker image rm <your_image_name> -f
.
To delete docker unused container then run the following commnad docker container rm <your_image_name>
.
To delete docker "In use" running... container forcefully then run the following commnad docker container rm <your_image_name> -f
.
Use the docker container prune -a
command to remove all stopped containers, or refer to the docker system prune -a
command to remove unused containers in addition to other Docker resources, such as (unused) images and networks.
To Automate Docker Process you need to create compose.yaml
file and define set of instructions.
after that you need to run the following command docker compose up