Quick reference guide for Docker commands
Click ⭐if you like the project. Pull Requests are highly appreciated. Follow me @SudheerJonna for technical updates.
No. | Questions |
---|---|
1 | What is docker? |
2 | Why docker? |
3 | Installation |
4 | Registries and Repositories |
5 | Create,Run,Update and Delete containers |
6 | Start and stop containers |
7 | Networks |
8 | Cleanup commands |
9 | Utility commands |
10 | Docker Hub |
11 | Dockerfile |
12 | Docker Compose |
13 | Docker Swarm |
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers.
Docker is useful to automate the deployment of applications inside a software containers, which makes the applications easy to ship and run virtually anywhere (i.e, platform independent). The Docker container processes run on the host kernel, unlike VM which runs processes in guest kernel.
The docker desktop downloads are available for windows, mac and linux distributions.
It supports for Windows 10 64-bit: Pro, Enterprise, or Education (Build 15063 or later) editions. You need to follow the below steps for installation.
- Download docker desktop for windows from https://docs.docker.com/docker-for-windows/install/
- Double-click
Docker Desktop Installer.exe
to run the installer. - Make sure
Enable Hyper-V Windows Features
option is selected
- Download docker desktop for mac from https://docs.docker.com/docker-for-mac/install/
- Double-click
Docker.dmg
to open the installer and drag it to the Applications folder. - Double-click
Docker.app
in the Applications folder to start Docker.
You can install from a package easily
- Go to https://download.docker.com/linux/ubuntu/dists/, choose your Ubuntu version and then go to pool/stable/ to get .deb file
- Install Docker Engine by referring the downloaded location of the Docker package.
$ sudo dpkg -i /path/to/package.deb
- Verify the Docker Engine by running the
hello-world
image to check correct installation.
$ sudo docker run hello-world
Docker Registry is a service that stores your docker images. It could be hosted by a third party, as public or private registry. Some of the examples are,
- Docker Hub,
- Quay,
- Google Container Registry,
- AWS Container Registry
A Docker Repository is a collection of related images with same name which have different tags. These tags are an alphanumeric identifiers(like 1.0 or latest) attached to images within a repository.
For example, if you want to pull golang image using docker pull golang:latest
command, it will download the image tagged latest within the golang
repository from the Docker Hub registry. The tags appeared on dockerhub as below,
Login to a registry
> docker login [OPTIONS] [SERVER]
[OPTIONS]:
-u/--username username
-p/--password password
Example:
1. docker login localhost:8080 // Login to a registry on your localhost
2. docker login
Logout from a registry
> docker logout [SERVER]
Example:
docker logout localhost:8080 // Logout from a registry on your localhost
Search for an image in registry
docker search [OPTIONS] TERM
Example:
docker search golang
docker search --filter stars=3 --no-trunc golang
This command pulls an image or a repository from a registry to local machine
docker image pull [OPTIONS] NAME[:TAG|@DIGEST]
Example:
docker image pull golang:latest
This command pushes an image to the registry from local machine.
docker image push [OPTIONS] NAME[:TAG]
docker image push golang:latest
Create a new container
docker container create [OPTIONS] IMAGE [COMMAND] [ARG...]
Example:
docker container create -t -i sudheerj/golang --name golang
Rename a container
docker container rename CONTAINER NEW_NAME
Example:
docker container rename golang golanguage
docker container rename golanguage golang
docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]
Example:
docker container run -it --name golang -d sudheerj/golang
You can also run a command inside container
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Example:
docker exec -it golang sh // Or use bash command if sh is failed
Update configuration of one or more containers
docker container update [OPTIONS] CONTAINER [CONTAINER...]
Example:
docker container update --memory "1g" --cpuset-cpu "1" golang // update the golang to use 1g of memory and only use cpu core 1
Remove one or more containers
docker container rm [OPTIONS] CONTAINER [CONTAINER...]
Example:
docker container rm golang
docker rm $(docker ps -q -f status=exited) // Remove all the stopped containers
Start one or more stopped containers
docker container start [OPTIONS] CONTAINER [CONTAINER...]
Example:
docker container start golang
Stop one or more running containers
docker container stop [OPTIONS] CONTAINER [CONTAINER...]
Example:
docker container stop golang
docker stop $(docker ps -a -q) // To stop all the containers
Restart one or more containers and processes running inside the container/containers.
docker container restart [OPTIONS] CONTAINER [CONTAINER...]
Example:
docker container restart golang
Pause all processes within one or more containers
docker container pause CONTAINER [CONTAINER...]
Example:
docker container pause golang
Unpause all processes within one or more containers
docker container unpause CONTAINER [CONTAINER...]
Example:
docker container unpause golang
Kill one or more running containers
docker container kill [OPTIONS] CONTAINER [CONTAINER...]
Example:
docker container kill golang
Block until one or more containers stop and print their exit codes after that
docker container wait CONTAINER [CONTAINER...]
Example:
docker container wait golang
Docker Hub is a cloud-based repository provided by Docker to test, store and distribute container images which can be accessed either privately or publicly.
Dockerfile is a text document that contains set of commands and instructions which will be executed in a sequence in the docker environment for building a new docker image.
Docker compose(or compose) is a tool for defining and running multi-container Docker applications.
Docker Swarm(or swarm) is an open-source tool used to cluster and orchestrate Docker containers.