# Essential Docker Commands for Container Management and Deployment

1&gt; **container ls**:- This command lists the currently running Docker containers on your system.  
syntax:- `docker container ls`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698654517716/14820038-0b5a-4186-897c-6a168530dd85.png align="center")

i&gt; **container ls -a** :- This command lists hidden docker container.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699600865928/94aeafbf-3243-4c4f-ad60-8a0ef4069114.png align="center")

**2&gt; docker container run**:- This command is used to create and run a container from a specified image.  
syntax:- `docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]`  
Example:-  
`docker container run ubuntu`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699611559466/05229280-85e9-46bb-a3f3-5983a4e1d253.png align="center")

This specifies the name of the image. If the image is not already present on your system, Docker will automatically pull it from Docker Hub  
i&gt; If you want to run the container in the background.  
syntax:- `docker container run -d ubuntu`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699612983675/aed57349-3236-4552-b5dd-92bb07be72a9.png align="center")

ii&gt; If you want to see the background processes running in a detached container. syntax:- `docker container run -it ubuntu`  
iii&gt; The command executed in the container is <mark>sleep 30</mark>, which means the container will stay running for 30 seconds before it completes  
syntax:- `docker container run -d ubuntu sleep 30`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699612837216/321cc155-37a6-4181-bc16-2d70727e12bc.png align="center")

iv&gt; when you exit the interactive shell in the detached container, the container continues running.  
synatx:- `docker stop container-id`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699612316499/2fbead67-acd6-4078-bb98-eec8ccce3c3d.png align="center")

v&gt; This command is used to start a stopped container.  
syntax:- `docker start container-id`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699612669797/dc576c3c-89f5-4d16-90e0-89b4381766db.png align="center")

3&gt; **container rm**:- Command is used to remove one or more containers from your Docker environment.  
syntax:- `docker container rm "container name" or docker container rm "container id"`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699610903272/553b2f77-4eb0-4286-a2ef-624484084339.png align="center")

i&gt; **Remove Multiple Containers:-** You can remove multiple containers at once by specifying their names or IDs separated by spaces:  
syntax:- `docker container rm container1 container2 container3........`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699669768370/b2b305fa-a44b-44f3-b526-afde5b415857.png align="center")

**4&gt; Container logs**:-In Docker, container logs provide valuable information about the status and activities of a running container.  
syntax :- `docker logs [OPTIONS] CONTAINER`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699664177835/d8d16d70-316b-4de1-827c-c7c918d9cb2d.png align="center")

Replace *<mark>CONTAINER </mark>* with the ID or name of your Docker container.  
i&gt;docker logs -f container-id:- Follow log output.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699664589586/597b2c6a-e0eb-4f84-b82d-a03cd1c4204f.png align="center")

ii&gt;docker logs --tail 20 container-id:- Number of lines to show from the end of the logs.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699664768092/1d3e104d-e297-43e5-8902-532cdb3fa025.png align="center")

iii&gt;docker logs --since="2023-01-01T00:00:00" my\_container:- Show logs since or until a specific timestamp or relative time.  
iv&gt;docker logs --timestamps container-id:- Show timestamps in the log entries.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699664913691/bdbbd094-e63f-435e-a2d9-a2ad3679e95a.png align="center")

5&gt; **docker container inspect**:- This command is used to obtain detailed information about a running or stopped container.  
syntax:- `docker container inspect container-id`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699673202729/75d57ff2-29e0-4cb5-9b4e-73649cb11925.png align="center")

6&gt; **docker container top**:- This command is used to display the running processes of one or more containers.  
syntax:- `docker container top container-id`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699665007700/2320065f-7d12-4e1e-8c16-dda62b3f40b5.png align="center")

7&gt; **docker stats:-** This command provides real-time information about CPU usage, memory usage, network I/O, and block I/O of one or more containers.  
syntax:- `docker stats container-id`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699670185779/350e4684-75a6-490f-a0e2-dd8f1b0d05ba.png align="center")

8&gt; **docker container restart:-** This command is used to restart one or more stopped containers.  
syntax:- `docker container restart container-id`  
note:- If you want to restart multiple containers,  
`docker container restart container-id1 container-id2`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699665047519/3622af5d-bac7-4f79-bef2-4d5d95500d39.png align="center")

9&gt; **docker container attach:-** This command is used to attach the terminal to a running container. When you attach to a container, you connect your terminal's input and output streams to the container, allowing you to interact with the running processes in the container as if you were connected directly to its console.  
syntax:- `docker container attach container-id`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699670378264/edf8abef-b6b5-4e70-9ad2-263c83792268.png align="center")

10&gt; **docker container kill:-** This command is used to send a signal to a running container, causing it to stop abruptly.  
syntax:- `docker container kill container-id`  
note:- 1&gt; By default, the `docker container kill` command sends the SIGKILL signal to the container, which immediately terminates the processes in the container.  
2&gt; This is a forceful way to stop a container and should be used judiciously.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699665103373/7839ee24-6f90-4310-b700-9c43d5566b14.png align="center")

11&gt; **docker container wait:-** This command is used to block until a container stops, then prints the container's exit code.

syntax:- `docker container wait container-id`  
note:-  
1&gt; This command will block until the specified container stops, and then it will print the exit code of that container.  
2&gt; The exit code is a numeric value that indicates the exit status of the container, where 0 typically means success and non-zero values indicate an error.

12&gt; **docker container pause:-** This command is used to suspend the processes in a running container.  
syntax:- `docker container pause container-id`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699670635663/c8191307-96e5-4305-b4c8-d5a338eeebe3.png align="center")

13&gt;**docker container unpause:-** This command is used to resume the execution of processes in a paused container.  
syntax:- `docker container unpause container-id`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699670656863/56af80ff-fb8a-48a0-863a-83e134b3ecbf.png align="center")

**14&gt; image ls**:- This command is used to list the Docker images that are currently available on your system.  
syntax:- `docker image ls`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698654649329/392d8659-a397-46ae-b500-ef6a87a65e9b.png align="center")

15&gt; **network ls**:- This command lists the docker networks on your system.  
syntax:- `docker network ls`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699600759950/eaf13651-eb01-43a9-8a26-5b7e6e2e1b6a.png align="center")

16&gt; **docker run Image**:- This command is a fundamental and essential command in Docker used to create and start a new container from a specified image.  
syntax:- `docker run Image-name`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699610727758/8b1cf443-c872-40d8-9a8f-ba3ee5c7346f.png align="center")

17&gt; **container run ubuntu cat /etc/os-release**:- This is the command that will be executed inside the container. It reads the contents of the `/etc/os-release` file, which contains information about the operating system.  
syntax:- `docker container run ubuntu cat /etc/os-release`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699600727753/601f7653-a9bc-4970-b2e2-b3fe20d2d5b5.png align="center")

**18&gt; Port Mapping:-** Port mapping is the process of binding a network port on the host machine to a port on the container. This allows external traffic to access services running inside the container.  
Port mapping is a crucial aspect of Docker networking, enabling communication between the container and the host machine or external networks.  
syntax:- `docker run -p hostPort:containerPort ...`  
`docker run -p 8080:80 nginx`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699672995650/64fa0b26-06f1-4cf2-b5e4-dcc2b4fa2dd8.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699673012782/c5738386-d591-4f1e-b395-4dd14e69fb44.png align="center")

**i&gt; Mapping Multiple Ports:-** `docker run -p hostPort1:containerPort1 -p hostPort2:containerPort2 -p hostPort3:containerPort3 ... image`  
The <mark>-p </mark> option automatically maps all exposed ports on the container to random ports on the host.
