How to create docker image using already configured application folder - docker

My basic requirement is to create docker image and deploy it to docker registry.
I have a pre-configured application folder(/home/myfolder) in my jenkins server(to do this configuration I have used ansible script). Then I need to create docker image from that folder and deploy it to docker registry.
What's the best way to do this? Please help me with this as I'm new to docker.
please find my Dockerfile below
#Download base image ubuntu 16.04
FROM ubuntu
WORKDIR /dockerprojects
#copy the zip file to docker folder
COPY /wso2telcohub-4.0.0-SNAPSHOT /home/myfolder/dockerprojects/IGW/dockerCI
COPY cp /wso2is-km-5.6.0 /home/myfolder/dockerprojects/IGW/dockerCI
CMD [“bash”]

There are a bunch of things in that Dockerfile that potentially can go sideways. I will comment on them one by one here:
#Download base image ubuntu 16.04
FROM ubuntu
If you intend to use the ubuntu:16.04 image, you need to specify it. Without a specific tag, the FROM instruction will look for the latest tag, in this case the find the image ubuntu:latest for you.
WORKDIR /dockerprojects
This command sets the workdir inside the docker image, so that when the container starts, the sessions PWD will be set to /dockerprojects. This is important because all other commands durring the build and when the container is started will be relative to this location in the file structure.
#copy the zip file to docker folder
COPY /wso2telcohub-4.0.0-SNAPSHOT /home/myfolder/dockerprojects/IGW/dockerCI
This command will copy the file /wso2telcohub-4.0.0-SNAPSHOT from the "host machine", the machine where the docker image is being built, into the image at the location /home/myfolder/dockerprojects/IGW/dockerCI. If the location does not already exist in the image, then it will create a file named dockerCI at the location /home/myfolder/dockerprojects/IGW/. I don't think that this is what you want.
Also, your comment states that this is a zip file, but it seems to be missing an extension like .zip or .gz - I believe that you are not referencing the file correctly.
COPY cp /wso2is-km-5.6.0 /home/myfolder/dockerprojects/IGW/dockerCI
This instruction will not execute. For the COPY instruction you don't need to use a "cp" command. If you removed "cp" from the line however, it would try to copy the file or directory /wso2is-km-5.6.0 from the host machine (that's a file in the root of the filesystem) to the location /home/myfolder/dockerprojects/IGW/dockerCI inside the resulting image.
CMD [“bash”]
The CMD instruction simply sets the image to start a new bash shell when started - which will make the container exit immediatly when the bash command completes.
I have a feeling that the source location of the files that you want to put in the image is not in the root of the host machine. But probably at /home/myfolder/dockerprojects/ on the host that you mention. I have asked you to clearify the location of the files that you want in the image in a comment on your question.
Update
The error that you are getting 'no such file or directory' means that the source file that you are referencing in the COPY instruction, does not exist.
The COPY instruction works like this:
COPY <sourcepath> <targetpath>
Where the <sourcepath> is the path of the file on the machine where the image is being built. This path is relative to the Dockerfile (or the build context if specified), unless it starts with a /, then it is relative to the root of the filesystem on the host machine. And the targetpath is the desired path inside the resulting image.
Let's say that I have the following folder structure:
/home/myfolder/dockerprojects/
├── Dockerfile
├── wso2telcohub-4.0.0-SNAPSHOT.zip
├── wso2is-km-5.6.0/
│ ├── anotherfile.txt
And I wanted all the files in the path /home/myfolder/dockerprojects/ to be put inside the docker image, below the path /app. I would do this with a Dockerfile like:
FROM ubuntu:16.04
WORKDIR /app
COPY . /app/
Or each file individually like this:
FROM ubuntu:16.04
WORKDIR /app
COPY ./wso2telcohub-4.0.0-SNAPSHOT.zip /app/wso2telcohub-4.0.0-SNAPSHOT.zip
COPY ./wso2is-km-5.6.0 /app/wso2is-km-5.6.0
That would leave me with the following in the docker image:
/app/
├── Dockerfile
├── wso2telcohub-4.0.0-SNAPSHOT.zip
├── wso2is-km-5.6.0/
│ ├── anotherfile.txt

Related

Extending docker image

I want to extend the existing redis:6.0-alpine image from docker-hub and want to add my configuration file in the container.
For that I've created this dockerfile
FROM redis:6.0-alpine
WORKDIR .
COPY redis.master.conf ./config/redis.conf
but when building a container from this image, there is nothing copyed at the specified location.
Setup:
wsl2 (ubuntu 18.04 distro)
windows 10
docker-for-windows (v20.10.2)
Some help ?
Just tested myself, and it's copied without issues.
Where are you trying to look for the file? Notice the entry directory of this image is not /, it's /data, hence your file is on /data/etc/redis.conf
This is because WORKDIR is set to . in your dockerfile, which means current working directory. If you look at the official dockerfile of redis:6.0-alpine, the WORKDIR is set to /data.
Hence according to your dockerfile, you are copying the redis.master.conf file to ./ which means /data. Please check your file at /data/etc/redis.conf. I tried this at my end and can confirm this behavior.
If you want to copy it at /etc/redis.conf then remove ./ before /etc.

How to copy files from a docker image - dockerfile cmd

