Guide to Port Mapping, Binding, and Docker Container Image Creation

Port Mapping/Binding

  • Port Mapping / Port Binding is used to map the host port with the container port, to expose the container application to the internet.

  • docker run -it -p <host_port>:<container_port>

    • e.g. docker run -it -p 8089:8080 tomcat

  • Navigate to internet and access the container through public ip along with required port.

Docker Images

How to create Docker images and push to Container Registry

Docker Commit

  • Create a container image based on the existing container reference

  • docker commit <container_id> <Container_Repository_Name>/<New_Container_Image_Name>:<Tag>

Creation of Container image using Docker Commit:

  • Let’s first login to any previous container

  • Install some build tools like maven and git

  • Exit the container and stop the container

  • Let’s use this command : docker commit <container_id> <Container_Repository_Name>/<New_Container_Image_Name>:<Tag>

    • docker commit 7316260b08f0 jayk2810/build_tool_img:v2

  • Now we can run the container using this image and check build tool are already installed

Push the Images to Container Registry

  • Login to Container Registry using Docker CLI

    • Create Access Token in DockerHub

    • docker login -u <username>

      • Enter username and password
  • Docker Push Command to Push the Image to Container Registry

    • docker push<Container_Repository_Name>/<New_Container_Image_Name>:<Tag>

  • Check in Container registry that container image is pushed

Docker Build

  • To create the new Container image on the Dockerfile reference

  • Dockerfile composed of instructions to create docker container images.

  • Application developer create the Dockerfile and update in the source code repository.

  • Command : docker build -t <Container_Repository_Name>/<New_Container_Image_Name>:<Tag> . , “.” Denotes the Dockerfile Reference

    • First create the Dockerfile

      • “From” references base image

      • Now run the build command

Write Dockerfile :::

Docker File Instructions :::

  • FROM - To Identify the Base Image

  • RUN - To run the package manager

  • COPY - To Copy the file from host volume to container volume

  • CP - To Copy the file within the container volumes

  • ADD - To Copy the file from Host Volume as well as from URL

  • ENV - To define the Environment Variable

  • ARG - To pass Arguements to the Steps in Dockerfile

  • EXPOSE - To Define the Container Port

  • WORKDIR - To set the current working directory within the Container

  • CMD - To set the default start-up command to the container This Command can be changed at run-time.

  • ENTRYPOINT - To set the default start-up command to the container This Command cannot be changed at run-time.

docker history <Image_Name>

  • Used to find the Image Layers from Local Machine