Grid Guides

Explore How System On Grid Can Work For You

How to Install and Use Docker on Ubuntu 18.04


Table of Contents

Introduction

Docker is an application which simplifies the process of Managing application processes in containers. Docker is an Open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly.

Prerequisites

To install Docker you must do Initial Server Setup with Ubuntu 18.04, including a sudo non-root user and a firewall and an account on Docker Hub if you want to create your images and push them into Docker Hub.

How to Install Docker

Docker Installation package is available in the official Ubuntu repository. To do that we will add a new package source, and add the GPG key from Docker to ensure the downloads are valid, and then you can install the package. First of all, you have to update your existing list of packages.

$ sudo apt update

After that, install some prerequisite packages those are apt use packages over HTTPS:

$ sudo apt install apt-transport-https ca-certificates cur software-properties-common

And then add the GPG key for the official Docker repository to your system.

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

You have to add the Docker repository to your system.

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"

And then you have to update the package database with Docker package from the newly added repository.

$ sudo apt update

And ensure that you are yet to install from the Docker Repository instead of the default Ubuntu repository.

$ apt-cache policy docker-ce

Then, You will get output like this.

So far now, Docker is not installed. Now you have to install the Docker:

$ sudo apt install docker-ce

Now the Docker installed. You have to enable separately to start the daemon. To check that whether it is running or not enter the below command.

$ sudo systemctl status docker

Then you will get output like this:

After installing the Docker you will get docker service along with Docker command line utility.

How to Use the Docker Command

In Docker command, you have a chain of options, commands and arguments as given below.

$ docker [option] [command] [arguments]

If you want to view all available commands then use the below command.

$ docker

If you want to check the options available for a particular command, then enter the below command.

$ docker docker-subcommand --help

You can view the system information of Docker, use the below command.

$ docker info

How to Work with Docker Images

Docker images are required to build Docker Containers. Generally Docker pulls these images from Docker Hub. It is a registry managed by Docker. You can host your Docker images on Docker Hub. If you want to check whether you can access your images from Docker Hub, then enter below command.

$ docker run hello-world

Then you can get output like this:

At first Docker unable to find the hello-world image locally, So it will be downloaded the image from the Docker Hub, is a default repository. Once the image downloaded, Docker created a container for that image. So the application within the container is executed, and it will display the message. If you want to search for images which are available in Docker Hub use the following command. For example, see below command.

$ docker search ubuntu

It will crawl the Docker Hub and return a list of all required images like the following output for above command.

OK in the OFFICIAL column indicates that the image built and also supported by the company. If you identify the image that you want to use, then you can download it your system by using pull command.

For example, run the below command to download the official Ubuntu image to your system.

$ docker pull ubuntu

Then you will get below output.

Once the image is downloaded, you can run a container using that image with run command. If you want to see the images that have been downloaded to your computer, then enter below command.

$ docker images

Then you will get output like this:

How to Run a Docker Container

Let’s execute a container using the latest image of Ubuntu. The association of -i and -t gives access to the container.

$ docker run -it ubuntu

Now your command prompt should change and reflect that you are not working inside the container. You will get below output.

Note that container ID to identify the container. So from now, you can run any command inside the container. For example, if you want to update the package database inside the container. From now you don’t need to prefix any command with sudo, as you are executing inside the container as the root user.

apt update

Now you can install any application inside it, For example, let’s install Node.js:

apt install nodejs 

This installs the Node.js in the container from the official Ubuntu repository. If you want to verify whether the Node.js is installed or not enter the below command.

node -v

Now you can see the version number displayed in your terminal.

You can exit from that prompt by entering the below command.

How to Manage Docker Containers

In Docker containers, You may have more active and inactive containers on your system. You can view the active ones by using the below command.

$ docker ps

Output:

If you want to view all the containers then you can enter below command.

$ docker ps -a

Output

If you want to view the latest containers you have created, pass the -l switch:

$ docker ps -l

If you want to start a stopped container then use docker start followed by the container ID or Containers name as shown below:

docker start 0000dbad2f7b

Now the container start, now you can use docker ps to view its status:

If you want to stop a running container then use the docker stop, followed by the container ID or container name.

docker stop optimistic_chatterjee

If you want to remove your container then do it by entering below command.

docker rm heuristic_nestorf

You can start a new container then name it by using --name switch. You can also use --rm switch to create a container that removes automatically when it is stopped. If you need more information run help command.

Committing Changes in a Containers to a Docker Image

If you destroy your Docker image using rm command then you will not get back it will be lost. When you start up a Docker image, you can create, modify and delete files as you wish on your virtual machine. Once you have installed the Node.js inside the Ubuntu container , now you will be having a container running off an image, but the container is different from the image you used to create it. Commit the changes to a new Docker image instance using the below command.

docker commit -m "What you did to the image" -a "Author Name" container_id repository/new_image_name

The -m switch is for the commit message which helps you and others know what you changes you made and -a is used to specify the author. The repository is usually your Docker Hub username until you create additional repositories on Docker Hub.

If you want to create any repository use the below command.

docker commit -m "added Node.js" -a "ubuntu" 0000dbad2f7b ubuntu/ubuntu-nodejs

Once you commit an image, the new image is saved locally on your computer. Listing the Docker images again will show the new image, and also the old one. To get it to use the below command.

$ docker images

Conclusion

In this guide, we have described that installing Docker, and working with images and containers and pushing a modified image to a Docker Hub.