Essential Docker Commands for Container Management and Deployment

Essential Docker Commands for Container Management and Deployment

Start your Docker journey today!

Nov 11, 2023Ā·

5 min read

1> container ls:- This command lists the currently running Docker containers on your system.
syntax:- docker container ls

i> container ls -a :- This command lists hidden docker container.

2> 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

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> If you want to run the container in the background.
syntax:- docker container run -d ubuntu

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

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

v> This command is used to start a stopped container.
syntax:- docker start container-id

3> 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"

i> 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........

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

Replace CONTAINER with the ID or name of your Docker container.
i>docker logs -f container-id:- Follow log output.

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

iii>docker logs --since="2023-01-01T00:00:00" my_container:- Show logs since or until a specific timestamp or relative time.
iv>docker logs --timestamps container-id:- Show timestamps in the log entries.

5> docker container inspect:- This command is used to obtain detailed information about a running or stopped container.
syntax:- docker container inspect container-id

6> docker container top:- This command is used to display the running processes of one or more containers.
syntax:- docker container top container-id

7> 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

8> 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

9> 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

10> 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> By default, the docker container kill command sends the SIGKILL signal to the container, which immediately terminates the processes in the container.
2> This is a forceful way to stop a container and should be used judiciously.

11> 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> This command will block until the specified container stops, and then it will print the exit code of that container.
2> 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> docker container pause:- This command is used to suspend the processes in a running container.
syntax:- docker container pause container-id

13>docker container unpause:- This command is used to resume the execution of processes in a paused container.
syntax:- docker container unpause container-id

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

15> network ls:- This command lists the docker networks on your system.
syntax:- docker network ls

16> 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

17> 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

18> 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

i> Mapping Multiple Ports:- docker run -p hostPort1:containerPort1 -p hostPort2:containerPort2 -p hostPort3:containerPort3 ... image
The -p option automatically maps all exposed ports on the container to random ports on the host.

Ā