Docker is a powerful tool for developers to containerize applications, making deployment and management seamless. This blog post will cover essential Docker commands and explain their purpose and usage.
1. Building Docker Images
docker build
This command is used to build a Docker image from a Dockerfile.
- docker build .
- The . refers to the current directory where the Dockerfile is located.
docker build -t imagename:01 .
- docker build -t imagename:01 .
- -t assigns a tag (imagename:01) to the built image.
- . represents the build context (current directory).
2. Listing Docker Images
docker image ls
- docker image ls
- Displays a list of all Docker images stored on the system.
3. Running Docker Containers
docker run -d --rm --name "myappinstance1" -p 3001:3001 71ad36636fc1
- docker run -d --rm --name "myappinstance1" -p 3001:3001 71ad36636fc1
- -d runs the container in detached mode (in the background).
- --rm removes the container once it stops.
- --name assigns a name to the container.
- -p 3001:3001 maps port 3001 of the container to port 3001 on the host machine.
- 71ad36636fc1 is the image ID used to create the container.
docker run -d --rm --name "container" -p 3211:3211 lawson:01
- docker run -d --rm --name "container" -p 3211:3211 lawson:01
- Similar to the previous command, but uses an image named lawson:01.
docker run -d -it --rm --name "container" -p 3211:3211 lawson:01
- docker run -d -it --rm --name "container" -p 3211:3211 lawson:01
- -it allows interactive mode (useful for running shell commands inside the container).
4. Listing and Managing Running Containers
docker ps
- docker ps
- Shows all running containers.
docker ps -a
- docker ps -a
- Shows all containers, including stopped ones.
5. Stopping and Removing Containers
docker stop abc1231
- docker stop abc1231
- Stops a running container by its ID or name (abc1231).
docker rm containername
docker rm containername
- Removes a stopped container by name.
docker rm 71ad36636fc1
- docker rm 71ad36636fc1
- Removes a stopped container using its ID.
6. Removing Docker Images
docker rmi 7123423fc132
- docker rmi 7123423fc132
- Removes an image by its ID.
docker rmi imagename:01
- docker rmi imagename:01
- Removes an image by its name and tag.
7. Viewing Logs
docker logs imagename
- docker logs imagename
- Fetches logs from a running container.
8. Running Containers with Environment Variables
docker run -d --env myrootpassword="root" --env mysqldatabase="sdfsdf" --name mysqldb mysql
- docker run -d --env myrootpassword="root" --env mysqldatabase="sdfsdf" --name mysqldb mysql
- Runs a MySQL container with environment variables set for root password and database name.
9. Inspecting Containers
docker inspect mysqldb
- docker inspect mysqldb
- Displays detailed information about the mysqldb container.
10. Managing Docker Networks
docker network create my-net
- docker network create my-net
- Creates a custom network named my-net.
docker network ls
docker network ls
- Lists all available Docker networks.
docker run -d --env myrootpassword="root" --env mysqldatabase="sdfsdf" --name mysqldb --network my-net mysql
- docker run -d --env myrootpassword="root" --env mysqldatabase="sdfsdf" --name mysqldb --network my-net MySQL
- Runs a MySQL container attached to the my-net network.
Additional Useful Commands
docker stop $(docker ps -q)
docker stop $(docker ps -q)
- Stops all running containers.
docker rm $(docker ps -aq)
docker rm $(docker ps -aq)
- Removes all stopped containers.
docker rmi $(docker images -q)
docker rmi $(docker images -q)
- Removes all Docker images.

Post a Comment
0Comments