Extending docker image - docker

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.

Related

Why would you use a shared volume and also copy the project directory in Docker Compose

I'm following this tutorial on using Docker Compose to deploy a Django application. It suggests that in my Dockerfile I should have (abbreviated for clarity):
WORKDIR /usr/src/app
COPY . .
Where the local directory is the Django project root. But also that in my docker-compose.yml (in the parent directory to the project root) I should have the line
volumes:
- ./app/:/usr/src/app/
Where ./app/ is the project root, relative to docker-compose.yml. From reading, the volumes line in docker-compose should make my host directory ./app/ available in the container at /usr/src/app/. Testing also seems to confirm this.
In that case, what's the point of the COPY . . line? In fact beyond the point, why does it not throw an error? It seems to be copying a directory over itself.
Well at beginning in the tutorial, the write just pretends run Dockerfile and docker-composer without entrypoint.sh then is not necessary use: COPY . .
But while you continues reading there is a point in Dockerfile that is required have the code of Django inside de Container Docker.
The line that I'm talking about ends in ENTRYPOINT ["/usr/src/app/entrypoint.sh"]
So if COPY . . is not present, the image building will throw error.
I hope you got me, what I'm say.

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 (.)

What does . mean in docker? Does it mean the current working directory of the image or the local machine?

I am confused about whether . means that it's a shortened abbreviation of the current directory of the image or if it's the current working directory on the local machine. Or is it the same meaning of . in most console commands like essentially selecting all in the current directory.
COPY somecode.java .
#copy the rest of the code
COPY . .
The . also seems to mean find the docker file in the current directory.
docker build -t image-tag .
The . simply means "current working directory"
docker build
In the context of the docker build command, you are using it to signal that the build context for docker build is the current working directory. Like so:
docker build -t mytag:0.1 .
Let's say that you have this structure:
/home/me/myapp/
├── Dockerfile
├── theapp.py
And you invoke the docker build command from /home/me/myapp - you will pass the current working directory as the build context. This means that docker will see the following filestructure when building:
/
├── Dockerfile
├── theapp.py
Dockerfile
In the context of a Dockerfile, it means that same. Both inside and outside the image.
Take this COPY instruction for example:
COPY . /app
Here the . means the current working directory, where the docker build command is executed. This will be relative the to build context that is passed to the docker build command.
For this COPY instruction:
COPY theapp.py .
It means, take the file theapp.py and copy it to the working directory of the docker image that is being built. This directory can be set at build time with the WORKDIR instruction, so that:
WORKDIR /app
COPY theapp.py .
Would leave you with the file /app/theapp.py inside the resulting docker image.
Finally, this COPY instruction:
COPY . .
Means take everything from the working directory where the docker build command is issued, relative to the build context that is passed to it. And copy it to the current working directory of the docker image.
I saw 3 . characters on your question, so let me expand one by one.
The first, as you imagine, the . character means the current directory.
In your Dockerfile
COPY . .
The second dot represented the current location on your virtual machine. Whenever you run cd command in the Dockerfile. That may be easy to understand.
The first dot more unintelligible a little. The first dot character represented the current location on your host machine. The location you input after docker build command like that:"docker build [options] <location>".
Docker build command
The dot character means the current directory whenever you call your docker build command. For example:
[~]$ docker build .
The dot character represented for default home directory of this user on your real machine.
It depends on the context. In your COPY somecode.java . it's the image. In COPY . . it's both. The first dot is in the local machine and the second dot is the image.
In the docker build command, it tells Docker to take files for the newly built image from the working directory on your local machine.
As others said, it's basically just means "current working directory". But when building a Docker image, there are two of those. One in your local machine where you're building the image. The second one is the file system that's built in the image.
.(dot) means current working directory.

How to create docker image using already configured application folder

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

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