Shashikant shah

Wednesday 16 September 2020

Docker image commands. Part-4

 Docker image command :-

  1. docker image list.

# docker image ls / docker images

  1. Show the history of an image. (what action to perform)

# docker image history <image_name>

  1. Docker pull image from docker hub.

# docker pull ubuntu:18.04

  1. Create image of running container.

# docker container commit --author "shashikant" -m "this is test commit" <container ID> <new image_name>

  1. Docker image push to docker hub.

# docker login

<username and password>

# docker push account_name/image_name

  1. Docker image remove from docker host.

# docker image rm <image_name>

# docker rmi <image_name>

  1. inspect image command for show details image.

# docker image inspect <image_name>

  1. create image backup and restore.

# docker image save <image_name> > logstash.tar

# docker image load < logstash.tar

  1. what is a dockerfile ?  
 A Docker File is a simple text file with instructions on how to build your images. A Docker image consists of layers. Each layer adds something to the final Docker image. Each layer is actually a separate Docker image.

         MAINTAINER :- The Dockerfile MAINTAINER command is simply used to tell who is                                                     maintaining this Dockerfile.

            # MAINTAINER   shashikant <shashi@gamil.com>

         FROM :- The Dockerfile FROM command specifies the base image of your Docker images.
            # FROM ubuntu:latest

         CMD :- The CMD command specifies the command line command to execute when a                                             Docker container is started up which is based on the Docker image built from this                                 Dockerfile. This example just prints the text “image created” when the Docker                                         container is started.

# cmd [“echo”,”Image created”]

COPY :- The COPY command can copy both a file or a directory from the Docker host to the Docker image. 

# COPY  /myapp/target/myapp.jar  /myapp/myapp.jar   (for file)

# COPY  /myapp/config/prod  /myapp/config       (for directory)

ADD :- The ADD instruction can copy and extract TAR files from the Docker host to the Docker image.

# ADD  myapp.tar  /myapp/

# ADD  http://jenkov.com/myapp.jar  /myapp/

ENV :- The Dockerfile ENV command can set an environment variable inside the Docker image.

# ENV  MY_VAR  123

RUN :- The Dockerfile RUN command can execute command line executables within the Docker image. The RUN command is executed during build time of the Docker image, so RUN commands are only executed once. The RUN command can be used to install applications within the Docker image.

# RUN apt-get install some-needed-app

 

ARG :- The Dockerfile ARG instruction lets you define an argument which can be passed to Docker when you build the Docker image from the Dockerfile.

Notice the --build-arg followed by the tcpPort=8080 . This part sets the tcpPort argument value to 8080.

# vim dockerfile

ARG tcpPort

ARG useTls

CMD start-my-server.sh -port ${tcpPort} -tls ${useTls}

# docker build --build-arg tcpPort=8080 --build-arg useTls=true .

WORKDIR :- The WORKDIR instruction specifies what the working directory should be inside the Docker image. The working directory will be in effect for all commands following the WORKDIR instruction.

# WORKDIR    /java/jdk/bin

EXPOSE :- The Dockerfile EXPOSE instruction opens up network ports in the Docker container to the outside world.

# EXPOSE   8080

# EXPOSE   8080/tcp 9999/udp

 

VOLUME :- The Dockerfile VOLUME instruction creates a directory inside the Docker image which you can later mount a volume (directory) to from the Docker host. In other words, you can create a directory inside the docker image, e.g. called /data which can later be mounted to a directory, e.g. called /container-data/container1 in the Docker host. The mounting is done when the container is started up. Here is an example of defining a volume (mountable directory) in a Dockerfile using the VOLUME instruction.

# VOLUME   /data

 

ENTRYPOINT :- The Dockerfile ENTRYPOINT instruction provides an entrypoint for Docker containers started from this Docker image. An entrypoint is an application or command that is executed when the Docker container is started up. In that way, ENTRYPOINT works similarly to CMD, with the difference being that using ENTRYPOINT the Docker container is shut down when the application executed by ENTRYPOINT finishes. Thus, ENTRYPOINT kind of makes your Docker image an executable command itself, which can be started up and which shut down automatically when finished.

# ENTRYPOINT java -cp /apps/myapp/myapp.jar com.jenkov.myapp.Main

# ENTRYPOINT [“/usr/sbin/nginx”,”-g”,”daemon off;”]

SHELL :- Allows the default shell used for the shell form of commands to be overridden.

Its provide shell environment for linux and window on Dockerfile

# SHELL ["/bin/bash", "-c", "source /usr/local/bin/virtualenvwrapper.sh"]

10. create image to dockerfile

FROM ubuntu:latest

RUN apt-get -y update && apt-get -y upgrade

RUN apt-get -y install openjdk-8-jdk wget

RUN mkdir /usr/local/tomcat

RUN wget https://downloads.apache.org/tomcat/tomcat-8/v8.5.57/bin/apache-tomcat-8.5.57.tar.gz -O /tmp/tomcat.tar.gz

RUN cd /tmp && tar xvfz tomcat.tar.gz

RUN cp -Rv /tmp/apache-tomcat-8.5.57/* /usr/local/tomcat/

EXPOSE 8080

CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]

# docker image build -t <new_imageName>:Tagname .

# docker image build -t ubuntu_new:1 .

OR

# docker image build -t <new_imageName>:Tagname  -f  path/dockerfile .





No comments:

Post a Comment