How to install and run Docker containers on a Webdock server
Last updated: January 4th 2022
Docker is an open-source platform used to build, manage and run containerized applications. Docker packages the application in a container that has everything that the application needs to run.
This guide gives a brief overview of installation and running Docker containers on a Webdock server.
- Webdock cloud Ubuntu 18.04 or Ubuntu 20.04 instance
- You have shell (SSH) access to your VPS
First, uninstall the old version of Docker if it is already installed.
$ sudo apt-get remove docker docker-engine docker.io containerd runc
It would be fine if apt-get says that none of these packages are installed.
Now update the apt packages.
$ sudo apt-get update
Install necessary packages to allow apt to use a repository over HTTPS.
$ sudo apt-get install ca-certificates curl gnupg lsb-release
Add Docker's official GPG key.
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Set up Docker's stable repository using the following command.
$ echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Update the apt packages.
$ sudo apt-get update -y
Install the latest version of the Docker engine and containerd.
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
Enable and start Docker service.
$ sudo systemctl enable docker.service $ sudo systemctl start docker.service
Add your user to the Docker group. This will enable docker to run without using sudo (as a non-root user).
$ sudo usermod -aG docker $USER
To activate changes to groups, run the following commands.
$ sudo newgrp docker $ groups
Check the Docker version.
$ docker version
Now to check that Docker is working correctly we will run hello-world.
$ docker run hello-world
If you see the message “Hello from Docker!” it means that docker is working correctly.
Managing Docker images
In order to run a Docker container, a Docker image is required. Use the following command to pull the Docker image from DockerHub.
$ docker pull nginx
The above command will pull the nginx Docker image.
List all the downloaded Docker images on the host machine using the following command.
$ docker images
To remove a Docker image, use the following command.
$ docker rmi nginx
The above command will remove the nginx image with the latest tag from the host machine. For example, if you have nginx image with the tag version-2 then nginx in the above command becomes nginx:version-2.
Similarly, all the unused Docker images can be removed by using the following command.
$ docker image prune -a
To know detailed information about a Docker image (in this case nginx):
$ docker image inspect nginx
The above command will return a JSON object containing all the details of the nginx Docker image.
Managing Docker container
A container is created from a Docker image. Use the following command to create a Docker container.
$ docker create nginx
The above command will create a Docker container with a random name from the nginx image.
Multiple Docker containers can be created from a single Docker image. Also, the name of the Docker container can be specified while creating it by using the --name option.
$ docker create --name nginx-1 nginx
It will create a container named nginx-1 from the nginx Docker image.
The create command only creates a container. After creating, start the container using the following command.
$ docker start nginx-1
However, a Docker container can be created and started using a single command.
$ docker run --name nginx-2 nginx
The above command will create and start a container named nginx-2 from the nginx Docker image.
Add the --detach option to run the Docker container in detached mode.
$ docker run --name nginx-3 --detach nginx
Default Docker network does not allow access to the container port from the host machine. In order to access the Docker container port on the host machine, the container's ports are mapped to the host machine. Use -p option to specify port mapping.
$ docker run --name nginx-4 -p 8000:80 --detach nginx
It will run the nginx container and map port 80 of the container with port 8000 of the host machine. Now the nginx server can be accessed through port 8000 of the host machine.
In the case of Webdock perfect server stack, allow traffic on port 8000.
$ sudo ufw allow 8000/tcp
Similarly, any directory of the Docker container can be mapped to the host machine for data persistence. Use the --volume option to map Docker volume to the host machine.
$ docker run --name nginx-5 -p 9000:80 --volume nginx:/var/www/html --detach nginx
It will create a Docker volume on the host machine and map it into the Docker container. List all the Docker volumes using the following command.
$ docker volume ls
To learn more about Docker networks and volumes, please visit this guide.
View all the running docker containers using the following command.
$ docker ps
It will display only running Docker containers. To display all the running including the stopped containers, use the following command.
$ docker ps -a
A running Docker container can be stopped using the following command.
$ docker stop nginx-3
To remove the stopped Docker container, use the following command.
$ docker rm nginx-3
The above command will remove the container named nginx-3 if it is stopped or exited.
To remove a running Docker container use the following command.
$ docker rm -f nginx-5
This command will remove the container named nginx-5 whether it is running or stopped.
To get the logs of a running Docker container for debugging purposes use the following command.
$ docker logs [container name]
Get into the Docker container shell to run some shell commands using the following command.
$ docker exec -it [container name] bash
This command will get you into the Docker container's shell and you can run some basic commands in the container.
The following command returns a JSON object containing detailed information of a Docker container.
$ docker container inspect nginx-2
Check resource utilization by Docker container
Docker containers use the resources of the host machine on which they are running. Check the memory and CPU consumption by Docker containers using the following command.
$ docker stats
This command will stream live memory and CPU utilization by the Docker containers.
Similarly check the storage used by the Docker images, containers, and volumes using the following command.
$ docker system df
This command will return the space utilization by Docker images, containers, and volumes.
This article showed the installation steps to run Docker on a Webdock server and some basic commands to manage Docker containers, images, and volumes.