Basics Docker Commands
Docker has become a cornerstone technology in modern software development and deployment, empowering developers and sysadmins alike to build, ship, and run applications with ease. If you’re just getting started with Docker, mastering a few key commands will set you on the path to Docker proficiency. Let’s dive in!
1. docker run
The docker run command is your entry point into the world of Docker containers. It creates and starts a new container based on a specified Docker image. For example:
docker run <image>
This command will pull the specified image from Docker Hub (if not already available locally) and run a container based on that image.
2. docker ps
Once you have containers running, you’ll want to keep track of them. The docker ps
command displays a list of running containers along with essential information such as container ID, image, status, and ports. Use it like this:
docker ps
The
command, when executed in a single line, provides a concise overview of all Docker containers, including both running and stopped ones. Here’s an example of its usage:docker ps -a
This command will list all containers along with their details, such as container ID, image used, status, ports, and creation time, in a single line output. It’s particularly useful for quickly checking the status of all containers on your system without having to scroll through lengthy outputs.
3. docker images
The docker images
command is a fundamental tool in Docker for listing all the Docker images that are currently stored on your system. Here’s how you can use it:
docker images
Executing this command will display a list of Docker images along with their repository, tag, image ID, creation date, and size. This information can be invaluable for managing your Docker images, such as identifying images you want to use for running containers or images you want to remove to free up disk space.
The docker rmi
command is used to remove one or more Docker images from your system. Here’s how you can use it:
docker rmi image_name
4. docker pull
The docker pull
command is used to fetch Docker images from a specified registry, such as Docker Hub, and store them locally on your machine. Here’s how you can use it:
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
NAME
refers to the name of the image you want to pull.TAG
(optional) specifies a particular version or variant of the image. If omitted, Docker pulls the default tag, typically “latest”.DIGEST
(optional) provides a unique identifier for a specific image version, ensuring reproducibility.
In the provided scenario, we’ve successfully obtained two Docker images, specifically the nginx
and ubuntu
images, from the Docker Hub registry.
5. docker start and stop
The docker start
command is used to start one or more stopped containers. Here’s how you can use it:
docker start my_container
In this scenario, Docker images were already created and available, but no containers based on the nginx image are currently running. When checking the list of containers using the docker ps
command, it shows an empty list, indicating that no containers are active.
To rectify this, we start the nginx container by using its container ID. After initiating the nginx container with its unique identifier, the docker ps
command then displays the running container, confirming that the nginx container has successfully started and is now operational.
The docker stop
command is used to halt one or more running Docker containers. Here’s how you can use it:
docker stop my_container
6. docker rmi
The docker rm
command is used to remove one or more Docker containers from your system. Here’s how you can use it:
docker rm my_container
In conclusion, mastering these fundamental Docker commands—docker run
, docker pull
, docker images
, docker start
, docker stop
, docker rm
—lays a strong foundation for efficient container management and deployment.
In wrapping up, these basic Docker commands unlock a world of possibilities for smoother container management and deployment. With these tools in hand, you’re ready to explore the vast realm of Docker and streamline your development workflows. So, roll up your sleeves, dive in, and let Docker simplify your journey towards more efficient software deployment. Happy coding!