Build and Ship App Image

Learn how to build a Docker image of app and how to ship it


In the previous section, we had seen how to build, run and ship static website or application. It had only one container.

In this section, we will running application which contain Database and we need at least two container to properly run the application.

Building Application Image

  • Get the application source code.
    • Download zip or tar file from here and unzip in your machine
    • Or you can clone the repo as git clone https://github.com/gorkhadev/docker-nginx-app
  • Enter into the docker-nginx-app directory
  • Check the Dockerfile:

    FROM nginx
    
    COPY ./brgrestaurant/ /usr/share/nginx/html
    
  • Build the application image by running following command:

    $ docker build -t static-app:v1 .
    
  • Run the application container as:

    $ docker run -d -p 8000:80 static-app:v1
    
  • Open URL http://localhost:8000 in your browser.

Shipping the Application Image

To ship the application image, we need registry where we can push our application image. For the demo purpose, we will be using Docker Hub.

NOTE: One can use private registry as well to store Docker images.

Prepare the application image

Currently, our application image repository name is static-app. We need to rename as per our Docker Hub username.

Run following command to give new tag to image:

$ docker login
# Enter your Docker Hub credentials

$ docker tag static-app:v1 mentorbrg/static-app:v1

NOTE: Use your Docker Hub username instead of mentorbrg.

Push the application image

Run following command to push the application image:

$ docker push mentorbrg/static-app:v1

Verify the application image

Visit Docker Hub and verify the application image.

Use application image

To use application image, we will be deleting the application image we created in earlier step. Then, Docker will pull the image from registry and run the container out of it.

# Delete previous app image
$ docker rmi -f mentorbrg/static-app:v1

# Run the container from app image
$ docker run -d -p 8000:80 mentorbrg/nginx-site:v1

Open URL http://localhost:8000 in your browser.

Help me to improve Gorkha Dev.