Create and Run a Docker Image

Jatin Aneja
5 min readMar 16, 2021

Step-by-step guide
If you do not have Docker Toolbox installed follow instructions on the Docker Toolbox download page (at https://www.docker.com/toolbox) to install and setup the Docker tools.

Java Application Environment:

a) Spring Based Web Application

b) Maven to build the project

c) Tomcat as Application Server

d) MySQL as Database

1. Open Project Directory -

In docker terminal, go to the root of your repository.

2. Create a Dockerfile -

Type command “touch Dockerfile” as shown below:

3. Open Dockerfile -

To add docker commands which will execute following tasks:

· Take JDK 8 and Tomcat 8 base image

· Copy tomcat conf file into docker — The file “tomcat-users.xml” is present in MyProj root folder.

· Install Maven

· Install MySQL Server

· Create a working dir in Linux VM.

· Copy source code into working directory — The “src” folder is present in MyProj root folder.

· Run Maven commands to build the code — The file “pom.xml” is present in MyProj root folder.

· Move generated WAR file to Tomcat webapps folder

· Move database schema file to working directory — The file “projddl.sql” is present in MyProj root folder.

· Run MySQL commands to create a database and import the schema file

To open Dockerfile for edit, type command “notepad Dockerfile&” as shown below:

Save the Dockerfile. Then press Enter in docker terminal to go to next prompt.

4. Build an image

To create an image of application, type command “docker build -t my-proj” as shown below: Where “my-proj” is the name of image that we would like to be created.

This uses the file ./Dockerfile in the root of the repo to make a Docker Image. Once build is successful following screen is displayed.

5. View Image -

When complete check the docker image has been created, within the command shell execute:

$ docker images

6. Run the docker image -

Within the command shell execute:

“ docker run -d -p 8080:8080 — name backend my-proj”

Here -p 8080:8080 exposes the docker image’s http internal port 8080 as port 8080 on the virtual machine the docker image is contained (and running) within.

7. Verify the Tomcat is running. -

Enter url: http://192.168.99.100:8080 . This will open up the Tomcat Admin console page

8. Verify the Application is running -

Enter url: http://192.168.99.100:8080/MyProj. This will open up the default landing page in your application

Front-end Application Docker

Frontend Application Environment:

a) ReactJS based frontend Application

b) NPM to build the dependency

c) Gulp to package the application

d) Nginx as Web Server

1. Open Project root folder -

Go to the root of repository where frontend code is:

2. Create a Dockerfile.

3. Open Dockerfile -

To add docker command which will execute following tasks:

· Take Node as the base image

· Create a working dir in linux vm

· Copy current directory source code into working directory

· Run NPM command to download dependencies

· Install Gulp

· Install Nginx

· Run Gulp command to build the project

· Copy dist folder inside Nginx default html folder

· Copy Nginx conf into Nginx directory inside linux vm

4. Build Image -

To create an image of application, execute following command:

"docker build -t my-frontcode ."

This uses the file. /Dockerfile in the root of the repo to make a Docker Image.

5. Verify Image -

When complete check the docker image has been created, within the command shell execute:

$ “docker images”

6. Run the docker image -

Within the command shell execute:

"docker run -p 80:80 --name frontend --link backend:backend my-frontcode"

Here -p 80:80 exposes the docker image’s http internal port 80 as port 80 on the virtual machine the docker image is contained (and running) within.

7. Test the frontend code in browser. Enter URL: http://192.168.99.100:80/

--

--

Jatin Aneja
0 Followers

Architect, Blogger, Java Passionate