Open In App

How To Create a Docker Container from an Existing Image?

Last Updated : 11 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Docker is an open-source software, that is used to contanerize our applications. Contanerizing applications makes deployment a lot easier. For contanerizing applications, docker uses Docker images, which act like templates for making containers. Today we will learn how to create a container from an existing docker image, but before that, let’s take a quick look at What are Docker images and containers.

Docker Image

  • A docker image is a static file that is used to make a docker container. It acts like a template, that contains set of instructions, application code, dependencies and libraires to build and run a container instance.
  • A docker image is created by using a file called Dockerfile. Dockerfile contains instructions that are required to create a docker image. You can learn more about Dockerfile from here – Dockerfile

This was just an overview of what is a docker image. To learn more about it in depth, checkout this article – Docker images

Docker Containers

  • Docker containers are runtime instances of docker images. They are very lightweight, share the kernel of host OS. These are the places where the actual application code is being executed and all the dependencies, libraries, environment variables required for the application are present inside them.
  • Containers are created by running a docker image. Docker image acts as a executable, on executing which, we get docker containers. To learn more about containers, checkout this article – Docker containers

Today we are going to learn about the 2nd point of these 2 points, that is how to create docker containers from docker images. Basically, this is achieved by using the docker run command. Given below are the steps to do this.

Creating Docker containers from Docker images

To create docker containers from a docker image, first we must have a docker image. We can get our required docker image either from Dockerhub, or can create our custom docker image by using a Dockerfile. After we have our required docker image, follow the following steps:

Step 1. List all the docker images, that are available locally. Enter the following command to do this:

Command:

 docker images

Output:

image_list_gfg.png

List of all the docker images locally present

Step 2. Copy the image ID of the target image, that we want to contanerize. Image ID is a unique ID of any docker image. Let’s say we want to create a container from ubuntu image with latest tag. We will copy the image ID of ubuntu image present at 2nd position, which is – 3b418d7b466a.

Step 3. The third and the last step is to start a container for our target image. The docker run command is used to do this. Below is the syntax of command:

Command:

docker run <options> <image_ID>

The <options> for the run command are explained in docker’s documentation, you can check it out from here – https://docs.docker.com/engine/reference/run

Command:

docker run -it 3b418d7b466a

Output:

interactive_docker_gfg.jpg

An interactive session of ubuntu docker image

Example – Running a Nodejs web app docker image

In this example, we will see a NodeJS docker image getting contanerized by the docker run command. Here are the steps to do it:

Step 1. List all the docker images, that are available locally. Enter the following command to do this:

docker images

Output:

docker_ls_gfg.png

Step 2. In this example, we wann run the first docker image, which is a NodeJS application. It’s the kartikkala/mirror_website. So we will copy it’s image ID and run it with the necessary volume mounted and port 8080 mapped with the host PC. We are mapping port 8080 as it’s programmed in the NodeJS app to listen at port 8080. To know which port you need to map to the host PC, you can refer to this article – Managing Ports

Command:

docker run -p 8080:8080 -d --mount type=bind,src=$(pwd)/../volume1,dst=/downloadables kartikkala/mirror_website:beta

So you can see, there are a bunch of things going on here:

  • First thing, port 8080 of the container is exposed to port 8080 of the host machine with the ‘ -p ‘ flag.
  • Second thing, volume1 directory is bind mounted as a volume on /downloadables folder, where volume1 folder is of the host machine, and /downloadables is inside the container. This will cause all the changes inside the /downloadables folder to be reflected in the the volume1 folder directly.

Step 3. Now we will open up our browser and check on localhost or any other IP address assosicated with out local machine. Below is the screenshot for our web app running in the browser:

Output:

Selection_004-(1).jpg

Our web app is up and running on port 8080


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads