Installing Docker on Ubuntu

1. Open the terminal on Ubuntu.

2. Remove any Docker files that are running in the system, using the following command:

$ sudo apt-get remove docker docker-engine docker.io

After entering the above command, you will need to enter the password of the root and press enter.

3. Check if the system is up-to-date using the following command:

$ sudo apt-get update

4. Install Docker using the following command:

$ sudo apt install docker.io

You’ll then get a prompt asking you to choose between y/n – choose y

5. Install all the dependency packages using the following command:

$ sudo snap install docker

6. Before testing Docker, check the version installed using the following command:

$ docker –version

7. Pull an image from the Docker hub using the following command:

$ sudo docker run hello-world

Here, hello-world is the docker image present on the Docker hub.

8. Check if the docker image has been pulled and is present in your system using the following command:

$ sudo docker images

9. To display all the containers pulled, use the following command:

$ sudo docker ps -a

10. To check for containers in a running state, use the following command:

$ sudo docker ps

You’ve just successfully installed Docker on Ubuntu!

Executing the Docker Command Without Sudo 

Executing Docker after installing it on Ubuntu (Docker Installation on Ubuntu)  instructions with sudo guarantees that they are run either by a user who belongs to the docker group or with elevated security privileges (by using sudo). 

By default, unless the appropriate user is introduced to the docker group, the docker operations cannot be executed without sudo. Follow these instructions to execute the Docker operation without sudo.

First, establish the Docker Group:

Making the docker group is the initial step. Run a particular phrase in a terminal window if it doesn’t already exist:

$ sudo groupadd docker

Next, include the user in the Docker group:

The next thing to do must add the individual to the group for Docker; execute the command line below to do so:

$ sudo usermod -aG docker $USER

Third step: restart the computer:

Reboot the computer now to view the modifications you made.

Using the Docker Commands

Mastering docker commands may enhance the framework’s capability after installing Docker on Ubuntu. 

  • To activate the logging, utilize the -f parameter.
  • To divide JSON, use Docker by default; to extract individual keys, use JQ.
  • In your Container file, there are quite a few areas where commands may be specified.
  • Posting to the volumes could be more effective while the picture is being built.
  • Docker offers a highly efficient way to create an alias for its own built-in commands. This makes it easier to set up and handle lengthy and enormous orders. These alias values are stored in the directories /.bashrc or and /.bash_aliases.
  • Docker offers further assistance to remove unused code fragments from the installation of the container.
  • Docker always favors reading statements from the container file that have not changed. Therefore, time savings may be realized by arranging what is shown in the container file in a way that ensures the elements that are susceptible to change are shown towards the end of the document and those that are most likely to undergo change are shown at the top.

Working with Docker Images

Docker images are directions contained in a specific file termed a Dockerfile. It has a unique syntax and outlines the actions Docker is going to perform to construct the container you’re using.

Because containers are just tiers upon countless modifications, each new function you run in an image of Docker will add another layer to the container.

The highest layer is a light programmable layer. The individual using it may modify an empty layer and commit it with the docker push command.

STEP 1: You may use the following command to retrieve the Docker Pictures in your local Container repo

sudo docker images

STEP 2: You may use the -a switch to see a list of all Docker Photos, which comprises intermediary images.

sudo docker images -a

STEP 3: Typically, just the first twelve letters of an image’s ID are shown when listing images for Docker. Utilise the -no-trunc switch to see images from Docker having extended Image Ids.

sudo docker images –no-trunc

STEP 4: The Docker Search Command: The typical syntax for a Docker search command is: 

sudo docker search <image-name>

Running a Docker Container

The Docker run function controls container execution. Launching the Docker container is the initial step in running a container in asynchronous mode.

sudo docker run –it centos /bin/bash 

After that, use Ctrl+p to navigate back to your OS terminal.

Managing a Docker Container

1. Listing Docker Containers

Execute this command to display the containers:

# docker ps [ OPTIONS ]

2. Starting a Docker Container

Start a container with Docker using the command shown below:

# docker run [ OPTIONS ]  IMAGE[:TAG] 

3. Stopping a Docker Container

One, many, or all containers can be stopped at once. Use the following syntax:

docker stop [-t|–time[=10]] CONTAINER [CONTAINER…]

How to Commit Changes in a Docker Container?

One of the fundamental functions when dealing with images for Docker and containers is to commit changes to an existing Docker image. Once you commit to modifications, you effectively make a new picture with a modified underlying image overlay on top.

1. Retrieve a Docker image.

sudo docker pull ubuntu

2. You will find the Ubuntu image if you check the list of accessible images again:

sudo docker images

3. Activate the Container

sudo docker run -it cf0f3ca922e0 bin/bash

The container is told to begin operating in a collaborative state and allow the terminal’s typing access with the -it settings. The option opens another container and takes you to a fresh shell window so you can start functioning within.

4. Commit Changes

Finally, publish the modifications by using the code syntax shown below to produce an alternate picture.

sudo docker commit [CONTAINER_ID] [new_image_name]

5. Move out of the modified container when you’ve finished modifying it

Exit

6. To preserve the adjustments you applied to the existing picture, you must have the CONTAINER ID. Take note of the ID value in the final result. 

Pushing an Image into Private Docker Repositories

The picture must first be ready by having the appropriate aliases and label, if applicable. This may be accomplished by creating a picture or by modifying a previously created image. Then we build the image using syntax. Sometimes we choose to push an existing picture rather than creating one from scratch. 

To tag the picture with the correct name/alias before pushing it into our repository, use the command below:

docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

The command below may be used to check the outcome:

docker images

We may push the image of Docker to our personal repo now that it has been constructed. The DockerHub repository must first be logged into using this instruction:

docker login

The picture must be pushed as the last step using the subsequent command line:

docker push [OPTIONS] NAME[:TAG]

Engaging with Docker Volumes

When working with containers, Docker volumes are popular and practical solutions for assuring data durability. They are more effective than building extra editable layers, which enlarge the dimension of the Docker image. 

When starting a container, there are many approaches to attaching a Docker volume. The –mount and -v parameters have been introduced into the docker start command, allowing users to choose amongst them. 

Use this command to build a Docker Volume:

docker volume create [volume_name]

For instance, you would use the following command to create a disc with its title data:

docker volume create data

The inspect function may be utilized to find additional details regarding a Docker volume

docker volume inspect [volume_name]

It provides information about a volume, such as where it resides on the file system that hosts it (Mountpoint). The folder mentioned beneath the mountpoint route also contains everything kept on the data drive.

A Docker volume can be deleted using the syntax that follows on its command line:

docker volume rm [volume_name]

Docker Network Commands

A single server hosting a Docker Engine version houses an interconnected network. Multiple hosts with independent engines can be part of a network with overlays. Docker Network Build will build a link between two networks  for you if you just provide the network’s name when you carry out the command.

Overlapping networks can only be created if certain prerequisites already exist, compared to bridge networks. These circumstances are: 

  1. A key-value storage is accessible. Key-value databases supported by the engine.
  2. Servers connected with the key-value storage in the cluster.
  3. On each computer in the swarm, an Engine daemon is correctly set up.

The layered network can be enabled by the following docker options:

  • –cluster-store
  • –cluster-store-opt
  • –cluster-advertise

Engine by default produces a distinct subnetwork for the entire network when you build one. Through the use of the –subnet option, you may modify this default and manually define a subnetwork. You are limited to choosing a single subnet on the bridged network. Several subnets are supported via a network with overlay.