During build time, I want to copy a file from the image (from folder /opt/myApp/myFile.xml), to my host folder /opt/temp
In the Dockerfile, I'm using the --mount as follows, trying to mount to my local test folder:
RUN --mount=target=/opt/temp,type=bind,source=test cp /opt/myApp/myFile.xml /opt/temp
I'm building the image successfully, but the local test folder is empty
any ideas?
Copying files from the image to the host at build-time is not supported.
This can easily be achieved during run-time using volumes.
However, if you really want to work-around this by all means, you can have a look in the custom build outputs documentation, that introduced support for this kind of activity.
Here is a simple example inspired from the official documentation:
Dockerfile
FROM alpine AS stage-a
RUN mkdir -p /opt/temp/
RUN touch /opt/temp/file-created-at-build-time
RUN echo "Content added at build-time" > /opt/temp/file-created-at-build-time
FROM scratch as custom-exporter
COPY --from=stage-a /opt/temp/file-created-at-build-time .
For this to work, you need to launch the build command using these arguments:
DOCKER_BUILDKIT=1 docker build --output out .
This will create on your host, aside the Dockerfile, a directory out with the file you need:
.
├── Dockerfile
└── out
└── file-created-at-build-time
cat out/file-created-at-build-time
Content added at build-time

Troubleshoot directory path error in COPY command in docker file

I am using COPY command in my docker file on top of ubuntu 16.04. I am getting error as no such file or directory eventhough the directory is present. In the below docker file I want to copy the directory "auth" present inside workspace directory to the docker image (at path /home/ubuntu) and then build the image.
FROM ubuntu:16.04
RUN apt-get update
COPY /home/ubuntu/authentication/workspace /home/ubuntu
WORKDIR /home/ubuntu/auth
a Dockerfile COPY command can only refer to files under the context - the current location of the Dockerfile, aka .
so you have a few options now:
if it is possible to copy the /home/ubuntu/authentication/workspace/ directory content to somewhere inside your project before the build (so now it will be included in your Dockerfile context and you can access it via COPY ./path/to/content /home/ubuntu) it can be great. but sometimes you dont want it.
instead of copying the directory, bind it to your container via a volume:
when you run the container, add a -v option:
docker run [....] -v /home/ubuntu/authentication/workspace:/home/ubuntu [...]
mind that a volume is designed so any change you made inside the container dir(/home/ubuntu) will affect the bound directory on your host side (/home/ubuntu/authentication/workspace) and vice versa.
i found a something over here: this guy is forcing the Dockerfile to accept his context- he is sitting inside the /home/ubuntu/authentication/workspace/ directory, and running there
docker build . -f /path/to/Dockerfile
so now inside his Dockerfile he can refer to /home/ubuntu/authentication/workspace as his context (.)

Docker: Ignore some files which are needed in build context

I have a folder : /docker on my project root and it contains certain files like Apache configs which I need to copy to the correct folders during teh build.
Project-root
------docker folder
-----------apache.conf
Now
DOCKERFILE
In my dockerfile i have a copy command which copies the whole of root to the container.
`Issue`
I don't want the docker folder to be copied to the built image.
I added docker to the .dockerignore. But this leads to the problem that these files are not sent to the build context and then the build command fails.
WORKAROUND
Use the RUN command in the dockerfile to remove the docker folder instead of using .dockerignore
QUESTION
Is there any better solution?
DOCKER FILe:
FROM <baseimage>
WORKDIR /var/www/html/
COPY . /var/www/html/MYFOLDER
COPY ./docker/apache.conf /etc/httpd/conf/apache.conf
COPY ./docker/sites-enabled /etc/httpd/sites-enabled

Dockerfile COPY from a Windows file system to a docker container

I have a simple Dockerfile:
FROM php:7.1-apache
LABEL maintainer="rburton#agsource.com"
COPY C:/Users/rburton/code/MyAgsourceAPI /var/www
It is the last line that is giving me problems. I am copying from a Windows structure to a docker container (Linux I assume). When I build this image I get:
...
Step 3/3 : COPY C:/Users/rburton/code/MyAgsourceAPI /var/www
COPY failed: stat /var/lib/docker/tmp/dockerbuilder720374851/C:/Users/rburton/code/MyAgsourceAPI: no such file or directory
First, something is preventing the recognition that this is an absolute path and naturally if the path is pre-pended with /var/lib/docker/tmp/dockerbuilder720374851 then the file will not be found. Second, I have tried / and \ but all with the same result. Also the drive letter I suspect is confusing to docker. So the question is how do I copy files and folders (along with the contents) from a Windows folder to a docker container?
First, change your Dockerfile to:
FROM php:7.1-apache
LABEL maintainer="rburton#agsource.com"
COPY MyAgsourceAPI /var/www
Then, to go your code directory: cd Users/rburton/code.
Within that directory, run:
docker build -t <image_name> .
Another tip that might be helpful, I've seen same issue while running build from correct context, and issue remained until I've used all small caps on src folder that I wanted to copy from. eg:
COPY MyAgsourceAPI /var/www -> COPY myagsourceapi /var/www
The root of the path is relative to the Dockerfile and not your Windows filesystem.
If for example your filesystem is layed out like this:
+-+-MyProject
|
+---Dockerfile
|
+-+-build
|
+---MyAgsourceAPI
You can use:
COPY /build/MyAgsourceAPI /var/www
Note that "MyProject" (or anything above it) is excluded from the path.

Resources