Loading...

21 Docker commands everyone should know

question docker infrastructure
Ram Patra Published on August 20, 2024

Docker is a popular containerization tool that allows you to package and run applications in isolated environments. Here are some basic Docker commands that will come in handy if you work with Docker on a daily basis, are looking for a refresher, or are preparing for an interview.

1. Docker Installation Verification

Verify if Docker is installed and check its version.

   docker --version

2. Docker Help

Display a list of Docker commands or options available.

   docker --help

3. Pull an Image

Pull a Docker image from a repository like Docker Hub.

   docker pull <image_name>:<tag>

Example:

   docker pull ubuntu:latest

4. List Docker Images

Show all Docker images stored locally.

   docker images

5. Run a Container

Run a container from a Docker image.

   docker run <image_name>

Example:

   docker run -it ubuntu

6. List Running Containers

List all currently running containers.

   docker ps

Add the -a option to show all containers, including stopped ones.

   docker ps -a

7. Stop a Running Container

Stop a running container using its ID or name.

   docker stop <container_id>

Example:

   docker stop 1a2b3c4d5e6f

8. Remove a Container

Remove a stopped container.

   docker rm <container_id>

Example:

   docker rm 1a2b3c4d5e6f

9. Remove an Image

Remove a Docker image from your local system.

   docker rmi <image_name>

Example:

   docker rmi ubuntu:latest

10. View Container Logs

View the logs generated by a specific container.

   docker logs <container_id>

Example:

   docker logs 1a2b3c4d5e6f

Add the -f option to follow the logs in real-time:

   docker logs -f 1a2b3c4d5e6f

11. View Application Logs Inside a Container

To view logs of an application running inside a container, first access the container and then view the relevant log files.

   docker exec -it <container_id> <command>

Example:

   docker exec -it 1a2b3c4d5e6f bash

Once inside the container, navigate to the directory where the logs are stored (e.g., /var/log), and use a command like cat, tail, or less to view the logs:

   tail -f /var/log/app.log

12. Execute Commands in a Running Container

Execute a command inside a running container.

   docker exec -it <container_id> <command>

Example:

   docker exec -it 1a2b3c4d5e6f bash

13. Build an Image from a Dockerfile

Build a Docker image from a Dockerfile in the current directory.

   docker build -t <image_name> .

Example:

   docker build -t myapp:1.0 .

14. Tag an Image

Tag an image with a name before pushing it to a repository.

   docker tag <image_id> <repository_name>:<tag>

Example:

   docker tag 123abc456def myrepo/myapp:1.0

15. Push an Image to a Repository

Push a tagged image to a Docker registry.

   docker push <repository_name>:<tag>

Example:

   docker push myrepo/myapp:1.0

16. Remove All Stopped Containers

Remove all stopped containers at once.

   docker container prune

17. Remove All Unused Images

Remove all dangling (unused) images.

   docker image prune

18. Inspect a Container

Get detailed information about a container in JSON format.

   docker inspect <container_id>

Example:

   docker inspect 1a2b3c4d5e6f

19. Docker Compose Commands

  • Start services defined in a docker-compose.yml file:
    docker-compose up
    
  • Stop services:
    docker-compose down
    

20. Export and Import a Container

  • Export a container to a tar file:
    docker export <container_id> > <container_name>.tar
    

    Example:

    docker export 1a2b3c4d5e6f > mycontainer.tar
    
  • Import a container from a tar file:
    docker import <container_name>.tar
    

    Example:

    docker import mycontainer.tar
    

21. Docker System Information

Display system-wide information about Docker, including the number of containers, images, and resources.

   docker info

The above commands provide a solid foundation for working with Docker, from managing images and containers to more advanced operations like building images and orchestrating with Docker Compose.

Presentify

Take your presentation to the next level.

FaceScreen

Put your face and name on your screen.

ToDoBar

Your to-dos on your menu bar.

Ram Patra Published on August 20, 2024
Image placeholder

Keep reading

If this article was helpful, others might be too

question docker infrastructure August 20, 2024 How to enter into a Docker container and run some commands even if it does not have bash?

If a Docker container does not have bash installed, you can still enter the container and run commands using other available shells or command-line interfaces. Here’s how to do it:

question docker infrastructure August 20, 2024 7 alternatives to Lazydocker

Lazydocker is a popular terminal UI for managing Docker containers, images, and volumes. While it provides a convenient way to interact with Docker, there are other tools that offer different features and functionalities that might be better suited depending on your needs. Here are some alternatives to Lazydocker: