Creating the customized image with docker for reusability of the container environment.

Nerd Writer
Readers Hope
Published in
3 min readJan 15, 2023

One of useful feature in docker is customized images.

Photo by Rubaitul Azad on Unsplash

Whenever the docker container is deleted,

all the software that we have installed within the container will also be deleted.

If we can save the container as an image, then we can preserve the software.

This creation of customized docker images can be done in two ways.

1) using the docker commit command

2) using the docker file

This creation of customized docker images can be done in two ways.

1) using the docker commit command

2) using the docker file

Using docker commit:

1. Let's create a docker ubuntu container

with name c11

2.Run it in interactive mode

docker run - name c11 -it ubuntu

— name is used to assign the name for the container

run is used to start the container

-it is used to run the container in interactive terminal

Then In the container let’s set up the git environment by running below command

Update apt repository

apt-get update

update is used to update the apt repository

2. Install the git

apt-get install git

3. To check the git

git --version 
exit

git — version is used for checking the git is install or not.

To save the container as an image (snapshot )

docker commit c11 myubuntu

c11 is the container name

myubuntu is the customized image name

commit is used to snapshot the current environment of the container.

To see the list of images

you can see the image which you have created

docker image ls

Now let's run the image which we have created

docker run - name c22 -it myubuntu

git — version ( git is pre-installed)

Using docker file

This is a simple text file, which uses predefined keywords for creating customized docker images.

Keywords used in the docker file ( case sensitive )

1) FROM — used to specify the base image from which the docker file has to be created.

2) MAINTAINER — This represents the name of the organization or the

author who created this docker file.

3) CMD — This is used to specify the initial command that should be executed when the container starts.

4) ENTRYPOINT — used to specify the default process that should be executed when the container starts.

It can also be used for accepting arguments from the CMD instruction.

5) RUN — Used for running Linux commands within the container. It is generally helpful for installing the software in the container.

6) USER — used to specify the default user who should log in into the container.

7) WORKDIR —

Used to specify the default working directory in the container

8) COPY — Copying the files from the host machine to the container.

9) ADD — Used for copying files from host to container, it can also be used for downloading files from remote servers.

10) ENV — used for specifying the environment variables that should be passed to the container.

EXPOSE — Used to specify the internal port of the container

VOLUME — used to specify the default volume that should be attached to the container.

LABEL — used for giving labels to the container

STOP SIGNAL — Used to specify the key sequences that have to be passed in order to stop the container.

Let’s create a git environment in the ubuntu container by using docker file

1. create a docker file

touch dockerfile
vim dockerfile

2. Inside the dockerfile

FROM ubunut
RUN apt-get update
RUN apt-get install git

3. Run the docker file to create a customized image

docker build -t gitenv .

build is used to built the image

t is used assign the tag to the image

“ . ” is used to refer the current directory

4. Check for the images

docker image ls

You can find the gitenv new create customized image in images.

Customized images for very useful when you're testing the environment.

Thank you for reading.

--

--