Blog

Blog

Install Docker – Docker Installation On Ubuntu And CentOS

image 77

Docker Installation On Ubuntu and CentOS

Docker is a containerization platform that allows developers to easily create, deploy, and run applications in isolated containers.

In This Tutorial we will ahave a Knowledge on

What is Docker?

Docker is a platform for developing, shipping, and running applications in containers. A container is a lightweight, standalone executable package of software that includes everything needed to run an application, including the code, libraries, and system tools.

Docker makes it easier to develop, deploy, and manage applications by providing a consistent environment across different systems, whether they are local development machines, testing servers, or production servers. It allows developers to package their applications and dependencies into a single container that can be easily shared and deployed on any platform that supports Docker.

Using Docker, developers can create, run, and manage containers without the need for a separate virtual machine or operating system. Docker provides a set of tools and APIs for building, distributing, and running containers, and supports a wide range of programming languages, platforms, and infrastructure.

A step-by-step guide to installing Docker on Ubuntu and CentOS

image

In this tutorial, we are going to see how to install Docker on Ubuntu and CentOS. Installation is very straightforward and easy too. Docker won’t run on all OS versions of Ubuntu and CentOS. Docker expects a 64-bit OS. You have to check your OS version before installing Docker.

Let’s start the installation. It is a step-by-step guide.

Install Docker On Ubuntu:

Docker is a containerization platform that allows developers to easily pack, ship, and run applications in containers.

To install Docker on Ubuntu:

  1. Update the apt package index:
sudo apt-get update

2. Install packages to allow apt to use a repository over HTTPS:

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

3. Add the Docker official GPG key:

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

4. Add the Docker repository to APT sources:

sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

5. Update the apt package index:

sudo apt-get update

6. Install the latest version of Docker:

sudo apt-get install docker-ce docker-ce-cli containerd.io

Install On CentOS:

To install Docker on CentOS:

  1. Uninstall older versions of Docker:
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

2. Install required packages:

sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2

3. Add the Docker repository:

sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

4. Update the yum package index:

sudo yum makecache fast

5. Install the latest version of Docker:

sudo yum install docker-ce

6. Start the Docker service:

sudo systemctl start docker

You can verify the installation by running the command “docker -v” which should show you the version of Docker installed.

Examples:

Example of how to run a simple “Hello World” program in a Docker container

Here is an example of how to run a simple “Hello World” program in a Docker container:

# Create a new container and run the "Hello World" command in it
docker run busybox echo "Hello World"

This command will download the busybox image from the Docker Hub, start a new container from it, and run the “echo” command inside the container, which will print “Hello World” to the console.

You can also run a simple web server in a container by using the command:

# Run a simple web server in a container
docker run -d -p 80:80 nginx

This command will download the Nginx image from the Docker Hub, start a new container from it, and run the Nginx server inside the container, which will be accessible on port 80 of your host machine.

Example of running a simple web app using Docker Compose:

here’s an example of running a simple web app using Docker Compose:

  1. Create a new directory for your project and navigate to it in your terminal.
  2. Create a new file named “docker-compose.yml” in the project directory.
  3. In the “docker-compose.yml” file, define the services for your web app. For example:
version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"

This defines a service named “web” that builds an image from the current directory (which should contain a Dockerfile), and maps port 5000 of the container to port 5000 of the host.

  1. Create a new file named “Dockerfile” in the project directory.
  2. In the “Dockerfile” file, define the steps for building your web app image. For example:
FROM python:3.8-slim-buster
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
CMD [ "python3", "app.py" ]

This defines a base image (in this case, Python 3.8) and sets up a working directory for the app. It then copies the requirements.txt file and installs the dependencies, copies the rest of the app files, and sets the command to run the app using “app.py”.

  1. Create a new file named “app.py” in the project directory.
  2. In the “app.py” file, define a simple web app using a framework like Flask. For example:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
    return 'Hello, World!'
if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

This defines a Flask app that returns the string “Hello, World!” when accessed at the root URL.

  1. Build and start the services using Docker Compose by running the following command in your terminal:
docker-compose up

This will build the image and start the container, and you should see output similar to the following:

Starting simple-web-app_web_1 ... done
Attaching to simple-web-app_web_1
web_1  |  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
web_1  |  * Restarting with stat
web_1  |  * Debugger is active!
web_1  |  * Debugger PIN: 123-456-789
  1. Open a web browser and navigate to “http://localhost:5000”. You should see the message “Hello, World!” displayed in the browser.

That’s it! You have successfully run a simple web app using Docker Compose.

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
Click outside to hide the comparison bar
Compare

Subscribe to Newsletter

Stay ahead of the rapidly evolving world of technology with our news letters. Subscribe now!