In this tutorial, we are going to show you the ten most used Docker commands you should know.
Docker is an open-source platform service used for running applications in isolated environments called containers. The containers have their own structure, with encapsulated services that can not interfere with the work of the main server. In this tutorial, we are going to use the Ubuntu 22.04 OS, but you can choose any Linux distro you want.
First, we will install the docker and then show you the ten most used docker commands. Let’s get started!
Table of Contents
Prerequisites
- A server with Ubuntu 22.04 as OS
- User privileges: root or non-root user with sudo privileges
Update the System
Before we start with the installation of Docker, we will update the system packages to their latest versions available:
sudo apt-get update -y && sudo apt-get upgrade -y
Install Docker
First, we need to install some Docker dependencies:
sudo apt-get install apt-transport-https curl gnupg-agent ca-certificates software-properties-common -y
Next is to add the GPGK key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Once the GPGK key is added, we need to add the repo because it is not included in the Ubuntu 22.04:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
After adding the key and repo, you can install Docker with the following commands:
sudo apt-get install docker-ce docker-ce-cli containerd.io -y
After successful installation, start and enable the docker service:
sudo systemctl enable docker && sudo systemctl start docker
To check the status of the service, execute the following command:
sudo systemctl status docker
You should receive the following output:
root@vps:~# systemctl status docker ● docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2022-12-06 15:13:24 CST; 10min ago TriggeredBy: ● docker.socket Docs: https://docs.docker.com Main PID: 120228 (dockerd) Tasks: 9 Memory: 21.1M CPU: 2.326s CGroup: /system.slice/docker.service └─120228 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Now, when the Docker service is installed, we can start with the docker commands.
1. Check the Docker version
To check the installed Docker version, execute the command docker –version. You should receive output similar to this:
root@host:~# docker --version Docker version 20.10.21, build baeda1f
2. Search package
To search for some software that can be installed with Docker, execute the following command:
docker search wordpress
You will receive a list of available WordPress packages:
root@host:~# docker search wordpress NAME DESCRIPTION STARS OFFICIAL AUTOMATED wordpress The WordPress rich content management system… 5012 [OK] bitnami/wordpress Bitnami container image for WordPress 208 [OK] bitnami/wordpress-nginx Bitnami Docker Image for WordPress with NGINX 67 [OK]
3. Docker pull
Docker pull is used for taking the application from the official Docker Hub. Let’s pull the WordPress package we searched for in the previous step.
docker pull wordpress
After a successful pull, you should receive the following output:
root@host:~# docker pull wordpress Using default tag: latest latest: Pulling from library/wordpress a603fa5e3b41: Pull complete c428f1a49423: Pull complete 156740b07ef8: Pull complete fb5a4c8af82f: Pull complete 25f85b498fd5: Pull complete 9b233e420ac7: Pull complete fe42347c4ecf: Pull complete 9a7bf1523229: Pull complete a0b541d575c5: Pull complete c0e75b0cc4dc: Pull complete a97a86207955: Pull complete f88820a52a78: Pull complete 81ebcb8aedf6: Pull complete 265e9160e272: Pull complete adbaf7c3bb9d: Pull complete 1b8e3ff1537e: Pull complete bc197583e391: Pull complete f89eb7cb30b3: Pull complete fa90bc2f4db7: Pull complete 1ca7d72233c9: Pull complete c68df4c97ee8: Pull complete Digest: sha256:fd08649a97d2cb6967fb0a5cd8a710e2b6075502eb18f6c3f841a4d986c0700b Status: Downloaded newer image for wordpress:latest docker.io/library/wordpress:latest
4. Docker run
The Docker run command is used for creating a container from an image.
docker run hello-world
You should receive output similar to this:
root@host:~# docker run hello-world Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
5. Docker ps
The docker ps command is used for listing the running containers. Let’s execute this command: docker ps
root@host:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d2a01e103a5d wordpress "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp wordpress 3ac6a4cf7b0c mariadb:latest "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 3306/tcp wordpressdb
As you can see, there are two running containers on our system.
6. Docker start
Docker start is used for starting the stopped containers. Let’s first list the stopped containers:
docker ps -a
You will receive an output similar to this:
root@host:~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d2a01e103a5d wordpress "docker-entrypoint.s…" 25 minutes ago Exited (0) About a minute ago wordpress 3ac6a4cf7b0c mariadb:latest "docker-entrypoint.s…" 28 minutes ago Exited (0) About a minute ago wordpressdb
Remember that we start the containers using their IDs. To start both containers, execute the following commands:
docker start 3ac6a4cf7b0c docker start d2a01e103a5d
7. Docker stop
Now, opposite of the docker start is the docker stop command used for stopping the containers.
docker stop d2a01e103a5d 3ac6a4cf7b0c
8. Docker Restart
Docker restart is used for restarting the containers:
root@host:~# docker restart 3ac6a4cf7b0c d2a01e103a5d 3ac6a4cf7b0c d2a01e103a5d
9. Docker Remove
The docker rm command is used for removing the Docker containers. Before removing note that you need to stop the containers.
root@host:~# docker rm 3ac6a4cf7b0c d2a01e103a5d 3ac6a4cf7b0c d2a01e103a5d
Now, the containers are deleted permanently.
root@host:~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
10. Docker Help
If you want to know more about the docker, you can simply execute the docker help command. You should receive the following output:
root@host:~# docker help Usage: docker [OPTIONS] COMMAND A self-sufficient runtime for containers Options: --config string Location of client config files (default "/root/.docker") -c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use") -D, --debug Enable debug mode -H, --host list Daemon socket(s) to connect to -l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info") --tls Use TLS; implied by --tlsverify --tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem") --tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem") --tlskey string Path to TLS key file (default "/root/.docker/key.pem") --tlsverify Use TLS and verify the remote -v, --version Print version information and quit
Congratulations! You just learned something about Docker commands with real examples. If learning Docker is difficult, you can sign up for one of our NVMe VPS hosting plans, and we will install and configure Docker for you. You can contact us anytime you want. We are available 24/7.
If you liked this post about the top 10 Docker commands, please share it with your friends on social networks or simply leave a reply below. Thanks.