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:
Like this you can create your docker images based on the requirements and use it for running your applications.