A handy guide to essential Docker commands for quick reference and efficient container management.
docker login
docker pull <image-name>
docker build . -t <account-name>/<image-name>
docker push <account-name>/<image-name>
-
Basic run:
docker run nginx
-
Run with a specific version:
docker run <image-name>:<version>
-
Run with interactive input and prompt:
docker run -it <image-name>
-
Run with a sleep command:
docker run ubuntu sleep 5
-
Run with environment variable:
docker run -e <env_name>:<env_value> <image_name>
-
DeprecatedRun with link command
docker run -d --link <hostname-in-application>:<container-name> <image-name>
-
Run with network command
docker network create my-network docker run --network my-network --name container1 <image1>
- Map host port to container port:
docker run -p <docker-host-port>:<docker-container-port> <image-name>
- Create and mount a volume
- Volume mount
docker volume create <volume_name> docker run --mount type=volume,source=<volume_name>,target=<location_inside_container> mysql
- Bind mount
docker run --mount type=bind,source=<folder_location_on_docker_host>,target=<location_inside_container> mysql
- Volume mount
- DeprecatedCreate and mount a volume
- Volume mount
docker volume create <volume-name> docker run -v <volume-name>:<location_inside_container>
- Bind mount
docker run -v <folder_location_on_docker_host>:<location_inside_container>
- Volume mount
- Run in detached mode:
docker run -d <image-name>
- Get image layer details:
docker history <image-name>
- Attach to a running container:
docker attach <container-id>
- Execute a command in a running container:
docker exec <container-id-or-name> <command>
- List all containers:
docker ps -a
- List all images:
docker images
- Inspect a container:
docker inspect <container-name-or-id>
- View container logs:
docker logs <container-name-or-id>
- Stop a container:
docker stop <container-id-or-name>
- Remove a container:
docker rm <container-id-or-name>
- Remove an image:
docker rmi <image-name>
- Run docker compose:
docker-compose up
- Running docker registry locally
docker run -d -p 5000:5000 --name registry registry:2
- Tag an image
docker image tag <image-name> <tag-name>
- Push image to registry:
docker push <user-name>/<image-name>
Feel free to contribute by adding more commands or improving existing ones! Open a pull request or file an issue for any suggestions.
This repository is licensed under the MIT License.
Happy Dockering! 🚢