Important docker command

Important tag for Docker
--detach , -d        Run container in background and print container ID
--name                Assign a name to the container
--publish , -p        Publish a container’s port(s) to the host
--volume , -v        Bind mount a volume
--restart            Restart policy to apply when a container exits
  1. Show the Docker version information
    docker version
  1. Display system-wide information
   docker info
  1. You can use the docker inspect command to find out low-level information about your container or image
   docker inspect [Container or Image name /ID]
  1. Pull an image or a repository from a registry
  docker pull ubuntu 
  1. pull a docker image with old version
  docker pull ubuntu:16.04
  1. List of all available images in local
  docker images 
  1. Launching a container with ‘docker run’ command
  docker run [imagename|ImageId]
  
  Create/run/start a docker container from image
  
  docker run -d --name <container_Name> <image_name>:<image_version/tag>
  d - run your container in back ground (detached)
  
  Expose your application to host server
  
  docker run -d  -p <host_port>:<container_port> --name <container_Name> <image_name>:<Image_version/tag>
  Example : docker run -d --name httpd_server -p 8080:80 httpd:2.2
  
  1. run a OS based container which interactive mode (nothing but login to container after it is up and running)
   docker run -i -t --name <container_Name> <image_name>:<Image_version/tag>
   i - interactive
   t - Terminal

   Example: docker run -i -t --name ubuntu_server ubuntu:latest
  1. List out running containers
   docker ps
  1. List out all docker container (running, stpooed, terminated, etc...)
   docker ps -a
  1. Stop a container
   docker stop <container_id>
  1. start a container which is in stopped or exit state
   docker start <container_id>
  1. Remove a container
   docker rm <container_id>
  1. Kill a container by sending a SIGKILL to a running container
  docker kill [CONTAINER]
  1. Remove the image
  docker rmi [IMAGE]
  1. Docker Exec Bash and Docker SSH into Container You'll often need to interact with a container's shell to create a service or solve problems. You can use the docker exec command to create an interactive shell. Let's start a container from the ubuntu image with a bash shell:
   docker run --name my_ubuntu -it ubuntu:latest bash
   
   Suppose you want to Docker ssh into container 'my_ubuntu'. You can use the docker exec   bash method:
   docker exec -it my_ubuntu bash
   
   Use docker exec   to issue commands into your container. For example, you can run the ls  command on your 'my_ubuntu'     docker container directly from the command prompt:
   docker exec -it my_ubuntu ls
   
.......................................................................................

Docker File

  1. The Dockerfile is a text file that (mostly) contains the instructions that you would execute on the command line to create an image.

  2. You’ll use a Dockerfile to create your own custom Docker image.

  3. Docker will build a Docker image automatically by reading these instructions from the Dockerfile.

Creating the Custom Docker image below is the process

  1. you create the Dockerfile and define the steps that build up your images

  2. you issue the docker build command which will build a Docker image from your Dockerfile

  3. now you can use this image to start containers with the docker run command

Docker provides a set of standard instructions to be used in the Dockerfile, like FROM, COPY, RUN, ENV, EXPOSE, CMD just to name a few basic ones.

Example 1 .

Step 1. create the DockerFile

vi Dockerfile

Step 2. Paste below instruction paste into Dockerfile

FROM ubuntu  
RUN mkdir TEST
RUN apt-get update
RUN apt-get install tree

Step 3. docker build -t

  docker build -t ubuntu_server . 

. is represent that Dockerfile is present in same Directory .

Step 4. To create the container from Docker Image. docker run -d --name <image_name>:<Image_version/tag>

  docker run -d --name ubuntu_container  ubuntu_server
Example 2 .

Create custom image for Nginx server .

Step 1. Create Index.html file

 vi index.html

Step 2. Put Some content into the index.html

<html>
<body>
    <h1> This is sample example of docker ngnix </h1>
</body>
</html>

Step 3: Create the DockerFile in same Directory

vi Dockerfile

Step 3: Paste below content into the Dockerfile and save (press esc btn then wq! end enter to save the file).

FROM nginx:latest
COPY index.html /usr/share/nginx/html/index.html   // Copy the file from source location to Destination
RUN chmod +r /usr/share/nginx/html/index.html      // Providing the permission to index.html 
CMD ["nginx","-g","daemon off;"]                   //  Nginx uses the daemon off directive to run in the foreground.