Thursday 23 May 2019

Docker: How to copy artefacts to docker image


You can add artefacts to a container using either COPY or ADD command.

COPY : Copy the files or directories from your local system to the container file system

ADD : COPY command + tar file auto extraction in the image.
For example, you can tar the entire directory structure and add it to the container using ADD command, then the tar file will be auto-extracted once it is copied to the container.

Follow below step-by-step instructions to add files to the container.

Step 1: Create a directory ‘welcomeWeb’.

Step 2: Copy 'helloWorld.war' file to welcomeWeb directory. You can get this file from 'https://github.com/harikrishna553/dockerExamples'. Or you can refer my previous post, on how to create a war file.
$mkdir welcomeWeb
$
$cp /Users/krishna/Documents/helloworld.war ./welcomeWeb/
$cd welcomeWeb/
$
$ls
helloworld.war


Step 3: Create a file with name 'Dockerfile' in welcomeWeb directory.
$touch Dockerfile
$ls
Dockerfile helloworld.war

Step 4: Update ‘Dockerfile’ with below information.


Dockerfile
FROM jboss/wildfly

COPY helloworld.war /opt/jboss/wildfly/standalone/deployments/helloworld.war


Step 5: Create docker image by executing the command 'docker image build -t helloweb .' from the directory where the Dockerfile is located.
$docker image build -t helloweb .
Sending build context to Docker daemon  4.608kB
Step 1/2 : FROM jboss/wildfly
 ---> 5de2811bb236
Step 2/2 : COPY helloworld.war /opt/jboss/wildfly/standalone/deployments/helloworld.war
 ---> f033c1fe2ca1
Successfully built f033c1fe2ca1
Successfully tagged helloweb:latest


Step 6: Execute the command 'docker image ls' to see all the available images in your system.
$docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
helloweb            latest              f033c1fe2ca1        55 seconds ago      745MB
jboss/wildfly       latest              5de2811bb236        7 weeks ago         745MB

Step 7: Run the image by executing the command 'docker run -p 1234:8080 -d helloweb'.


Open the url 'http://localhost:1234/helloworld/welcome' in browser, you can see below screen.


Previous                                                 Next                                                 Home

No comments:

Post a Comment