How to create an image from the container

Previous Docker tutorial we have learned how to install docker service and build the container. I recommend you to read my first article about docker before continuing with this article

Part1: Docker installation and configuration

Part2: How to create an image from the container

Part3: Dockerfile: To automate Docker image creation

In this article, we will discuss how to create a new image from the container.  My first post we have created a container named “lampstack” from the image Ubuntu.  Here we will create a new image from the container “lampstack” so we can use it for future.

The Docker flow:

Our lampstack container installed with the packages Apache2, MySQL and PHP, so converting this container to an image will help in future.

Steps to convert container to image

Check the status of the docker by issuing the following command

#docker ps -a

Here we can see our “lampstack” container is running. We will convert this container to an image for this, stop this container by issuing the following command

#docker stop container

and then run docker commit command as follows.

ubuntu:lampstack : Image name as Ubuntu and tag name is lampstack

cf377947975e : Container ID

Now we have created a new image, we can list it by running command “docker images”

From the above screenshot, we can see our new image ubuntu with tag “lampstack” is created 7 minutes ago.

To test the working of new image we will run a new container from it

Here we created a new container named “newlampstack” from the image ubuntu:lampstack.

To use our new image in future or if we want to deploy it in any other machine, first we have to publish it on a registry. There are many different registries you can use. Here I am using Docker hub registry.

Docker Hub Registry

Docker hub is public image registry service in which you can publish your images, test them and you can deploy it to any host. To publish in docker hub you should create an account on Dockerhub.  Once you registered in docker hub then you can proceed with publishing your image.

First, log in to the registry by issuing the command

#docker login

Provide the same credentials that you used for logging into Docker Hub.

and then tag your image name same as newly created repo name (create it at the docker hub)

#docker tag 1452cfe9e4ff rinshadr/ubuntu:lampstack

1452cfe9e4ff : Image ID

rinshadr/ubuntu : my repo name created in docker hub

Then push the image with docker push command. The format should be repo name/image name.

# docker push rinshadr/ubuntu:lampstack

We have created our own image and pushed it to the DockerHub public registry.

Leave a Reply

Your email address will not be published. Required fields are marked *