Create Dockerfile Ubuntu
I mentioned in my previous post that I’ll explain how to create your own Docker image and customize it however you’d like. While is great to just use an image from Docker Hub, it can be that you need some customized image to fit your needs. As said before, is not hard at all to create the image and worth knowing how to do it.
- Dockerfile Create Ubuntu Container
- Create Dockerfile Ubuntu Tutorial
- Create Dockerfile Ubuntu Operating System
I’ll use for this tutorial a fresh Ubuntu 18.04 minimal installation. You can follow the same steps (or almost) using different Linux distro, Microsoft Windows or MacOS. The reason why I chose Ubuntu is simply because is the distro that I’m most familiar and enjoy working with.
Create Dockerfileubuntucuda11800180 Loading branch information; sganeshb committed Feb 13, 2021. Verified This commit was created on GitHub.com and signed. The Dockerfile is a configuration file that automates the steps of creating a Docker image. It is similar to a Makefile. Docker reads instructions from the Dockerfile to automate the steps otherwise performed manually to create an image. To build an image, create a file called Dockerfile. If we want to create a base image, we use ‘FROM scratch’ in the Dockerfile. In the above Dockerfile, ‘ubuntu’ is used as a base image, which is called parent image. Other instruction mentioned in the Dockerfile is going to modify this Docker image. It is used to set the environment variable while creating a Docker image.
For all steps below you need to be root or run the commands via sudo. So you’ll see either # at the begining of the command if you’re root or $ sudo if you pick to run it with elevated rights.
Install Docker
A word of advice here. Be sure to have docker.io typed. If you miss the .io, the system will install a docker, but that’s a different package:
You’ll end up with something that cannot be used for what we want to achieve, since the docker command isn’t even there.
You can test if the installation completed successfully by using the following command:
You should see something like this in the output:
Since this is a new installation, you’ll have no images, no containers, nothing.
You can check, just to be sure.
The result should be:
I’ll add at the end of the post some basic (and most important) Docker commands to get you started.
Dockerfile Create Ubuntu Container
Pull Ubuntu 18.04 image – Optional step
This step is optional, but I’d advise to do it, just to test that everything is fine with your Docker installation In this case we’re going to use the official Ubuntu 18.04 minimal Docker image. If you want to read more about this image you can check the explanation on Ubuntu 18.04 minimal Docker image and check their repository on Docker Hub – Ubuntu.
If everything goes well you should see a message ending with “Status: Downloaded newer image for ubuntu:18.04” :
Time to run our first container:
You should be now in container shell:
Now that we tested you can type exit to leave the container.
Create Dockerfile
The Dockerfile is nothing more than a text document which contains all the commands a user could call on the command line to create an image.
A detailed explanation is beyond the scope of this post, but if you’d like to learn more, you can check the Docker Documentation – Dockerfile
Here is a sample that’s good to start with:
A short explanation:
#
– This is a comment, add here whatever you think is useful. I’ve picked the name “mycustomlinux01”, but you can add whatever you like.FROM
– is always your first instruction, because it names the base image you’re building your new image from.MAINTAINER
– is the creator of the Dockerfile.RUN
– instruction to run the specified command, in this case apt-get to install various packages
There are multiple instructions for setting environment variables like ADD, COPY, ENV, EXPOSE, LABEL, USER, WORKDIR, VOLUME, STOPSIGNAL, and ONBUILD. You can read all about them in the Docker Documentation – Dockerfile
Using RUN you can add whatever package you need in your custom image. The same like you would do on a regular Ubuntu installation.
Yes, all the packages above could have been added in one RUN line, but for the sake of better visibility I would suggest to have separate lines.
Create your custom Docker image
After you save the Dockerfile is time to create your image
You’ll see a lot of output, the same like when you’re installing new packages in any Linux distro. When you see the following lines, you’ll know that the image was successful created:
Let’s check if the image is listed using:
You should see the mycustomlinux01 image listed:

Since the image is created successful I’d suggest that you run a container using this image following the same steps like in the “Pull Ubuntu 18.04 image”
Basically that’s it, you just created your custom image.
As mentioned above, here is a list of commands that I find useful to have at hand when working with Docker containers.
Create Dockerfile Ubuntu Tutorial
List images:
Start a container from an image:
Create Dockerfile Ubuntu Operating System
Using an ID (you get the ID from List image command):
List all containers:
List running containers:
Attach running container:
Remove a container:
Last but not least. If you liked my Ubuntu 18.04 Docker image customized for network engineers who wants to learn Python and you would like to install additional packages, here is the Dockerfile:
Obviously there is more about Docker than is covered on this post. It wasn’t in my scope to make a detailed analyze of Docker, rather a cheatsheet on how to create your custom image. If you want to learn more there are plenty resources out there and a good starting point is the Docker website.
I hope you find this how-to useful. As always, if you need to add something or you have questions about, please use the Comments form to get in contact with me.
