- Create Dockerfile Ubuntu Operating System
- Create Dockerfile Ubuntu Server
- Create Dockerfile Ubuntu File
- Dockerfile Create User Ubuntu
- Dockerfile Create Ubuntu Container
- Create Dockerfile Ubuntu Server
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.
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.
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.
- 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.
- May 26, 2020 The default php-apache dockerfile does not include some php extensions, like mysqli or pdo. To install them we have to build our own dockerfile, based on it. To do that, we create a directory inside of our project named php-apache (this will be our build context) and inside of it, our dockerfile. Paste and save the code below as php-apache.
- Create a Dockerfile The most common scenario when creating Docker images is to pull an existing image from a registry (usually from Docker Hub) and specify the changes you want to make on the base image. The most commonly used base image when creating Docker images is Alpine because it is small and optimized to be run in RAM.
Dockerfiles are text files that tell the Docker tool how to build the image. The files could contain the commands to install and configure Nginx for example. Setting up the Dockerfile. We first specify what we want to base our image off of, which the first line From ubuntu, specifies. This tells the Docker system to use the base.
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.
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.
List images:
Start a container from an image:
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.
Need Hosting? Try ours, it's fast, reliable and feature loaded with support you can depend on.
Docker allows you to build containers using a Dockerfile. Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to build an image.
A Dockerfile consists of various commands and arguments listed successively to automatically perform actions on a base image in order to create a new one. It helps us to avoid issuing the command everytime while running container.
You can build the Docker image using one of the following two options:
- Interactively launch a BASH shell under the Ubuntu Base image, install Apache and its dependencies, and then save the image.
- Build the Docker image using Dockerfile with the web site included.
In this tutorial, I will explain how to create a Dockerfile, install the required Apache packages, add the necessary content and then build image.
Requirements
Ubuntu Server 14.04 with Docker installed on your system.
Creating a Dockerfile
A Dockerfile is a text file that has a series of instructions to build an image. It supports a set of commands that we need to use in our Dockerfile.
There are several commands supported like FROM, CMD, ENTRYPOINT, VOLUME, ENV and many more. Each and every instruction set in the Dockerfile adds an additional layer to the image and then performs a commit.
Create Dockerfile Ubuntu Operating System
Here, we will create a Dockerfile to create an image to install the Apache Web Server container.
To do this, we will need to create a file named Dockerfile using any text editor:
sudo nano Dockerfile
Add the following content which includes the commands and arguments for the Apache Web Server Container.
Now, save and close the file.
In the above Dockerfile, the first parameter FROM
tells Docker what the source of our image is, in this example we're using Ubuntu image.
A RUN
parameter executes a series of commands inside the image to install package. Here we are updating the Ubuntu repository and installing Apache and other dependencies.
The EXPOSE
parameter set's the default Apache port 80
so that the website will be available normally. Then, the ENTRYPOINT
is set to /usr/sbin/apache2ctl so that the Apache Server will execute.
Building an Image using Dockerfile
Create Dockerfile Ubuntu Server
Now, after we finish creating our Dockerfile for the Apache container, we are ready to create our first Apache Web Server images with docker.
We'll need to run the following command in our current working base directory to build an image.
sudo docker build -t ubuntu:Apache_Server .
You should see the following output:
Note:
-> The -t
parameter used to tag the Docker image. The .
parameter is used to specify the location of the Dockerfile that we created.
After building Apache_Server image, run docker images command:
sudo docker images | grep -i Apache_Server
You should see the Apache_Server image listed in the output as shown below:
Creating a Docker Container
Using the image we have built, we will now proceed to create a container running an Apache instance inside, using a name of our choice. Here we will use Apache_Instance.
Create Dockerfile Ubuntu File
Run following command to create a container:
sudo docker run --name Apache_Instance -p 80:80 -d ubuntu:Apache_Server
Dockerfile Create User Ubuntu
Finally, we have created our Apache Container and it is forwarded to port 80
. Now to check if its running properly or not we can run docker ps command.
sudo docker ps
Dockerfile Create Ubuntu Container
You should see the following output:
Create Dockerfile Ubuntu Server
After setting everything up, you can verify the Apache Web Server by typing the url http://your.container.ipaddress:80
or http://localhost:80
in your web browser.
Enjoy......
Need Hosting? Try ours, it's fast, reliable and feature loaded with support you can depend on.