Top 10 must know docker commands
Docker is a great tool for building microservices, allowing you to create cloud-based applications and systems. To make the most of it via your terminal, here is a run down of the top 10 Docker commands for your terminal.
Before going through refer the below for a quick recap,
A container is launched by running an image. An image is an executable package that includes everything needed to run an application–the code, a run time, libraries, environment variables, and configuration files.
A container is a run time instance of an image i.e. the image with a state or a user process.
Refer Docker-concepts for complete information.
Top 10 docker commands are,
Lists running containers. With options -a/ -all will show running containers and -quiet/ -q will list the id's ( helps you when you want to get all the containers).
2. docker pull
Most of your images will be created on top of a base image from the Docker Hub registry. Docker Hub contains many pre-built images that you can pull and try without needing to define and configure your own. To download a particular image, or set of images (i.e., a repository), use docker pull.
3. docker build
The docker build command builds Docker images from a Docker file and a “context”. A build’s context is the set of files located in the specified PATH or URL. Use the -t flag to label the image, for example docker build -t my_container . with the . (dot) at the end signalling to build using the currently directory.
4. docker run
Run a docker container based on an image, you can follow this on with other commands, such as -it bash to then run bash from within the container. Example: docker run my_image -it bash
5. docker logs
Use this command to display the logs of a container, you must specify a container and can use flags, such as --follow to follow the output in the logs of using the program. docker logs --follow my_container
This
lists the volumes, which
are the preferred mechanism for persisting data generated by and used by Docker
containers.
docker rm— Removes one or more containers. docker rm my_container
7. docker rmi— Removes one or more images. docker rmi my_image
6. docker stop
Stops one or more containers. docker stop my_container stops one container, while docker stop $(docker ps -a -q) stops all running containers. A more direct way is to use docker kill my_container, which does not attempt to shut down the process gracefully first.
Use them together, for example to clean up all your docker images and containers:
kill all running containers with docker kill $(docker ps -q) delete all stopped containers with docker rm $(docker ps -a -q) delete all images with docker rmi $(docker images -q)
Same like stop, you can start or restart the containers with these commands, docker start my_container & docker restart my_container. Exec command to enter the container like docker exec -it <container-id> bash
Hope this would be informative.