Docker is an open-source containerization platform that enables developers to build, deploy, run, update, and manage containers.
Containers are standardized, executable components that combine application source code with the operating system (OS) libraries and dependencies necessary to run that code in any environment.
To manage containers we make use of the command line interface. In this blog, we will see the important commands required to manage our containers.
1. docker info
This command display system information.
Usage: docker info
2. docker version
This command displays the version information.
Usage: docker version [OPTIONS]
3. docker pull
This command is used to pull an image or a repository from the registry.
Usage: docker pull [OPTIONS] IMAGE_NAME[:TAG]
By default, this command pulls images from the registry. If you have set up a local repository, you can specify its path to pull images. A registry path is like a URL but does not contain a protocol specifier (https://).
docker pull myregistry.local:5000/testing/test-image
4. docker images
This command shows images, tags, and their size.
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
5. docker run
This command is used for creating the container and then starting it.
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
In the above example the -d flag is used for running the container in the background or in detached mode. The -p flag is used for port mapping. We have mapped port 80 of the container to port 80 of local host so that we can access our web application from the local host. The –name flag is used for assigning a name to the container.
6. docker ps
This command is used for listing all running containers.
Usage: docker ps [OPTIONS]
Use -a flag to list both running and stopped containers.
7. docker stop and start
The docker stop command is used to stop one or more running containers.
Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
We can use either container ID or container name to stop it.
Similarly, we can use start to start one or more containers.
Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]
8. docker rm
This command is used to remove one or more containers.
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Before removing a container we need to stop it. A running container cannot be removed.
9. docker rmi
This command is used to remove one or more images.
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
In the above example, we have removed two images flaskapp and webapp with one command.
10. docker logs
This command is used for fetching the logs of a container.
Usage: docker logs [OPTIONS] CONTAINER
11. docker exec
This command is used for executing a command in a running container.
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
In the above example, with the first command, we create a directory /home/chkmt inside our container. With the second command, we created a bash session in our container, using which we can go inside the directory that we created.
12. docker inspect
This command displays detailed information about objects like containers or images.
Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...]
In the above example, we used inspect command to get detailed information about an image. Similarly, it can be used with other docker objects like containers, networks, or volume.
13. docker cp
This command is used for copying files between the container and the local filesystem.
Usage(container to local system): docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH
Usage(local system to container): docker cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH
We will copy a file from our local system to a container named java-app. We can use the exec command to confirm if the file is copied.
Similarly, we can copy files from the container to the local system.
14. docker volume create
This command is used for creating volumes that can be used for data persistence.
Usage: docker volume create [OPTIONS] [VOLUME]
Now, we can mount this volume to a container so that even if our container gets deleted, our data will remain safe. We can use -v flag with the docker run command to mount the volume.
To delete the volume you can use the following command.
docker volume rm [OPTIONS] VOLUME [VOLUME...]
15. docker network
To list the networks use the following command.
docker network ls [OPTIONS]
To create a network use the following command.
docker network create [OPTIONS] NETWORK
To connect a container to a network use the following command.
docker network connect [OPTIONS] NETWORK CONTAINER
16. docker build
This command is used to build an image from a Dockerfile.
Usage: docker build [OPTIONS] PATH
In the above example, we navigated to the directory where our Dockerfile is present and used the build command to build an image.
We can use the -t flag to give a tag to the image.
17. docker login
This command is used to login into the repository. You can log in to your local repository as well. The default repository is the docker hub.
Usage: docker login [OPTIONS] [SERVER]
18. docker push
This command is used to push images to the registry.
Usage: docker login [OPTIONS] [SERVER]
19. docker system
Use the following command to get disk usage.
docker system df
Use the following command to remove unused containers, images, and networks.
docker system prune
20. docker commit
This command is used to create an image from the container changes. It can be used to commit a container’s file changes or settings into a new image.
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Please contact our technical consultants to learn cloud DevOps Infrastructure best practices to ensure stable production operation and faster go to market for product.