Spin up and run applications in split-seconds –; That’;s what makes Docker containers so popular among developers and web hosting providers.
In our role as Technical Support Services for web hosting companies and infrastructure providers, we provision and manage Docker systems that best suit their purposes.
One of the key tasks involved in this Docker management services, is editing docker images. Today, we’;ll see how we edit docker image for customer needs.
What is a docker image
Docker helps to easily create and run container instances with our desired applications. These containers are created using images.
A docker image is a package of code, libraries, configuration files, etc. for an application. The images are stored in repositories (storage locations).
Images can be downloaded from a repository and executed to create docker containers. So, in effect, a container is just a run-time instance of a particular image.
To create a Docker image, a Dockerfile is used. A dockerfile is a text document, usually saved in YAML format. It contains the list of commands to be executed to create an image.
How to edit docker image
The images provided by repositories are specific to a single instance type creation. In many scenarios, users need to edit these images to suit their needs.
For customizing or tweaking a docker image to specific requirements, we edit this docker image. But Docker has a drawback that an image cannot be directly edited or modified.
To edit Docker images, there are two ways:
1. Edit the Dockerfile
The most commonly used method is to edit the Dockerfile that is used to create the Docker image. This is a convenient and fool-proof method to edit docker image.
A sample Dockerfile for a Zabbix monitoring container would look like:
zabbix-agent: container_name: zabbix-agent image: zabbix/zabbix-agent:latest net: "host" privileged: true restart: always environment: ZBX_SERVER_HOST: zabbix.mysite.com
To modify the image used by an existing container, we delete that container, edit the Docker file with the changes needed and recreate the container with the new file.
In this sample Dockerfile, the image used to create the container is ‘;zabbix/zabbix-agent:latest’;. This image can be modified to another one or version by editing this file.
The ‘;docker compose’; tool helps to manage multiple Docker containers easily using a single YAML Dockerfile. In cases where changes are to be made for only one container, it can be edited in the file.
After making changes to the image, only the corresponding containers need to be recreated and others can be left intact. The option “–;no-recreate”; is used for that.
docker-compose -f dockerfile.yml up -d --no-recreate
As Docker containers are meant to be restarted and recreated, they cannot be used to store persistent data. The data in Docker infrastructure are usually stored in Docker volumes.
Storing the data in volumes outside the containers ensures that even when the containers are recreated, the data related to it won’;t get affected.
This enables us to easily edit a docker image without losing the underlying data. Modifying Dockerfiles also helps to keep track of the changes made in the images.
Utmost care has to be exercised while modifying a Dockerfile, especially in the production environment, as a single mistake can mess up a normally functioning container.
2. Create a modified image
Another option to edit docker image is to run an existing image as a container, make the required modifications in it and then create a new image from the modified container.
a. First, create a Docker container from a parent image that is available in repository.
b. Then connect to the container via bash shell
docker exec -it container-name bash
c. From the shell, make the changes in the container as required. This can include installing new packages, modifying configuration files, downloading or removing files, compiling modules, and so on.
d. Once the changes are done, exit the container. Then commit the container as a new image. This will save the modified container state as a new image.
docker commit container-ID image-name
e. This modified image is then uploaded to a repository, which is then used for creating more such containers for future use.
Docker containers are widely used in DevOps and niche web hosting. Today we’ve discussed how our Dedicated Support Engineers easily edit docker images in our Docker infrastructure management.