Running Containers
-
docker run IMAGE
: Runs a container from an image. -
docker run -it IMAGE
: Runs a container in interactive mode. -
docker run -d IMAGE
: Runs a container in detached mode (background). -
docker run -it --rm IMAGE
: Runs a container in interactive mode and removes it after exiting. -
docker run -p HOST_PORT:CONTAINER_PORT IMAGE
: Maps a host port to a container port. -
docker run -P IMAGE
: Maps all container ports to random host ports. -
docker run -v HOST_DIR:CONTAINER_DIR IMAGE
: Mounts a host directory into a container directory. -
docker run -e VARIABLE=VALUE IMAGE
: Sets environment variables for the container. -
docker run --name CONTAINER_NAME IMAGE
: Assigns a name to the container. -
docker run --hostname HOSTNAME IMAGE
: Sets the hostname of the container. -
docker run --workdir WORKDIR IMAGE
: Sets the working directory inside the container. -
docker run --entrypoint EXECUTABLE IMAGE
: Overrides the default entrypoint of the image. -
docker run --user USER IMAGE
: Runs the container as a specific user. -
docker run --restart POLICY IMAGE
: Sets a restart policy (no
,on-failure
,always
,unless-stopped
) for the container.
Managing Containers
-
docker ps
: Lists running containers. -
docker ps -a
: Lists all containers. -
docker stop CONTAINER_NAME
: Stops a running container. -
docker start CONTAINER_NAME
: Starts a stopped container. -
docker restart CONTAINER_NAME
: Restarts a running container. -
docker kill CONTAINER_NAME
: Kills a running container. -
docker rm CONTAINER_NAME
: Removes a stopped container. -
docker rm $(docker ps -q)
: Removes all stopped containers. -
docker exec -it CONTAINER_NAME COMMAND
: Executes a command inside a running container. -
docker attach CONTAINER_NAME
: Attaches to a running container's standard input, output, and error streams. -
docker logs CONTAINER_NAME
: Views the logs of a container. -
docker top CONTAINER_NAME
: Displays the running processes inside a container. -
docker stats CONTAINER_NAME
: Displays resource usage statistics for a container. -
docker inspect CONTAINER_NAME
: Displays detailed information about a container. -
docker rename OLD_NAME NEW_NAME
: Renames a container. -
docker commit CONTAINER_NAME IMAGE_NAME
: Creates a new image from a container. -
docker update --memory MEM_LIMIT --cpu-shares CPU_SHARE CONTAINER_NAME
: Updates container resource limits.
Managing Images
-
docker images
: Lists images. -
docker pull IMAGE_NAME[:TAG]
: Pulls an image from a registry. -
docker push IMAGE_NAME[:TAG]
: Pushes an image to a registry. -
docker rmi IMAGE_NAME[:TAG]
: Removes an image. -
docker rmi $(docker images -q)
: Removes all unused images. -
docker image prune
: Removes dangling images. -
docker build -t IMAGE_NAME -f Dockerfile .
: Builds an image from a Dockerfile. -
docker tag IMAGE_NAME NEW_IMAGE_NAME[:TAG]
: Tags an image with a new name. -
docker save IMAGE_NAME -o IMAGE_NAME.tar
: Saves an image to a tar file. -
docker load -i IMAGE_NAME.tar
: Loads an image from a tar file.
Additional Commands
-
docker version
: Displays Docker version information. -
docker info
: Displays system-wide information about Docker. -
docker system df
: Displays disk usage information. -
docker system prune
: Removes all unused data (containers, networks, images, and volumes). -
docker network ls
: Lists networks. -
docker network create NETWORK_NAME
: Creates a network. -
docker network rm NETWORK_NAME
: Removes a network. -
docker network connect NETWORK_NAME CONTAINER_NAME
: Connects a container to a network. -
docker network disconnect NETWORK_NAME CONTAINER_NAME
: Disconnects a container from a network. -
docker cp SOURCE_PATH CONTAINER_NAME:DESTINATION_PATH
: Copies files between the host and a container. -
docker exec -it CONTAINER_NAME sh
: Opens a shell inside a running container. -
docker container prune
: Removes all stopped containers. -
docker volume ls
: Lists volumes. -
docker volume create VOLUME_NAME
: Creates a volume. -
docker volume rm VOLUME_NAME
: Removes a volume. -
docker volume prune
: Removes all unused volumes.
Note: Remember to replace placeholders like IMAGE_NAME
, CONTAINER_NAME
, etc., with actual values.
Top comments (2)
Nice cheatsheet
Thanks 🤗