Since I've become quite familiar with docker, I'll write down a brief overview, a list of frequently used commands, their usage, options, and simple examples.
Overview of Docker
An excellent tool that makes it easy to create environments that can be easily destroyed. For example, it makes it easy to execute code of unknown origin or code that presupposes data cracking. When you want to try a library with few Git stars, or when you want to check the operation before installing it on the host / when you want to verify in safe mode because there was a problem on the host, etc.
Docker has images (ubuntu/ruby etc) on top of it, and containers are created from those images. Containers range from OSes like ubuntu to simple execution environments like ruby.
There are two ways to create images: pull from docker hub, or create a Dockerfile and docker build ..
However, on Windows there are differences with Linux in many aspects such as mounting. Docker commands usually cannot be executed from powershell and can only be executed from the dedicated shell of docker tool box.
Basic Commands
Docker is a tool that can efficiently manage and deploy applications using container technology. This article lists the main Docker commands used daily. It also includes a brief description and usage examples for each command, so it can be widely referenced from beginners to intermediate users.
attach
Connect to a running container
docker attach <container>
Reference: start, ps
build
Build an image from Dockerfile
Create an image using the Dockerfile in the current directory.
docker build .
docker build -f /path/to/a/Dockerfile .
docker build -t shykes/myapp .
commit
Create a new image based on changes to a container
docker commit <container> <new_image_name>
cp
Copy files or folders between container and local filesystem
Example of copying files from local to container:
docker cp ./localfile <container>:/path/to/destination
Example of copying files from container to local:
docker cp <container>:/path/to/file ./local/destination
create
Create a new container
docker create <image>
diff
Inspect changes to a container’s filesystem
docker diff <container>
events
Get real-time events from the server
docker events
exec
Execute a command in a running container
docker exec <container> <command>
Example of starting an interactive shell session:
docker exec -it <container> /bin/bash
export
Export a container’s filesystem as a tar archive
docker export <container> > /path/to/tar/file.tar
history
Show the history of an image
docker history <image>
images
List locally stored images
docker images
import
Import a filesystem image from a tarball
docker import /path/to/tar/file.tar
info
Display system-wide information
docker info
inspect
Display detailed information about a Docker object
docker inspect <container|image>
kill
Force stop a running container
docker kill <container>
load
Load an image from a tar archive
docker load < /path/to/tar/file.tar
login
Log in to a Docker registry
docker login
logout
Log out from a Docker registry
docker logout
logs
Fetch the logs of a container
docker logs <container>
pause
Pause all processes in a container
docker pause <container>
port
Show port mappings of a container
docker port <container>
ps
List running containers
docker ps
To list all containers:
docker ps -a
pull
Pull an image or repository from a registry
docker pull <image>
push
Push an image or repository to a registry
docker push <image>
rename
Rename a container
docker rename <container> <new_name>
restart
Restart a container
docker restart <container>
rm
Remove a container
docker rm <container>
To remove all containers:
docker rm $(docker ps -aq)
rmi
Remove an image
docker rmi <image>
To remove all images:
docker rmi $(docker images -aq)
run
Run a command in a new container
docker run <options> <image> <command>
save
Save an image as a tar archive
docker save <image> > /path/to/tar/file.tar
search
Search for images on Docker Hub
docker search <term>
start
Start a stopped container
docker start <container>
stats
Display a live stream of container resource usage
docker stats
stop
Stop a container
docker stop <container>
tag
Tag an existing image with a new tag
docker tag <existing_image> <new_image>
top
Display running processes in a container
docker top <container>
unpause
Resume processes in a paused container
docker unpause <container>
update
Update a container’s configuration
docker update <options> <container>
version
Show Docker version information
docker version
wait
Wait for a container to stop and display its exit code
docker wait <container>
You can make Docker command operations more efficient by referring to this cheat sheet. We recommend using it while also consulting official documentation and references.