Docker has transformed the building, packaging, and ultimately the deployment of applications for the developers. An easy way to make your application and all its dependences into a light and portable container running consistently on all different environments-from developing to testing to production-is now available.
In this blog, we will go through the basic Docker commands which each beginner should know. These commands help you navigate in Docker, control containers, and streamline development.
What is Docker?
Let's take a look at that first because I kind of jumped over explaining what Docker is.
Docker is a platform that allows the development and deployment of applications inside isolated environments known as containers. Containers are all that an application needs to run: its code, libraries, dependencies—to ensure it works exactly the same in any environment.
Containers are lightweight, fast, and easy to manage, making Docker a go-to tool for DevOps and microservices architectures.
Setting Up Docker
First you have to ensure that Docker is running in your system. You should either download Docker Desktop if your operating system is Windows or macOs, then you install Docker Engine for Linux-based systems. After installation, you can start using Docker from the terminal or command line.
Basic Docker Commands
1. docker --version
Check if Docker was installed correctly and which version it is as well:
bash
docker --version
END
This command will print the installed Docker version on your machine.
#### 2. **docker pull**
The docker pull command lets you download images to your local machine from Docker Hub, which is the public repository of Docker images.
Syntax:
```bash
docker pull <image_name>
Example:
docker pull ubuntu
This will fetch the Ubuntu image from the Docker Hub and pull it into your local machine.
3. docker run
docker run
launches an image created that allows you to run applications inside containers.
Syntax
docker run <image_name>
Run Hello World Application
docker run hello-world
This command starts a container using a hello-world
image. When successfully executed, Docker will tell you that you have installed Docker correctly by showing you an output saying, Hello from Docker
.
You can also use the options with docker run
.
-d
: Run the container in the detached mode.-it
: Interactive terminal mode.
Run Hello World Application in Interactive Terminal Mode
docker run -it ubuntu bash
debug
This command executes the Ubuntu container in interactive mode. You can provide commands within the container.
4. docker ps
You need to execute this command to print out all running containers on your host machine:
docker ps
This will print the container ID, image, command, status, ports, and names of the containers that are running.
To print all containers, be them running or stopped, use:
docker ps -a
5. docker stop
The docker stop
command is used to stop an active container.
Syntax
docker stop <container_id or container_name>
Example
docker stop 9c09acd48a25
This command stops the container given its ID. You can replace the name of the container with its ID when giving this command.
6. docker rm
The docker rm
command is used to remove a stopped container.
Syntax
docker rm <container_id or container_name>
Example
docker rm 9c09acd48a25
This removes the stopped container whose ID has been specified. You can remove many containers simultaneously by specifying their IDs or names as separate arguments of the docker rm
command.
7. docker images
The following command lists all the Docker images in your local system.
docker images
It will print out the repository name, tag, image ID, and the size of each.
8. docker rmi
The docker rmi
command deletes an image from your system.
Syntax:
docker rmi <image_id or image_name>
Example:
docker rmi ubuntu
This will delete the Ubuntu image from your local system. If the image is in use by a currently running container, Docker won't let it be deleted till the container is stopped and removed.
9. docker exec
The docker exec
command is used to run commands inside an already existing, running container. This is very helpful when you are debugging or inspecting containers.
Syntax:
docker exec -it <container_id or container_name> <command>
Example
docker exec -it 9c09acd48a25 bash
This command opens an interactive bash session inside the running container and you could run your Linux commands from that bash.
10. docker build
The docker build
command is used in creating a Docker image from a Dockerfile .
Syntax:
docker build -t <image_name> .
Example
docker build -t my-app .
build an image named my-app
using instructions in the Dockerfile in the current directory (.)
Also Read:
Docker and Its Usefulness
The Ultimate Roadmap to Mastering Data Structures and Algorithms (DSA)
Understanding the MVC Architecture in Ruby on Rails
11. docker logs
The docker logs
command prints the log output from a running or stopped container.
Usage:
docker logs <container_id or container_name>
Example:
docker logs 9c09acd48a25
This will print the log output generated by the container with the specified ID.
12. docker network ls
To get a list of all networks that Docker created, you can use the docker network ls
command.
docker network ls
The above command will show the name and ID, and the type of driver used by the networks.
13. docker-compose
docker-compose
is the tool that lets you define and share multi-container Docker applications. Using docker-compose, you can define multiple services, networks, and volumes in a single docker-compose.yml
and then up the whole thing in one go with a single command.
Example:
docker-compose up
This command starts all the containers and services that are defined in docker-compose.yml
.
Conclusion
Docker's magic lies in its simplicity, and these basic commands constitute all that one needs to begin working with containers efficiently. Further on, when one becomes more familiar with Docker, one will find that there are a lot more functionalities coming in about networking, volumes, orchestration with Kubernetes, etc.
For now, knowing these basic commands should get you well on your way into the complexity of Docker topics for the future.
Happy learning