Docker Basic Commands
Docker Basic Commands provides a set of basic commands that allow users to manage containers, images, volumes, networks, and more. These commands are essential for anyone working with Docker, whether in development, testing, or production environments.
Key Docker Basic Commands

Table of Contents
1. `docker run`:
- This command is used to create and start a new container from a specified Docker image.
- Example:
```sh
docker run hello-world
```
This command runs a container based on the hello-world
image, which prints a message and exits.
2. `docker pull`:
- This command downloads a Docker image from a Docker registry (e.g., Docker Hub) to the local system.
- Example:
```sh
docker pull ubuntu
```
This command pulls the latest `ubuntu` image from Docker Hub to your local machine
3. `docker ps`:
- This command lists all running containers. If you want to see all containers (including stopped ones), you can use `docker ps -a`.
- Example:
```sh
docker ps
```
This command displays a list of currently running containers, including their IDs, names, and other details.
4. `docker stop`:
- This command stops a running container. You need to provide the container ID or name to stop it.
- Example:
```sh
docker stop my-container
```
This command stops the container named `my-container`.
5. `docker rm`:
- This command removes a stopped container. You can remove multiple containers by listing their IDs or names.
- Example:
```sh
docker rm my-container
```
This command removes the stopped container named `my-container`.
6. `docker rmi`:
- This command removes one or more Docker images from the local system.
- Example:
```sh
docker rmi ubuntu
```
This command removes the `ubuntu` image from the local Docker repository.
7. `docker build`:
- This command is used to build a Docker image from a Dockerfile. The `-t` option allows you to tag the image with a name.
- Example:
```sh
docker build -t my-java-app .
```
This command builds a Docker image named `my-java-app` from the Dockerfile in the current directory.
8. `docker exec`:
- This command is used to run a command inside a running container. It’s often used to start a shell in a running container.
- Example:
```sh
docker exec -it my-container /bin/bash
```
This command starts an interactive bash shell inside the running container named `my-container
9. `docker logs`:
- This command retrieves logs from a container, allowing you to monitor its output.
- Example:
```sh
docker logs my-container
```
This command displays the logs of the container named `my-container`.
10. `docker-compose up`:
- This command starts services defined in a `docker-compose.yml` file. It can build images, create containers, and start services as defined in the file.
- Example:
```sh
docker-compose up
```
This command starts all services defined in the `docker-compose.yml` file.
Java Example using Docker Commands
Let’s consider an example where we want to build and run a simple Java application using Docker commands.
1. Java Application:
```java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Docker World!");
}
}
```
2. Dockerfile:
```Dockerfile
FROM openjdk:17-jdk-alpine
WORKDIR /app
COPY HelloWorld.class /app/HelloWorld.class
CMD ["java", "HelloWorld"]
```
3. Build the Docker Image:
Use the `docker build` command to create an image:
```sh
docker build -t my-java-app .
```
4. Run the Docker Container:
Use the `docker run` command to create and start a container:
```sh
docker run my-java-app
```
5. Check Running Containers:
Use the `docker ps` command to list running containers:
```sh
docker ps
```
6. View Logs:
Use the `docker logs` command to view the output of your container:
```sh
docker logs <container_id>
```
7. Stop and Remove the Container:
Stop and remove the container using `docker stop` and `docker rm`:
```sh
docker stop <container_id>
docker rm <container_id>
```
Conclusion:
These Docker Basic Commands are essential for managing Docker containers, images, and services. They provide a solid foundation for working with Docker in both development and production environments