Docker is an open-source platform that allows developers to build, ship, and run applications inside containers. These containers package all the necessary components of an application, such as libraries, dependencies, and configurations, making it easy to move the application across different environments. Docker simplifies the process of managing, deploying, and scaling applications. This article provides a beginner-friendly overview of how to use Docker through basic Docker commands.
Prerequisites
Before diving into Docker commands, ensure that you have Docker installed on your system. You can download Docker Desktop for Windows or macOS, or install Docker Engine on Linux through the official Docker website: https://www.docker.com/get-started.
Command : docker –version
To Check the version of your Docker . If it ask you for the installation type set it as WSL
You can Install a linux sub system on your windows by using this command on your command prompt > wsl – – install
Common Docker Commands
1. docker --help
The docker --help
command provides a list of all available Docker commands along with a brief description of what each command does. It’s a useful reference when you’re starting with Docker.
2. docker pull
The docker pull
command is used to download an image from Docker Hub or another registry to your local system. Docker Hub is the default registry, containing thousands of images for different applications and operating systems.
docker pull ubuntu:latest
Example of Dockerfile for React application.
FROM node:21-alpine AS build WORKDIR /app COPY package*.json ./ RUN npm install COPY . . RUN npm run build FROM node:21-alpine RUN npm install -g serve COPY --from=build /app/build /app/build EXPOSE 3000 CMD [ "serve", "-s" , "/app/build", "-l", "3000" ] # docker build -t simple-cart-web:1.0v . #image creation # docker build -t react-app . # docker run -p 5000:3000 --name expense-react-web -d react-app
3. docker build
You can create custom Docker images using the docker build
command, typically from a Dockerfile
. The Dockerfile
is a script that defines the application environment, including its base image, dependencies, and configurations.
Example :
docker build -t my-app:1.0 .
In this case:
-t my-app:1.0
: Specifies the name and tag of the image..
: Refers to the current directory where theDockerfile
is located.
4. docker images
The docker images
command lists all the images stored locally on your system. This includes both images you have pulled from Docker Hub and those you’ve built yourself.
docker images
5. docker run
The docker run
command is used to create and start a container from a Docker image. You can specify various options when running a container, such as port mappings and environment variables.
docker run -p 5000:3000 --name expense-react-web -d react-app
This command runs a container with the following options:
-d
: Runs the container in detached mode (in the background).-p 5000:3000
: Maps port 5000 on the host to port 3000 on the container.--name expense-react-web
: Names the container expense-react-webreact-app
: Specifies the image to run (in this case, the official react-app custom image).
You can check if the container is running by visiting http://localhost:5000
in your browser.
6. docker ps
The docker ps
command lists the running containers on your system. If you want to see all containers, including the stopped ones, use the -a
flag.
docker ps docker ps -a
7. docker stop
and docker start
To stop a running container, use the docker stop
command followed by the container name or ID. To start a stopped container, use the docker start
command.
docker stop my-web-app docker start my-web-app
8. docker rm
and docker rmi
The docker rm
command removes one or more containers. Similarly, docker rmi
removes Docker images.
Example:
docker rm my-web-app
docker rmi nginx
9. docker logs
The docker logs
command allows you to view the logs from a running or stopped container. This is useful for troubleshooting or monitoring your application.
docker logs my-web-app
10. docker network
Docker allows you to create custom networks to manage how containers communicate with each other. The docker network
command allows you to manage these networks.
docker network ls
docker network create my-network
11. docker-compose
Although not a single Docker command, Docker Compose is a tool that allows you to define and manage multi-container applications. It uses a YAML file (docker-compose.yml
) to define the services, networks, and volumes required by your application.
To install Docker Compose, follow the installation instructions on the official Docker website.
You can start all services defined in a docker-compose.yml
file with:
docker-compose up docker-compose down
12. docker exec
The docker exec
command allows you to execute commands inside a running container. It’s often used for debugging or administration purposes, such as opening a shell inside the container.
Example :
docker exec -it my-web-app bash
Summary
Docker is an incredibly powerful tool for managing containerized applications. By learning the essential Docker commands, such as docker pull
, docker run
, docker ps
, and docker exec
, you can quickly start working with containers and building your own applications.
Once you’re familiar with these commands, you can explore advanced features like Docker Compose for multi-container applications, Docker networks for communication between containers, and Docker volumes for persistent storage.