1. Home
  2. Linux
  3. How to build your own docker image on linux

How to build your own Docker image on Linux

Have you ever wanted to build your own Docker image? As it turns out, with something called a “Dockerfile,” you can. In this guide, we’ll go over how to build your own Docker image.

How to install Docker on Linux

You’ll need to have Docker installed on your Linux server before you attempt to build a new image. Thankfully, docker is easy to install on a wide variety of Linux operating systems. In this guide, we’ll focus on Ubuntu, Debian, Arch Linux, Fedora, and OpenSUSE.

To install Docker, open up a terminal. Once it is open, the installation is ready to begin. Follow the installation instructions outlined below that match the Linux operating system that you use.

Ubuntu/Debian

You’ll first need to update the software index on your Ubuntu/Debian system. To do that, run the apt update command below.

sudo apt update

You must now install the HTTPS transport package to install over HTTPS. You’ll also need to install Curl and a few other packages. These packages can be installed with the apt install command.

sudo apt install apt-transport-https ca-certificates curl software-properties-common

With the packages installed, it is time to enable the Docker GPG key on your Ubuntu/Debian system. This key ensures that it is safe to install packages on Ubuntu.

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

With the GPG key enabled, it is time to enable the Docker repo. To do this, execute the following echo command in a terminal.

echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

When you’ve enabled the Docker repo, re-run the apt update command to refresh the software index.

sudo apt update

Finally, you can install the Docker packages you need on Ubuntu or Debian.

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

Arch Linux

If you use Arch Linux, you can simply run the pacman -S command and install the “docker” package to get Docker working on your system.

sudo pacman -S docker

With the Docker package installed on your Arch system, run the following systemctl commands to enable the software.

sudo systemctl enable docker

sudo systemctl start docker

Fedora

Getting Docker working on Fedora is fairly straightforward. To start, use the dnf install command to install the software on your system.

sudo dnf install docker

Once the package is installed on your Fedora system, enable it with systemd.

sudo systemctl enable docker

sudo systemctl start docker

OpenSUSE

To get Docker working on OpenSUSE, you must add the Docker repo.

sudo zypper addrepo https://download.docker.com/linux/opensuse/$(awk -F= '/^VERSION_ID/{print $2}' /etc/os-release)/x86_64/stable docker

Next, refresh Zypper and install the software.

sudo zypper refresh
sudo zypper install docker-ce

Finally, enable Docker on your system with systemd.

sudo systemctl enable docker
sudo systemctl start docker

How to choose your base image

Every Docker image taken from DockerHub is built with a base image. So, you’ll need to choose a base image for your Docker container. In this guide, we’ll use Ubuntu, as it is one of the most compatible Linux OSes.

In this guide, we’ll show you an example of how to build a Drupal 10 image with Ubuntu 22.04. However, these build instructions can apply to anything you wish to create, from web apps to Python programs, etc.

To start, open up the Nano text editor in a terminal. You’ll be using Nano to craft your own Dockerfile.

nano -w ~/dockerfile

Inside the Nano text editor, enter the following code to specify you wish to use Ubuntu. To get Ubuntu 22.04, specify it. If you wish to use a different release of Ubuntu, information can be found here.

# Use the official Ubuntu base image
FROM ubuntu:22.04

How to set up the working directory

You must set up a working directory in your Docker container. The working directory is where the application is deployed on Ubuntu in the container. In this example, we’ll be working with Drupal 10. Drupal is a web application, so we’ll need to set the WORKDIR as /var/www/html/.

In the Nano text editor, press the Enter key to make a space. Then, paste the following code.

# Set the working directory in the container
WORKDIR /var/www/html

How to install the program dependencies

The application you deploy in your Docker image needs to have its dependencies met. If the correct dependencies are not installed into the image, it will not work. We are creating a Drupal 10 image based on Ubuntu 22.04, so we must add all Drupal 10 dependencies to the file.

In the Nano text editor, press the Enter key to make a space. Then, paste the following code.

# Install dependencies
RUN apt-get update && apt-get install -y \
apache2 \
php \
libapache2-mod-php \
php-curl \
php-gd \
php-intl \
php-mbstring \
php-mysql \
php-pgsql \
php-xml \
php-zip \
unzip

How to copy the program code

The next step in the process is to provide your Docker file with the program code. Once again, we are using Drupal 10 as an example. So, we must provide the Dockerfile with the means to download and extract Drupal 10.

In the Nano text editor, press the Enter key to create a new line. Then, add the following code so that your Dockerfile can download Drupal 10.

# Download and install Drupal
RUN wget https://ftp.drupal.org/files/projects/drupal-10.2.tar.gz && \
  tar -xvf drupal-10.2.tar.gz && \
  mv drupal-10.2 .

How to expose ports

Every time you deploy a Docker container, it accesses ports. For example, if you run the default Syncthing container, it exposes port 8384. Nextcloud? Port 8080 and 80. Your container needs to expose the correct ports in order to work right.

First, figure out what ports the program you are deploying needs access to. In this example, we’re deploying Drupal 10. As Drupal 10 is a CMS system for a web server, it needs port 80. So, in the Nano text file, we expose port 80.

# Expose the Drupal port
EXPOSE 80

How to set the default command

You need to set the default command in your Dockerfile so that when your image is built and deployed, it automatically begins running the program. In our example, we want Drupal 10 to instantly start once the container is deployed, so we need to add a launch command to the Dockerfile.

In the Nano text editor, press Enter to create a new space in the file. Then, paste the following code.

# Start Apache in the foreground
CMD ["apache2ctl", "-D", "FOREGROUND"]

When you’ve added this last bit of code to the file, save it by pressing Ctrl + O, and exit the Nano text editor with Ctrl + X. The Dockerfile should look like the code below.

Note: this is an example Dockerfile. You can use the Drupal example as a reference to create your own for any application you wish.

FROM ubuntu:22.04

# Set the working directory
WORKDIR /var/www/html

# Install dependencies
RUN apt-get update && apt-get install -y \
apache2 \
php \
libapache2-mod-php \
php-curl \
php-gd \
php-intl \
php-mbstring \
php-mysql \
php-pgsql \
php-xml \
php-zip \
unzip


# Download and install Drupal
RUN wget https://ftp.drupal.org/files/projects/drupal-10.2.tar.gz && \
tar -xvf drupal-10.2.tar.gz && \
mv drupal-10.2 .


# Expose the Drupal port
EXPOSE 80

# Start Apache in the foreground
CMD ["apache2ctl", "-D", "FOREGROUND"]

How to build the image

To build your new Docker image with the Dockerfile you’ve created, enter the following docker build command.

sudo docker build -t my-docker-image .

The command above will build your Docker image.

How to deploy the image

To deploy your new image in a container, you need to use the docker run command. For example, to run the newly built Drupal 10 Ubuntu image on port 80, do the following.

sudo docker run -d --name my-drupal-container -p 80:80 my-docker-image

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.