You are currently viewing Docker Basics: A Practical Guide to Commands

Docker Basics: A Practical Guide to Commands

Docker is a powerful platform for automating containerized applications. In this tutorial, I’ll introduce you to some basic Docker commands with examples to help you get started.

Prerequisites:

1. Check Docker Version:

Verify that Docker is installed and check the version.

docker version

2. Pull a Docker Image:

Download a Docker image from Docker Hub. In this example, we’ll pull the official Ubuntu image.

docker pull ubuntu

3. List Docker Images:

View the list of downloaded Docker images on your machine.

docker images

4. Run a Docker Container:

Create and start a Docker container from an image. This example runs an interactive Ubuntu container.

docker run -it ubuntu

5. List Running Containers:

See the list of currently running Docker containers.

docker ps

Add the -a flag to see all containers, including stopped ones.

docker ps -a

6. Stop a Running Container:

Stop a running Docker container using its container ID or name.

docker stop <container_id or container_name>

7. Remove a Container:

Remove a stopped container from your system.

docker rm <container_id or container_name>

8. Remove an Image:

Delete a Docker image from your machine.

docker rmi <image_id or image_name>

9. Build a Docker Image:

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

docker build -t <image_name:tag> .

10. Run a Container from a Specific Image:

Start a container from a specific Docker image.

docker run <image_name:tag>

11. Run Container in Detached Mode:

Run a container in the background (detached mode).

docker run -d <image_name:tag>

12. Expose Ports:

Specify ports to expose when running a container.

docker run -p <host_port>:<container_port> <image_name:tag>

13. View Container Logs:

Display logs of a running container.

docker logs <container_id or container_name>

14. Execute Commands in a Running Container:

Run a command inside a running container.

docker exec -it <container_id or container_name> <command>

15. Clean Up:

Remove all stopped containers and unused images.

docker system prune

These are some of the basic Docker commands to help you start working with containers. As you become more familiar with Docker, you’ll discover more advanced features and options. Refer to the official Docker documentation for a comprehensive guide and additional commands.

Leave a Reply