Basic Docker Commands 2024
Here are some essential Docker commands, along with examples and explanations for each:
1. docker --version
- Description: Displays the Docker version installed on the system.
-
Example:
docker --version -
Output:
Docker version 20.10.7, build f0df350 -
Explanation: Helps verify if Docker is installed and check the version for compatibility with specific features or projects.
2. docker pull
- Description: Downloads a Docker image from a remote registry (Docker Hub by default) to your local system.
-
Example:
docker pull nginx -
Explanation: Downloads the official NGINX image to your local machine. Docker will fetch the image from Docker Hub if it is not available locally.
3. docker images
- Description: Lists all the Docker images available locally.
-
Example:
docker images -
Output:
REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest ae2feff98a0c 2 weeks ago 133MB -
Explanation: Shows all downloaded or built images with details such as
repository,tag,image ID,creation date, andsize.
4. docker run
- Description: Runs a container from an image. It creates, starts, and runs the container from a specified image.
-
Example:
docker run -d -p 8080:80 nginx -
Explanation:
-
-d: Runs the container in detached mode (in the background). -
-p 8080:80: Maps the container's port 80 to the host machine's port 8080. - This command will run an NGINX container and serve it on your local machine at
localhost:8080.
-
5. docker ps
- Description: Lists all running containers.
-
Example:
docker ps -
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 87dfb6cf56d2 nginx "/docker-e..." 5 minutes Up 5 mins 0.0.0.0:8080->80/tcp amazing_elion -
Explanation: Displays all active containers along with information like container ID, image used, running status, and mapped ports.
6. docker stop
- Description: Stops a running container.
-
Example:
docker stop 87dfb6cf56d2 -
Explanation: Stops the container with the ID
87dfb6cf56d2. You can also use the container name instead of the ID.
7. docker rm
- Description: Removes a stopped container.
-
Example:
docker rm 87dfb6cf56d2 -
Explanation: Removes the container from the Docker system. The container must be stopped first. You can use the container ID or name.
8. docker rmi
- Description: Removes a Docker image from your local system.
-
Example:
docker rmi nginx -
Explanation: Deletes the image
nginxfrom the local machine. If any containers are using this image, you must stop and remove them first.
9. docker exec
- Description: Runs a command inside a running container.
-
Example:
docker exec -it 87dfb6cf56d2 /bin/bash -
Explanation: Opens an interactive shell (
/bin/bash) inside the running container. The-itoption makes the session interactive, allowing you to execute commands within the container.
10. docker logs
- Description: Displays the logs from a running or stopped container.
-
Example:
docker logs 87dfb6cf56d2 -
Explanation: Fetches the logs for the container with the specified ID, showing output generated by the application running inside the container.
11. docker build
- Description: Builds an image from a Dockerfile.
-
Example:
docker build -t myapp . -
Explanation: The
-tflag assigns a tag (myapp) to the image. The.specifies the build context (usually the current directory where the Dockerfile is located).
12. docker inspect
- Description: Returns detailed information about a container or image.
-
Example:
docker inspect 87dfb6cf56d2 -
Explanation: Outputs a JSON object containing low-level details about the specified container or image, such as network settings, mounts, and configuration.
13. docker-compose up
-
Description: Starts containers as defined in a
docker-compose.ymlfile. -
Example:
docker-compose up -
Explanation: Spins up all services defined in the
docker-compose.ymlfile, creating containers, networks, and volumes as needed.
14. docker-compose down
-
Description: Stops and removes containers, networks, and volumes created by
docker-compose up. -
Example:
docker-compose down Explanation: Cleans up resources used by the
docker-composesetup, effectively bringing down the application stack.
Summary of Basic Docker Commands
These commands provide a foundation for working with Docker, covering common tasks such as managing images, containers, networks, logs, and executing commands.
Top comments (0)