Docker - Setting Ubuntu



Ubuntu is a very popular OS, based on Linux, famous for its stability, security measures, and wide community that supports it with great enthusiasm. Ubuntu has a huge set of applications, ranging from server administration and desktop computing to embedded systems/IoT.

Docker is an open-source platform for containerization of applications together with their dependencies into manageable units called containers. These containers guarantee consistent environments, portability, isolation, and easier deployments and application management becomes a whole lot easier.

In this chapter, lets learn the various ways to Dockerize an Ubuntu OS into a container.

Prerequisites for Dockerizing Ubuntu

Ensure that you have the following prerequisites before beginning to Dockerize Ubuntu.

  • Docker installed − If not already, you can download and install Docker for your operating system from https://www.docker.com/.
  • Ubuntu image − We will use Docker images to containerize Ubuntu. There are tons of official Ubuntu images on the Docker Hub registry (https://hub.docker.com/).

Method 1: Pulling an Ubuntu Image and Running the Ubuntu Container

Let' understand how you can pull an Ubuntu image and run the Ubuntu container −

Pulling the Docker Image

We can directly pull a pre-built Ubuntu image from the Docker Hub Registry. We can use the docker pull command to pull the image.

For example, we can use the following command to pull the latest Ubuntu image −

$ docker pull ubuntu:latest
Pulling the Docker Image 1

You can verify that the image has been pulled successfully by listing all the Docker images on your local machine.

$ docker images
Pulling the Docker Image 2

Running the Ubuntu Container

Now that we have pulled the Ubuntu Docker image, we can spin up a container using this image using the docker run command.

We can use options like the container name (--name), whether to run it in the background (-d), and whether we need to expose any necessary ports (-p).

$ docker run -it --name my-ubuntu-container ubuntu:latest
Running the Ubuntu Container

Accessing the Ubuntu Shell

The -it flag that we have used in the command allows us to access the bash of the container by making it interactive and attaching a terminal to it. With this, we can directly access the Ubuntu shell within the container.

Method 2: Creating a Dockerfile and Building a Custom Image

Let's understand how to create a Dockerfile and build a custom image −

Creating a Dockerfile

A Dockerfile is just a text document that has all the instructions used to build a custom Docker image. It has instructions that define the base image, packages to install, and commands to run.

Create a file named Dockerfile and paste the below instructions in it.

FROM ubuntu:latest

# Install packages (replace with your desired packages)

RUN apt-get update && apt-get install -y nginx

# Expose a port (if necessary)

EXPOSE 80

# Command to run when the container starts

CMD ["nginx", "-g", "daemon off;"]

Building the Docker Image

Next, navigate to the directory where you have created the Dockerfile, open a terminal, and use the docker build command to create an image from the Dockerfile.

$ docker build -t my-ubuntu-image .
Building the Docker Image 1

Lets verify the image by listing all the images.

$ docker images
Building the Docker Image 2

Running the Container

Now that our image is ready, lets create a container from this image.

$ docker run -itd --name my-ubuntu-container my-ubuntu-image

This starts the container in detached (background) mode.

Running the Container Ubuntu 1

If we list all the containers using the command docker container ls, we can see that our container is running.

Lets attach a shell to this container using the below command.

$ docker exec -it my-ubuntu-container /bin/bash
Running the Container Ubuntu 2

Method 3: Using Docker Compose

Docker Compose is a very useful and widely-used tool that you can use to define and run multi-container Docker applications. With this, you can ease up the process of managing multiple containers, which are often needed for complex applications.

Docker Compose File

Create a file called docker-compose.yml and paste the following content.

version: '3.8'

services:
   ubuntu1:
      image: ubuntu:latest
      command: /bin/bash
   ubuntu2:
      image: ubuntu:latest
      command: /bin/bash
   ubuntu3:
      image: ubuntu:latest
      command: /bin/bash

Explanation

  • version − '3.8': Specifies the Docker Compose version.
  • services − Defines the services (containers) to be created.
  • ubuntu1, ubuntu2, ubuntu3 − These are the names of the individual containers.
  • image − ubuntu:latest: Sets the base image for each container to the latest Ubuntu image.
  • command − bash: Specifies the command to run within the container. In this case, it's a Bash shell.

Building and Running

Lets use the below command to build the image and run the container in the background.

$ docker-compose up -d
Build Image and Run Container 1

Lets list all the running containers to verify.

$ docker container ls -a
Build Image and Run Container 2

Conclusion

With Dockerizing Ubuntu, you will be able to create an isolated and portable environment for your Ubuntu-based applications. In this chapter, we have understood different ways to create an Ubuntu container, be it using a Dockerfile, Docker Compose, or directly pulling pre-built images.

Docker brings along a powerful toolset for managing and deploying your applications. You can easily follow through with the steps in this tutorial to Dockerize your Ubuntu applications and make use of the benefits of containerization.

FAQs on Dockerizing Ubuntu

In this section, we have collected a set of Frequently Asked Questions on dockerizing Ubuntu, followed by their answers.

1. How do I install additional packages in a Dockerized Ubuntu instance?

To install packages in Ubuntu, we can use the apt package manager that is set up inside the container. Include the relevant package installation commands in your Dockerfile or run them interactively from within the container. Ensure that the packages are compatible with the Ubuntu version used in your base image.

2. How do I configure networking for a Dockerized Ubuntu instance?

We can configure networking in an Ubuntu container using Docker's networking features or by configuring networking within the Ubuntu container itself. You can consider using network aliases, port mapping, or connecting to external networks.

3. How do I persist data in a Dockerized Ubuntu instance?

You can use Docker volumes to persist data in an Ubuntu container. We can mount volumes to directories inside the container that can store data to be preserved if the container stops. You can also configure the volumes as per your requirements to ensure data persistence.

Advertisements