Create Dockerfile Ubuntu



Oct 30, 2018 So these were all the commands that we can use with our Dockerfiles. Mentioned below Dockerfile examples for Ubuntu & Fedora, for reference, Ubuntu Dockerfile # Get the base image. FROM ubuntu:16.04 # Install all packages. RUN apt-get update && apt-get -y upgrade && apt-get install -y apache2 && # adding some content for Apache server. The trick is to use useradd instead of its interactive wrapper adduser. I usually create users with: RUN useradd -ms /bin/bash newuser which creates a home directory for the user and ensures that bash is the default shell. Starting Up a Ubuntu Instance To get started with a Docker instance is simple: Ensure your system can run VMs (even though it’s not a VM per se, it still needs the access for abstraction).

  1. Dockerfile Build Ubuntu Image
  2. Dockerfile Create Ubuntu Container

Docker provides a simple way to configure any docker image and create your own custom image with the help of the dockerfile.

In this tutorial, you will learn how to pull an official image from the Docker repository and customize it according to your own requirements. And then you can run and verify the custom docker image.

Creating custom docker image

So, in this example, you’ll be using an Alpine Linux image that does not include the Vim editor by default. You’ll modify this image to create a new docker image of Alpine Linux that includes Vim editor by default. It cannot get any simpler than this. can it?

Prerequisite

If you have not already, please install Docker on Ubuntu or whichever Linux distribution you are using. Make sure to add yourself to the docker group so that you can run docker without sudo.
You’ll need active internet connection for downloading the base docker image.

Step 1: Get docker image [optional]

I chose Alpine Linux in this example because it is really small. The Alpine docker image is hardly 5 MB in size, can you believe it? It’s the perfect Linux distribution for containerization.

This step is optional. I included it show that you can compare it with the customized docker image.

Create Dockerfile Ubuntu

Pull the latest docker image of Alpine Linux using docker pull command:

Step 2: Create Dockerfile with the needed customization

Now let’s create a new empty file named Dockerfile using touch command.

Now you need to edit this file and these three lines to it and save it. You can use an edition like Vim or Nano or use cat command to add these lines to the Dockerfile.

What you are doing here is to create a new docker image by downloading the latest Alpine docker image from the Docker Hub.

Like apt, Alpine uses apk package manager. So the next two commands are basically telling Alpine linux to update the available package cache (apk update) and then install Vim (apk add vim).

As you can see, with RUN in Dockerfile, you can customize your base docker image by running specific commands.

Step 3: Create the custom docker image with Dockerfile

The command to build the custom image from the Dockerfile looks like this:

With the -t tag, you specify the name of your custom docker image.

Considering that your Dockerfile is in your current directory, you can create the new docker image of Alpine Linux with Vim installed like this:

Let’s see the available Docker images on the system now:

You can see that the base docker image which was hardly 5 MB in size is now 33 MB with Vim installed on it (and the package cache updated).

Dockerfile Build Ubuntu Image

Now, let’s verify that your modified docker images has vim installed by running a container from it:

Once you are inside the container, you can verify that Vim is installed by checking its version:

Exit the container by typing exit in the terminal. Stop the container, remove the container and remove the docker images (if you want to) to free up disk space.

Dockerfile Create Ubuntu Container

Congrats! You just learned how to create your very own customized docker image.

I know it’s not a very extensive tutorial and you may have complex need. But this tutorial works as the first step towards understanding docker image customization.

I highly recommend reading more on Dockerfile to know what other options you have available for customizing docker images.

If you have questions or suggestions, please leave a comment below.

Become a Member for FREE
Become a member to get the regular Linux newsletter (2-4 times a month) and access member-only contents.

Join the conversation.