Open In App

How To Create a Docker Container from an Existing Image?

Last Updated : 10 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Docker is an open-source software, that is used to contanerize our applications. Containerizing applications makes deployment a lot easier. For containerizing 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.

What is 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 libraries to build and run a container instance.
  • A docker image is created by using a file called Dockerfile. The Dockerfile contains instructions that are required to create a docker image.

What are Docker Containers?

  • Docker containers are runtime instances of docker images. They are very lightweight, and share the kernel of the 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.

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.

What is Docker Container Create?

Docker Container Create is a command in the docker that helps in creating new container instances with the specified Docker images. It initialize the container by setting up all the essential environments for this quickly and effectively but it not start the docker container. It facilitates the developers to isolate the applications and their dependencies from the other containerized applications.

Description

Docker container create facilitates with instantly preparing a new container for the specified image. It setup the environment required to launch the container. It configure the container in advance by allocating the namespaces, hostname, filesystem, networks, volumes, variables and other that defined in the Dockerfile or command-line options.

Docker Container Create Options

The following are the options that are available with Docker Container Create:

Options

Default

Description

–add-host

N/A

It provides the custom host-to-IP mapping as ( host:IP )

-a, –attach

N/A

It attaches the STDIN, STDOUT or STDERR buffers to the container

-e, –env

N/A

It facilitates with setting the environment variables to the container

-h, –hostname

N/A

It helps in setting the container hostname

-m, –memory

N/A

It facilitates with setting the memory limit to the container

Examples of Docker Container Create

The following are the examples of Docker Container Create with different options:

1. Adding –add-host option

  • This command creates the container with mapping the custom hostname to the host IP. The commands as follows:
docker container create --add-host myhost:192.168.1.100 my_image

2. Adding -a, –attach option

  • On usage -a or –attach option with docker container create facilities with creating the container having attachments to its STDIN, STDOUT or STDERR:
docker container create -a my_container

3. On Adding -e, –env

  • On usage of -e or –env with docker container create facilitates on setting the environmental variables for the container which is necessary while using Database Images.
docker container create -e MYSQL_ROOT_PASSWORD=password my_image

4. On Adding -m, –memory

  • On usage of docker container create with options -m, –memory, we are creating the containers with a memory limit of 512 MB based on the docker image. The commands looks as follows:
docker container create -m 512m my_image

How to Build A Docker Container Image? A Step-By-Step Guide

The following are the steps for building the Docker Container Image:

Step 1: Setup Docker Service

  • Firstly ensure that docker software is installed and it is in running. To know about the Installation of Docker refer this – Article.

Step 2: Explore Docker Containers

  • Before to the building of docker container images, make yourself exposed the various docker image that available in Dockerhub and other container registries and view their Container Image’s Dockerfile. So that you will be familiarized how to write the instructions in the Dockerfile and create customized Images.

Step 3: Create a Dockerfile

  • Create a text file named Dockerfile in your project Directory.
  • Define the Instructions in the Dockerfile such as defining the Base Image, Setting Variables, Run the Softwares and other required customization. Here we are providing the sample Dockerfile that you are going to build an image on it.
 FROM ubuntu:latest
RUN apt-get update && apt-get install -y nginx
CMD ["nginx", "-g", "daemon off;"]

Step 4: Build Docker Image

  • On using the `docker build` command try on building the docker image to that defined Dockerfile with the following command:
docker build -t my_nginx_image:latest . 

Step 5: Test Docker Container Image

  • To verify whether the Docker image is build successfully or not by listing the docker container images. Use the following command to list the images:
docker images

Step 6: Login in to Dockerhub Registry

  • Now, to push the docker image into the dockerhub firstly login into your dockerhub account with the credentials. The following command helps in login into the Dockerhub:
docker login

Step 7: Tag the Docker Container Image

  • After successfully login into the Dockerhub account, now change the tag of the Created Docker Image in the local registry with adding this docker registry. The commands looks as follows:
docker tag my_username/my_nginx_image:latest  my_nginx_image:latest 

Step 8: Push to Dockerhub

  • Now, push this docker image into the dockerhub with the following command:
 docker push my_username/my_nginx_image:latest

How to Create Docker Containers from Docker Images? A Step-By-Step Guide

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

Listing Docker Images

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

creating container with containerid

Example of 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

Build, Name and Tag the Docker Container Images

The following are the commands we are going to discuss on building, Name and Tag the docker container Images:

Build Docker Container Image

  • The following command is used for building the docker image from a Dockerfile in the current directory:
docker build -t my_image_name .

Name and Tag the Docker Container Image

  • The following command is used for Naming and tag the Docker Container Image:
docker build -t my_image_name:tag .

Example

docker build -t my_web_app:v1

Verify the Docker Image

  • The following is the command used for verify the Docker Image:
docker images

Running And Viewing Docker Containers

  • The following are the commands used for running and viewing the Docker Containers:

1. Run a Docker Container

The following is the command used for running a docker container from an Image:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Example:

docker run -d -p 8080:80 my_web_app

2. View Running Containers

  • The following is the command used for view running Docker Containers:
docker ps

3. View All Docker Containers

  • The following is the command used for viewing all the Docker Containers:
docker ps -a

TroubleShooting of Docker Container Create

The following are the some of the Trouble Shooting Issues of the Docker Container Create:

1. Docker Container has created but not running

  • Insufficient Resources: Check whether the container requires more resources than the available.
  • Conflict Ports: Ensure that the container isn’t trying to use the containers that already using.
  • Incorrect command: Verify the command is correctly entered or not.

2. Docker Container Create file permission Denied

  • Insufficient Permissions: Make sure that the docker service has permission to access the files needed for container creation.
  • Docker Volume Permissions: Check if the volume or directory specified in the Dockerfile has correct permissions or not.
  • SELinux or AppArmor: If using SELinux or AppArmor to ensure that they are not blocking access to the files required by the container.

Docker Container Create – FAQs

What is Docker Container Create User?

Docker container create doesn’t directly specifies a user, it takes user inheritance from the base image or can be specified in the Dockerfile using the USER instruction.

What is Docker Container Create directory?

Docker Container Create doesn’t creates directory directly; directories can be created within the containers during runtime using commands or specified in the Dockerfile.

What is Docker Container Create From Image?

Docker Container Create creates a new container instance from a specified image. It initalizes the environment but not starts it until the docker start command is used.

What is Docker Container Create Name?

Docker Container Create allows the specifying the name for the container using the “–name” option. It provides a user-friendly identifier to easier the managment.



Like Article
Suggest improvement
Next
Share your thoughts in the comments

Similar Reads