You can use Docker to automate the application deployment in your organization; it will boost employee productivity and create a more agile development environment to manage resources better. That’s why we are here to demonstrate how to install and configure Docker on the Ubuntu system. So, let’s start!
How to install Docker on Ubuntu
First of all, you have to update your system repositories:
Now, we will install some essential packages required for installing Docker on the system:
$ sudo apt install apt-transport-https ca-certificates curl software-properties-common
Enter “y/Y” to permits the installation process to continue:
Utilize the below-given command for adding the GPG key of the Docker repository:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –
If the execution of the command shows “OK“, it means the GPG key is successfully added to our system:
Next, we will add the Docker repository to Ubuntu system:
$ sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable”
Again, update the repositories of your Ubuntu system:
Now, execute the “apt-cache” command to ensure that we are going to install the application from the Docker repository:
$ apt-cache policy docker-ce
Install Docker on your system by utilizing this command in your terminal:
$ sudo apt install docker-ce
Enter “y/Y” to permit the Docker installation process to continue:
After the successful installation of Docker, check out its status on your system:
$ sudo systemctl status docker
How to add a user to Docker group on Ubuntu
Command related to Docker requires you to be a “root” user or a “docker” group member. If you are not working as a root user, you can use the “usermod” command to add the current user to the “docker” group:
$ sudo usermod -aG docker $USER
Here, the “-aG” option will append the current user “$User” to the “docker” group:
Now, log out and log in back to the system for applying the new “docker” membership:
If you want to add a user that is not logged in to the system, then execute the below-given command by adding its username:
$ sudo usermod -aG docker username
How to use docker command on Ubuntu
To view information about docker such as its syntax, option, commands, type “docker” in your terminal:
Also, execute the below-given command for knowing the system-wide information related to Docker:
How to work with Docker images on Ubuntu
Docker images are utilized for creating Docker containers. You can “push” these images to your Docker repository, whereas Docker “pull” these images from the Docker Hub by default. Docker Hub permits anyone to host their images; thus, most Linux distros and related images are hosted in the Docker Hub.
First of all, we will check that we have access to download images from Docker Hub. For this, execute the sample command given below:
$ sudo docker run hello-world
The output is declaring that Docker is enabled to find our specified “hello-world” image locally. Then, the Docker application utilized Docker Hub for downloading it. Then Docker creates a new container from the “hello-world” image. Lastly, the application present in the container showed output on our Ubuntu terminal:
How to search for a Docker image on Ubuntu
The “docker search” command is utilized in the Ubuntu terminal for searching any Docker image available on the Docker Hub. For instance, we will search for the “ubuntu” image on the Docker Hub but if you want to search for another Docker image, then specify its name where we added “ubuntu”:
The output of command mentioned above will list out all the Docker images whose names successfully matched with our search string “ubuntu”:
How to pull a Docker image on Ubuntu
The “docker pull” command is used to download a Docker image to your Ubuntu system. For instance, we have searched for the “ubuntu” image, and we found out that it is available on Docker Hub. Now, to download the “ubuntu” image, we will execute the “docker pull” command in the following way:
The output signifies that the “ubuntu” image is successfully downloaded to our system:
To verify the existence of the downloaded images on your system, execute this command:
How to run a Docker container on Ubuntu
Now, we will demonstrate how you can run a Docker container with “ubuntu” image. To do so, utilize the below-given command in your terminal:
Here the combination of the “-it” option will permit you to interact with the container using the shell access:
The output is also displaying our container ID, which is “bdee2efafad“. You should note your container ID because you will later need it to manage this container on your system.
How to update packages inside a Docker container on Ubuntu
If you want the package present in the current Docker container, then execute the “apt update” command in the following way:
How to install an application in a Docker container on Ubuntu
Docker container also provides you access to install any specified application in it. For instance, in our Docker container, we will install “Node.js”:
Enter “y/Y” to permit the Node.ls installation process to continue:
The error-free output indicates that Node.js is successfully installed inside our Docker container. To verify its existence, execute this command:
This command will display the version of the Node.js application installed inside the Docker container:
How to list Docker containers on Ubuntu
In your Ubuntu terminal, utilize the “docker ps” command for listing the active Docker container on your system:
You can add the “-a” option in the same command if you want to extract all Docker containers, including the active and inactive ones:
Currently, we do not have any inactive Docker containers; that’s why the output is only showing the information related to active containers:
To check the latest created Docker container, add the “-l” option to the “docker ps” command:
How to start a Docker container on Ubuntu
The “docker start” command is used to start a Docker container on your system. In this command, we add the container ID of the specific Docker container. For instance, we will start the “ubuntu” Docker container by adding its container ID “3bdee2efafad” in the below-given command:
$ sudo docker start 3bdee2efafad
How to stop a Docker container on Ubuntu
In our system, the name of the container “ubuntu” assigned by Docker is “brave_brown”. We will specify this name in the “docker stop” command to stop the running “ubuntu” container:
$ sudo docker stop brave_brown
How to commit changes to a new Docker image on Ubuntu
The changes you make in your Docker container are only limited to itself, such as starting and stopping the Docker container, adding files in it, etc. However, once you completely remove the Docker container, all of the applied changes will be lost. Docker offers you the function of committing these changes to a new instance of Docker image. It can be handy in a situation where you want to reuse a container for some other purpose. You can operate by committing changes of a Docker container to a new Docker image with the help of the “docker commit” command.
Now, check out the below-given syntax of “docker commit” command:
$ docker commit -m “description of changes” -a “Author_Name” [Container_ID] [Repository]/[Docker_Image_Name]
- “-m” option is added to commit the message.
- “-a” is utilized for specifying the name of the author.
- “Author_Name” will be your username.
- [Container_ID] is the ID of a specific Docker Container for which you want to commit the changes.
- If you have created any repository on the Docker Hub, then add its name in the [Repository]; otherwise, you will write your Docker username in its place.
- Lastly, specify the name of the new Docker image in the [Docker_Image_Name].
For instance, for the user “linuxhint” having the container ID “3bdee2efafad” the “docker command” will be:
$ sudo docker commit -m “added Node.js” -a “linuxhint” 3bdee2efafad linuxhint/ubuntu-nodejs
In the commit mentioned above, we have specified that in this Docker container, we have added NodeJS:
Again, list out the Docker images to ensure that new Docker image “ubuntu-nodejs” is created or not:
How to push a Docker image to a Docker Hub on Ubuntu
At this point, the changes we committed to a new Docker image are saved on our local system. You may want to share the new Docker image with any friend, team members, or with the whole world on Docker Hub for development purposes. To do so, you should have an account on Docker Hub so that you can push the newly created image to the repository on Docker Hub.
If you already hold a Docker Hub account, then skip this step. In the other case, you can create a new Docker account using the following link.
Enter your Docker username, Email ID, and password for creating a new account:
Click on the “Create a repository” option for creating a Docker repository on Docker Hub:
Specify the name of the repository and its type of visibility. If you want to make you Docker repository appear in search result then make it “Public”, otherwise keep it “Private”:
Here, you can see the information related to the newly created repository:
If the username of your local system and your Docker registry name is different, then you have to tag the newly created Docker image with the Docker registry username.
Firstly, log into your Docker hub account using your terminal:
$ sudo docker login docker.io
Now, we will utilize the “docker tag” command to tag the “ubuntu-nodejs” images created by “linuxhint” user with the “sharqa” Docker registry name:
$ sudo docker tag linuxhint/ubuntu-nodejs sharqa/ubuntu-nodejs
After logging into the Docker account, we will push the newly created Docker image into our Docker repository present on the Docker Hub.
Now, check out the syntax of “docker push” command:
$ sudo docker push [Docker_registry_username]/[Docker_image_name]
In the command mentioned above, specify your Docker registry name, which is “sharqa” in our case, then add a “/” followed by the Docker image name:
$ sudo docker push sharqa/ubuntu-node.js
The output is showing that we have successfully pushed into our Docker registry. Verify it by exploring your Docker Hub account:
Here you can see the information related to the pushed “ubuntu-nodejs” image:
How to remove a Docker container on Ubuntu
You cannot remove a running Docker container from your system by using the “docker rm” command. The first thing you need to do is stop the Docker container with “docker stop” and then utilize the “docker rm” command to remove it from your system.
Now, we will stop the “ubuntu” container from our system:
$ sudo docker stop brave_brown
Here “brave_brown” is the name of the “ubuntu” container assigned by Docker. Write out the below-given “rm” command to remove it:
$ sudo docker rm brave_brown
To confirm the removal of the “ubuntu” container, again list out the Docker container by executing the “docker ps” command:
You can see that the “ubuntu” container is nowhere in the list, which means that we have successfully deleted this container.
Developers can use Docker as a virtualization platform to create lightweight, self-contained, and portable application containers that make it simple to develop, test, and deploy applications. This article demonstrated how to install and configure Docker on Ubuntu. We showed you how to create, stop, and remove a container. Moreover, this article also provided the procedure of pulling and pushing the Docker images to Docker Hub.