Friday, January 27, 2023

Create Your Docker Image

Create Your Docker Image

The article is described in the following scenarios:

  1. Get the Nginx Image from Docker Registry (Container Registry)
  2. Execute the Container and add/change files
  3. Return to the background process and check the status
  4. Build the new Image, and
  5. Finally, upload to the Docker Registry

Use Nginx as a Web Server

  1. Get nginx image from Docker Hub

    docker pull nginx
    
  2. Run nginx as a container with the name webserver

    docker run -d -p 8080:80 --name webserver nginx

    Options Description
    -d --detch, Run container in background
    -p Publish all exposed ports to the port,
    format host-port:container-port
    --name Assign a name to the container
    nginx Docker image or ID

    After execution, enter localhost:8080 in your host browser and see the “Welcome to nginx!” web page.

  3. Stop, start and restart the container

    docker stop webserver
    docker start webserver
    docker restart webserver
    
  4. Make sure that the container is in the execution, then type the following command to enter the shell environment

    docker exec -it webserver sh
    
    Options Description
    -i --interactive, Keep STDIN open even if not attached
    -t --tty, Allocate a pseudo-TTY

    The default home page of Nginx is located at /usr/share/nginx/html/index.html; you can use the following command to confirm it.

    # cat /usr/share/nginx/html/index.html

    Enter exit to leave the terminal and return to PowerShell.

  5. Copy the file to replace the default web page with a simple Hello World.

    <!DOCTYPE  html>
    <html>
    <head>
    	<title>Hello World Sample</title>
    </head>
    <body>
    	Hello World!
    </body>
    </html>
    

    Overwrite the container’s homepage with the cp command.

    docker cp ./index.html \
      webserver:/usr/share/nginx/html
    

    Enter localhost:8080 in the browser, and you can see that the homepage has been updated.

  6. Create a new image
    Use docker commit to save the container as a new image.

    docker commit webserver \
      <docker_hub_account> \
      /hello_world_nginx:1.0
    
  7. Stop the container and remove it.

    docker stop webserver
    docker rm webserver
    
  8. Run the newly docker image, and test in the browser.

    docker run -d -p 8080:80 \
      --name webserver \
      <docker_hub_account>\
      /hello_world_nginx:1.0
    
  9. Login to docker hub and upload the new docker image.

    docker login
    
    docker push \
      <docker_hub_account>\
      /hello_world_nginx:1.0
    

    After completion, you can see the new docker image on the docker hub web page.


Reference:
How to Create a Docker Image From a Container


No comments:

Post a Comment

Deploying Vue & .NET with Google OAuth on GCP Cloud Run

Deploying Vue & .NET with Google OAuth on GCP Cloud Run Deploying Vue & .NET with Google OAuth on GCP Cloud Run...