How to create a docker image?

12/02/2020

How to create a docker image?

Docker is a tool that is designed to benefit both developers and system administrators, making it a part of many DevOps (developers + operations) toolchains. For developers, it means that they can focus on writing code without worrying about the system that it will ultimately be running on.

What is Docker?

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.

In a way, Docker is a bit like a virtual machine. But unlike a virtual machine, rather than creating a whole virtual operating system, Docker allows applications to use the same Linux kernel as the system that they’re running on and only requires applications be shipped with things not already running on the host computer. This gives a significant performance boost and reduces the size of the application.

Docker is open source. This means that anyone can contribute to Docker and extend it to meet their own needs if they need additional features that aren’t available out of the box.

Creating your new image

We’re going to create a new image, based on the latest Ubuntu image, which will include a LAMP server. Although there are tons of such images already available, this will serve as an easy example you can follow.

The first thing we must do is pull the latest Ubuntu image with the command:

docker pull ubuntu

The above command will pull down the latest Ubuntu image. Now we’re going to create a container such that we can work within our latest Ubuntu. To do this, issue the command:

docker run --name my-lamp-server -it ubuntu:latest bash

When the above command completes, you’ll notice your terminal has changed to indicate you are now working within the container.

Once inside the container, the first thing you must do is update apt with the command:

apt-get update

If you do not issue the above command, you won’t be able to install anything into the container. Once the update completes, you can install the required commands in the server.  You can use the following commands to install lamp with basic modules.

apt-get install apache2
apt-get install mysql-server
apt-get install php libapache2-mod-php
/etc/init.d/apache2 restart
service apache2 status

When the installation is complete, you need to exit the container with the exit command. Issue the command docker ps -a and you should see the new container listed.

We only created a very basic image here, but you can use all of your developer skills to create many varied images that will be useful to you, your colleagues, and the community at large.

When you create a Docker container, its hostname is automatically generated. For example, when I create a new Ubuntu container the hostname is 69ff24d6e252. This is the name that Docker has given to your container.

Install what you want on it, and make sure everything works. Then exit your Docker container:

exit

We now need to commit; otherwise, all of your changes will be lost. Commit the changes to a new Docker image instance using the following command. The -m switch is for the commit message that helps you and others know what changes you made, while -a is used to specify the author. The container ID will get from the command docker ps -a. Unless you created additional repositories on Docker Hub, the repository is usually your Docker Hub username:

docker commit -m "What did you do to the image" -a "Author Name" <container-id> <repository> <new_image_name>

For Example:

docker commit -m "LAMP Server" -a "Suhesh K S" 69ff24d6e252 suhesh/ubundu-lamp

Note: When you commit an image, the new image is saved locally, that is, on your computer. Later in this tutorial, you’ll learn how to push an image to a Docker registry like Docker Hub so that it may be assessed and used by you and others.

After that operation has completed, listing the Docker images now on your computer should show the new image, as well as the old one that it was derived from:

docker images

Following is the result:

docker

Like this you can create your docker images based on the requirements and use it for running your applications.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Install Minio on Ubuntu 18.04 LTS

Minio is a self-hosted solution for creating your own Object storage. It is an alternative for AWS S3, if you have used...
29/12/2020

LVM Ubuntu Tutorial

LVM or Logical Volume Manager makes management of disk space on a Linux machine easier to manage.  Features of LVM include: Create,...
28/12/2020

5 Best Screen Recorders for Ubuntu

Often times, we find ourselves in a position where we have to record our desktop screen such as broadcasting your live...
29/12/2020