Typically, Docker containers are created by Docker images. Developers occasionally update the Dockerfile or source code. After that, they want to update or create new images through the container or save the container as an image.
This blog will demonstrate how to create and save a Docker container as an image.
How to Create a Container in Docker?
To containerize the application, first, create a container through the Docker image. For this purpose, go through the provided instructions.
Step 1: Create Base Container
First, create the Docker container through the “docker create” command. Here:
- “–name” is used to specify the container name.
- “-p” defines the port on which the container will execute
- “dockerimage” is a Docker image used to build a Docker container:
> docker create –name base-container -p 8080:8080 dockerimage
Step 2: List Docker Containers
To verify if the base container is created or not, list down all Docker containers using “docker ps -a” command:
Step 3: List Docker Images
Next, list down the Docker images using “docker images -a” command. Here, you can see that we only have the “dockerimage” Docker image:
Step 4: Start Container
Now, start the base Docker container using the “docker start” command:
Now, navigate the port on which you have executed the Docker container. In our scenario, we have visited “localhost:8080” on the browser:
How to Save a Docker Container as Docker Image?
In order to save the Docker container as a Docker image, users are required to commit the container. Upon doing so, the unnamed Docker image will be created from the Docker container. For the proper guideline, go through the given instructions.
Step 1: Update Program or Dockerfile
Let’s make minor changes in the Docker container’s application. For instance, we changed the content that is displayed on the specified port:
Step 2: Commit Base Container and Save Container as an Image
Next, commit the changes and base container using the “docker commit <container-name>” command. This command will also create an unnamed Docker image as a copy of the container:
Step 3: List Docker Images
To verify if the container is saved as a Docker image, list down the Docker images:
It can be noticed that the unnamed “<none>” image is saved as a container. Users can name the Docker image using image id:
Step 4: Name the Image Created by Base Container
To name the Docker image, utilize the “docker tag <image-id> <image-name>” command:
List down the images and check if the name is changed or not:
It can be noticed that we have successfully saved the container as “container-img” Docker image.
Conclusion
To save the Docker containers as images, first, create a simple base container that will be saved as a Docker image. Make changes in containerized application and commit the changes and base container through the “docker commit” command. This command will also save the container copy as a Docker image. Users can use the “docker tag” command to name the image. This blog has demonstrated how to save a container as an image.