Make your own Dockerfile to run an Ubuntu Container

Make your own Dockerfile to run an Ubuntu Container

Overview

This short article will show to make a simple Dockerfile from scratch to run an Ubuntu Container without relying on downloading the official Ubuntu image to make a container. By following this process demonstrated in this article, any Dockerfile can be created to make a different image to run a container that can support another operating system or software program.

Problem

A common suggestion to run any container is to download its official image, for ubuntu, the image can be pulled with the following command:

docker pull ubuntu

Confirm to see if the image Ubuntu exists on the machine with the following command:

 docker images

A container must be made in the same directory where the Docker image is to build it:

docker build -t ubuntu .

A problem shown below can occur when building a container:

As stated before, there is always a directory where the contained image is saved onto the computer and is necessary to change into that same directory, to execute Docker commands to run the image and create a container. The error message states "failed to solve with frontend dockerfile.v0: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount1830543670/Dockerfile: no such file or directory" it is trying to use the directory /var/lib/docker/tmp/buildkit-mount1830543670/Dockerfile to find and run the Dockerfile. When installing Docker, it has a default path where it saves images. By using "Docker info", shows a variable called "Docker Root Dir" that shows where files are saved:

Docker info

After using the "Docker info" command, a lot of information relating to how Docker Desktop is structured and where to find files is provided. Scroll down to see what "Docker Root Dir" says. By default, it should point to the "/var/lib/docker" directory.

While it is true, the default location for saved images is /var/lib/docker, the ubuntu image tried to be downloaded in the "/var/lib/docker/tmp/buildkit-mount1830543670/Dockerfile/" directory, which is further within the /var/lib/docker directory. Downloading Docker on any machine, may have a chance to encounter this error, and to avoid this error, the best practice is to create a Dockerfile from scratch and run it to make a container. This methodology can be applied to make any containers that run different operating systems or software.

Materials

  • Docker or Docker Desktop. Download Docker: https://www.docker.com/

  • Command Prompt or Terminal

  • Notepad, Visual Studio Code or a similar application

Procedure

  • Create a file called "Dockerfile". It has to be named "Dockerfile" and there is no extension to the file. Docker only locates and reads files in the specified directory that has the file named "Dockerfile".

  • Create a "New Text Document" and name it "Dockerfile". Once again, remove the extension. The Dockerfile in a directory should appear down below as is:

  • Within the Dockerfile there are three commands:

      FROM ubuntu:22.04
      RUN apt-get update -y
      RUN apt-get upgrade -y
      RUN apt-get install -y curl
    
  • FROM: Choose an image to download "FROM". In this case, the ubuntu image is being downloaded and the version can be specified.

  • RUN: After choosing an image, many commands can start with "RUN" to run commands such as downloading packages to run software more efficiently. Since a new ubuntu operating system is being created, it is always best to update and upgrade to receive the latest packages and install curl to download other packages, zip files, or other content from websites more easily.

  • Navigate to the directory that contains the Dockerfile

  • Run "dir" on Windows or "ls" on Mac or Linux to see if the "Dockerfile" file is in the directory that is currently shown in Command Prompt or the Terminal. To change directories, use the "cd" command to go to different folders or directories.

      dir
    

    An example of using the "dir" command:

  • After navigating to the same directory where the Dockerfile is located, build the image:

      docker build -t image_name .
    

The commands after "docker build" means:

  • -t: tag. Tag means to name the image.

  • image_name: After "-t" any name can be given to the image to identify it to be used in the future to build more containers by using the same image.

  • . : The single "." means the current directory. By navigating to the directory that contains the Dockerfile, a "." can be used to tell Docker to look and run for the Dockerfile that is in the same directory. A file path directory can be used instead of a single dot.

  • Build the image:

The image in this demonstration has been named "start_ubuntu"

  • After building, a similar result should appear:

  • Now use the "docker images" command to see if the image exists:

      docker images
    

  • By running the following command, a container can be created by using the recently created Ubuntu image:
docker run -it --name=ubuntu_container_name start_ubuntu /bin/bash

"docker run" runs an existing image. The keywords after "docker run" means:

  • -it: Executes and enters the Ubuntu container, where bash commands can be executed within the container's shell

  • ubuntu_container_name: The name to give to the newly created container. Not specifying a name will result in a random name being given to the container.

  • start_ubuntu - That is the name of the image created from our Dockerfile.

  • /bin/bash - A shebang. This is crucial since every script has to start with /bin/bash for the Ubuntu terminal to read bash scripts since Ubuntu uses bash.

Demonstration:

After running the command, the Ubuntu container is created:

Check Docker Desktop on Windows or type in a new Command Prompt or the Terminal window "docker container ps" to see the newly created container:

docker container ps

By closing Command Prompt, Terminal or typing "Exit", leaves the Docker container space. To enter the container, type the following command:

docker exec -it name_of_container /bin/bash