In this article, I will show you how to install and use Docker on Ubuntu 18.04 LTS.
Adding the Docker Package Repository:
In this section, I will show you how to add Docker package repository on Ubuntu 18.04 LTS.
First update the APT package repository cache of your Ubuntu 18.04 LTS machine with the following command:
The APT package repository cache should be updated.
Now run the following command to install some additional packages required to add Docker package repository on Ubuntu 18.04 LTS:
properties-common
Now press y and then press <Enter> to continue.
The required packages should be installed.
Now add the official GPG key of the Docker repository on your Ubuntu 18.04 LTS machine with the following command:
The GPG key should be added.
Now add the official package repository of Docker with the following command:
$(lsb_release -cs) stable"
The official Docker package repository should be added and the APT package repository cache should be updated as well.
Installing Docker on Ubuntu 18.04 LTS:
Now that everything is ready, you can install Docker on Ubuntu 18.04 LTS with the following command:
Now press y and then press <Enter> to continue.
All the required packages should be downloaded and installed.
Docker is installed.
Now check whether Docker service is running with the following command:
As you can see, the Docker service is running.
If it is not running, then start Docker with the following command:
Now should add Docker service to the system startup so that it will start automatically on system boot.
Run the following command to add Docker service to the system startup:
Now check whether Docker is working with the following command:
Running Docker without Root Privileges:
By default, you must run Docker as root user. You can also run Docker with sudo if it is configured on your Ubuntu 18.04 LTS machine. If you don’t want to use Docker as root user or with sudo, then you should add your login user to the docker group.
Add your login user to the docker group with the following command:
Now reboot your computer with the following command:
Now you should be able to run Docker without sudo or root user.
Searching for Docker Images:
Docker image repository has images for almost anything you need. For example, if you want a php server, you can install a php server image and you will be ready to run php scripts.
You can search for a Docker image, let’s say a Docker image for Node.js, in the official Docker image repository with the following command:
As you can see, all the Docker images for Node.js is displayed. To install a Docker image, you just have to type in the image name is the NAME column as marked in the screenshot below.
Downloading a Docker Image:
Now let’s say, you want to download the mhart/alpine-node image of Docker. Downloading a Docker image is called pulling a Docker image in the Docker term.
To pull mhart/alpine-node Docker image, run the following command:
The Docker image should be pulled.
Listing Downloaded Docker Image:
To list all the Docker images you pulled and is available in your machine, run the following command:
As you can see, the Node.js image I pulled is listed.
Running a Node.js Script with Docker:
In this section, I will show you how to run a Node.js script index.js with the Docker Node.js image mhart/alpine-node that I just pulled from the official Docker image repository.
First create a project directory with the following command:
Now navigate to the project directory with the following command:
Now create index.js and open it with nano text editor with the following command:
Now type in the following line and save the file.
Now you can run the Node.js script index.js with
hello mhart/alpine-node node index.js
As you can see, the index.js Node.js script ran successfully and the correct output was printed.
Now let’s explain what’s happening here.
- -it flags are used to attach the current STDIN and STDOUT to docker and run a command which is node index.js
- –name hello_node – Set’s hello_node as the name of the running container.
- –rm flag removes any other running container with the same name as this container.
- -v “$PWD”:/usr/src/app/hello – Mounts the current working directory of your Ubuntu 18.04 LTS machine to the /usr/src/app/hello directory of the Docker container.
- -w /usr/src/app/hello – Navigate to the /usr/src/app/hello directory in the Docker container.
- mhart/alpine-node – Name of the Docker image that this container will be based on.
- node index.js – It is the command that will be run inside the container.
So that’s how you install and use Docker on Ubuntu 18.04 LTS. Thanks for reading this article.