Docker →part 3 (Docker Images,Docker file and port expose)

Aakib
5 min readApr 15, 2023

--

In previous Part we discuss the Architecture of docker and its basic commands to setup docker in linux

In this part we usually discuss about the the topics such as →

  1. What is Docker Image
  2. ways to create a image
  3. docker file
  4. docker port expose

What is Image in Docker →

In the simplest terms, an image in Docker is like a snapshot or a template of a software application and all its dependencies. It contains everything that is needed to run the application, such as code, libraries, and configurations.

Think of it as a recipe for baking a cake, where the recipe is the Dockerfile and the resulting cake is the Docker image. Once you have the image, you can create multiple identical containers (like baking multiple cakes using the same recipe) that will run the same application, each in its own isolated environment.

Images are lightweight and portable, which makes them easy to share and deploy across different environments, such as development, testing, and production.

Ways to create Image →

  1. Using a Dockerfile: A Dockerfile is a script that contains a set of instructions for building a Docker image. You can create a Dockerfile by specifying the base image, adding files and configurations, and running commands to set up the environment.
  2. Take Image from Docker hub →Docker hub is just like github where all the types of images are there and updated time to time you just need to run the command docker pull <image name > and it automatically pulls the image

3. Create Image from Existing Docker containers → You can modify the running container and convert it into image such as suppose you run the ubuntu container and you install a no. of software in that ubuntu image than you convert that container into images by command

docker commit [OPTIONS] CONTAINER IMAGE

Where:

  • OPTIONS: Specifies any additional options for the commit command, such as author or commit message.
  • CONTAINER: Specifies the ID or name of the running container that you want to convert into an image.
  • IMAGE: Specifies the name and optional tag for the new image.

Here’s an example command:

docker commit mycontainer myimage:v1

This will create a new Docker image called myimage with the tag v1, based on the current state of the mycontainer container.

Note that using docker commit to create images from running containers is generally not recommended as it can lead to issues with reproducibility and version control. It's better to create images using a Dockerfile to ensure a consistent build process.

What is Docker file →

A Dockerfile is a text file that contains a set of instructions for building a Docker image. Think of it as a recipe for creating a container image.

The Dockerfile specifies the base image to use, adds files and configurations, and runs commands to set up the environment. Each instruction in the Dockerfile creates a new layer on top of the previous one, allowing for efficient image building and caching.

Here’s an example of a simple Dockerfile:

# Specify the base image
FROM ubuntu:latest
# Install some packages
RUN apt-get update && \
apt-get install -y nginx
# Copy some files
COPY index.html /var/www/html/
# Expose port 80
EXPOSE 80
# Set a default command
CMD ["nginx", "-g", "daemon off;"]

This Dockerfile creates an image based on the latest version of Ubuntu, installs the Nginx web server, copies an HTML file, exposes port 80, and sets a default command to start Nginx.

Once you have a Dockerfile, you can use the docker build -t myimgcommand to build the image. The Dockerfile provides a simple and repeatable way to build images that can be shared and deployed across different environments.

What is Port Expose in Docker →

When you run a program inside a Docker container, it can communicate with other programs inside the container or with other containers, but it’s isolated from the outside world. To allow the program to communicate with the outside world, you need to tell Docker which network ports the program is using.

The EXPOSE instruction in the Dockerfile is like a note that tells Docker which network ports the program inside the container is going to use. It's like saying "Hey Docker, the program is going to use this network port to talk to the outside world."

However, just because you’ve told Docker which network port the program is using, it doesn’t mean that the program can actually communicate with the outside world. To make the program accessible from outside the container, you need to publish the network port using the -p option when you run the container with the docker run command.

For example, let’s say the program inside the container is using network port 80. You can publish this network port to the outside world by running:

docker run -p 8080:80 myimage

This command will tell Docker to map the container’s network port 80 to the host machine’s network port 8080, so you can access the program by visiting http://localhost:8080 in your web browser.

I hope explanation in this blog is easier to understand!

In next part we usually see about the what is docker volume

--

--

Aakib

Cloud computing and DevOps Engineer and to be as a fresher I am learning and gaining experiance by doing some hands on projects on DevOps and in AWS OR GCP