DEV Community

Cover image for Docker Commands for Beginners and Pros
DevOps Descent
DevOps Descent

Posted on

Docker Commands for Beginners and Pros

Running Containers

  1. docker run IMAGE: Runs a container from an image.
  2. docker run -it IMAGE: Runs a container in interactive mode.
  3. docker run -d IMAGE: Runs a container in detached mode (background).
  4. docker run -it --rm IMAGE: Runs a container in interactive mode and removes it after exiting.
  5. docker run -p HOST_PORT:CONTAINER_PORT IMAGE: Maps a host port to a container port.
  6. docker run -P IMAGE: Maps all container ports to random host ports.
  7. docker run -v HOST_DIR:CONTAINER_DIR IMAGE: Mounts a host directory into a container directory.
  8. docker run -e VARIABLE=VALUE IMAGE: Sets environment variables for the container.
  9. docker run --name CONTAINER_NAME IMAGE: Assigns a name to the container.
  10. docker run --hostname HOSTNAME IMAGE: Sets the hostname of the container.
  11. docker run --workdir WORKDIR IMAGE: Sets the working directory inside the container.
  12. docker run --entrypoint EXECUTABLE IMAGE: Overrides the default entrypoint of the image.
  13. docker run --user USER IMAGE: Runs the container as a specific user.
  14. docker run --restart POLICY IMAGE: Sets a restart policy (no, on-failure, always, unless-stopped) for the container.

Image description

Managing Containers

  1. docker ps: Lists running containers.
  2. docker ps -a: Lists all containers.
  3. docker stop CONTAINER_NAME: Stops a running container.
  4. docker start CONTAINER_NAME: Starts a stopped container.
  5. docker restart CONTAINER_NAME: Restarts a running container.
  6. docker kill CONTAINER_NAME: Kills a running container.
  7. docker rm CONTAINER_NAME: Removes a stopped container.
  8. docker rm $(docker ps -q): Removes all stopped containers.
  9. docker exec -it CONTAINER_NAME COMMAND: Executes a command inside a running container.
  10. docker attach CONTAINER_NAME: Attaches to a running container's standard input, output, and error streams.
  11. docker logs CONTAINER_NAME: Views the logs of a container.
  12. docker top CONTAINER_NAME: Displays the running processes inside a container.
  13. docker stats CONTAINER_NAME: Displays resource usage statistics for a container.
  14. docker inspect CONTAINER_NAME: Displays detailed information about a container.
  15. docker rename OLD_NAME NEW_NAME: Renames a container.
  16. docker commit CONTAINER_NAME IMAGE_NAME: Creates a new image from a container.
  17. docker update --memory MEM_LIMIT --cpu-shares CPU_SHARE CONTAINER_NAME: Updates container resource limits.

Image description

Managing Images

  1. docker images: Lists images.
  2. docker pull IMAGE_NAME[:TAG]: Pulls an image from a registry.
  3. docker push IMAGE_NAME[:TAG]: Pushes an image to a registry.
  4. docker rmi IMAGE_NAME[:TAG]: Removes an image.
  5. docker rmi $(docker images -q): Removes all unused images.
  6. docker image prune: Removes dangling images.
  7. docker build -t IMAGE_NAME -f Dockerfile .: Builds an image from a Dockerfile.
  8. docker tag IMAGE_NAME NEW_IMAGE_NAME[:TAG]: Tags an image with a new name.
  9. docker save IMAGE_NAME -o IMAGE_NAME.tar: Saves an image to a tar file.
  10. docker load -i IMAGE_NAME.tar: Loads an image from a tar file.

Additional Commands

  1. docker version: Displays Docker version information.
  2. docker info: Displays system-wide information about Docker.
  3. docker system df: Displays disk usage information.
  4. docker system prune: Removes all unused data (containers, networks, images, and volumes).
  5. docker network ls: Lists networks.
  6. docker network create NETWORK_NAME: Creates a network.
  7. docker network rm NETWORK_NAME: Removes a network.
  8. docker network connect NETWORK_NAME CONTAINER_NAME: Connects a container to a network.
  9. docker network disconnect NETWORK_NAME CONTAINER_NAME: Disconnects a container from a network.
  10. docker cp SOURCE_PATH CONTAINER_NAME:DESTINATION_PATH: Copies files between the host and a container.
  11. docker exec -it CONTAINER_NAME sh: Opens a shell inside a running container.
  12. docker container prune: Removes all stopped containers.
  13. docker volume ls: Lists volumes.
  14. docker volume create VOLUME_NAME: Creates a volume.
  15. docker volume rm VOLUME_NAME: Removes a volume.
  16. docker volume prune: Removes all unused volumes.

Note: Remember to replace placeholders like IMAGE_NAME, CONTAINER_NAME, etc., with actual values.

Image description

Vist: https://linktr.ee/DevOps_Descent

Top comments (0)