DEV Community

Divyanshu
Divyanshu

Posted on

Introduction to Docker and Basic Commands for Beginners

Docker has revolutionized the way developers build, ship, and run applications. It's an open-source platform that automates the deployment of applications inside lightweight, portable containers. Containers are isolated from the host system, and they allow for consistent and reproducible environments across development, testing, and production.
In this article, we'll introduce Docker, explain its core concepts, and provide a list of essential commands for beginners to get started.
What is Docker?
Docker is a platform that enables developers to easily deploy, run, and manage applications in containers. A container is a standardized unit of software packaging that contains everything an application needs to run, including the code, runtime, libraries, and system tools. The key benefits of Docker include:
Portability: Docker containers run on any machine with Docker installed, regardless of the underlying operating system.
Consistency: Docker ensures that the application will run the same way in development, testing, and production environments.
Isolation: Containers are isolated from each other, which allows multiple applications to run on the same system without interfering with each other.

Key Docker Concepts
Before diving into Docker commands, it's important to understand some basic concepts:
Image: A Docker image is a read-only template that contains the application and all of its dependencies. Images can be used to create containers.
Container: A container is a running instance of an image. It is isolated and has its own file system, processes, and network interfaces.
Dockerfile: A Dockerfile is a script used to define a custom image. It contains instructions on how to build the image (e.g., installing dependencies, copying files, setting environment variables).
Docker Hub: Docker Hub is a public repository where you can find and share Docker images. It contains many pre-configured images for popular applications.


Installing Docker
To use Docker, you'll need to install it on your machine. Docker is available for various operating systems (Linux, macOS, and Windows). Here's how to install it on a popular system:
For Ubuntu (Linux):
sudo apt update
sudo apt install docker.io
sudo systemctl enable docker
sudo systemctl start docker

For macOS and Windows:
Visit the official Docker website and download Docker Desktop for macOS or Windows.
Follow the installation instructions.

Basic Docker Commands for Beginners
Once Docker is installed, you can start using it by running various commands. Here are some basic Docker commands every beginner should know:
1. docker - version
This command shows the version of Docker installed on your system.

docker - version

2. docker pull [image-name]
The docker pull command is used to download an image from Docker Hub to your local machine. For example, to pull the latest version of the official ubuntu image:

docker pull ubuntu

3. docker images
This command lists all the Docker images currently available on your system.

docker images

4. docker run [image-name]
The docker run command creates a new container from a specified image and starts it. For example, to run a container using the ubuntu image:

docker run ubuntu

By default, this runs the container in the foreground and gives you an interactive terminal. To run the container in the background, you can add the -d flag.

docker run -d ubuntu

5. docker ps
This command lists all the currently running containers. If you want to see all containers (including stopped ones), add the -a flag:

docker ps -a

6. docker stop [container-id]
To stop a running container, use the docker stop command followed by the container ID or name. You can find the container ID using docker ps.

docker stop

7. docker start [container-id]
To restart a stopped container, use the docker start command:

docker start

8. docker rm [container-id]
This command removes a stopped container from your system:

docker rm

9. docker rmi [image-name]
To remove an image from your local machine, use the docker rmi command. For example:

docker rmi ubuntu

Note: You can't remove an image if there are containers using it. You'll need to stop and remove the containers first.

10. docker exec -it [container-id] bash
The docker exec command allows you to execute commands inside a running container. The -it flags provide an interactive terminal. For example, to open a Bash shell inside a running Ubuntu container:

docker exec -it bash

11. docker logs [container-id]
To view the logs of a running or stopped container, use the docker logs command:

docker logs

12. docker build -t [image-name] [path]
The docker build command is used to create a custom image from a Dockerfile. The -t flag tags the image with a name, and [path] is the location of the Dockerfile.

docker build -t my-image .

13. docker network ls
Docker allows containers to communicate with each other using networks. The docker network ls command lists all networks available on your Docker system.

docker network ls

Conclusion

Docker simplifies the process of developing, shipping, and running applications. By using Docker containers, developers can ensure consistency across various environments, improving deployment times and reducing the risk of errors. The commands covered in this article are just the tip of the iceberg, but they are essential for beginners to get started with Docker.
As you become more familiar with Docker, you'll learn how to build custom images, manage networks and volumes, and use advanced Docker features. With these basic commands, you're ready to begin exploring the powerful world of Docker containers. Happy Dockerizing!

Top comments (0)