Docker is a tool that simplifies the process of creating, deploying, and running applications using containers. Containers allow developers to package an application with all the necessary parts, such as libraries and dependencies, and distribute it as a single package. To make your work with Docker easier, we have prepared this cheat sheet for you, which includes the most essential commands you should know.
Getting Started
-
Installing Docker
- Installation may vary for different operating systems, refer to Docker's official documentation.
-
Getting Started
docker run hello-world
: Runs a test container Hello World, useful for verifying that Docker is functioning correctly.
Working with Containers
-
Creating and Running a Container
docker run [options] image [command] [arg...]
: Creates and runs a container from an image. Examples of options include-d
to run in the background and-p
for port mapping.
-
Listing Running Containers
docker ps
: Lists running containers.docker ps -a
: Lists all containers, including those not currently running.
-
Stopping a Container
docker stop [container_id or name]
: Stops a running container.
-
Removing a Container
docker rm [container_id or name]
: Removes a container.
Working with Images
-
Searching for Images
docker search [term]
: Searches for images on Docker Hub.
-
Downloading Images
docker pull [image name]
: Downloads an image from Docker Hub.
-
Listing Images
docker images
: Lists downloaded images.
-
Removing an Image
docker rmi [image name]
: Removes a downloaded image.
Networking and Port Mapping
- Running a Container with Port Mapping
docker run -p host_port:container_port image_name
: Runs a container with port mapping from the host machine to the container port.
Sharing and Storing Data
- Creating and Using Volumes
docker volume create [volume_name]
: Creates a new volume.docker run -v volume_name:/path/in/container [image name]
: Runs a container with an attached volume.
Dockerfile and Building Images
- Building an Image
docker build -t [image name]:[tag] .
: Builds an image from a Dockerfile in the current directory.
Docker Compose
-
Running Services
docker-compose up
: Starts the services defined indocker-compose.yml
.
-
Stopping Services
docker-compose down
: Stops and removes all containers defined indocker-compose.yml
.
This cheat sheet covers basic commands you need for working with Docker. For more detailed information and advanced commands, we recommend consulting Docker's official documentation.