Docker Community Forums

Share and learn in the Docker community.

Docker File COPY


(Sackerman) #1

Docker 17.12.0-CE running under Centos 7.3

I am very new to docker however I am having a difficult issue with COPY.

I have a very simple dockerfile that I am using to setup a nagios instance:

FROM jasonrivers/nagios:latest
EXPOSE 8080
WORKDIR /etc/postfix
COPY main.cf main.cf
COPY master.cf master.cf

Every time I exec into the container and view the contents of the “main.cf” file one particular line is not getting copied correctly.

Source File contains:
relayhost = [smtp.sparkpostmail.com]:587

When I view it in the running container:
relayhost =

I have even tried modifying the line and removed the ‘[]’

and then rebuilding the image but the running container still ends up with the same problem. I can change any other line, add comments,etc and they get reflected correctly. I feel like I am missing something simple here.


(Isaiah Baker) #2

Can you share the output of your docker build command?

See if your COPY step runs without any errors. (Like so:)

Step 5/5 : COPY main.cf main.cf
 ---> 37049d19561b
Successfully built 37049d19561b
Successfully tagged nagios-test:latest

(Sackerman) #3

Yep, the interesting thing is that all of the other lines in main.cf are being copied correctly, except that one.

[root@docker nagios]# docker image build -t nagios .
Sending build context to Docker daemon  12.29kB
Step 1/5 : FROM jasonrivers/nagios:latest
 ---> 8dcbe7de7b64
Step 2/5 : EXPOSE 8080
 ---> Using cache
 ---> dfe812e55d46
Step 3/5 : WORKDIR /etc/postfix
 ---> Using cache
 ---> 1ffba04ac099
Step 4/5 : COPY main.cf main.cf
 ---> Using cache
 ---> f1a85f7f2ac7
Step 5/5 : COPY master.cf master.cf
 ---> Using cache
 ---> 003c4fded71f
Successfully built 003c4fded71f
Successfully tagged nagios:latest

(Sam) #4

if you do a copy (cp command) of that file on the host system, does it work correctly? is there an embedded end of file character??


(Sackerman) #5

Yep, CP from host to container after container is running is correct. However if I shut the container down (docker-compose down) and restart it it and view it the line is incorrect.


(Sam) #6

weird… if you zip the file and use ADD instead (it will unzip the file) of COPY does it transfer correctly?


(Sackerman) #7

I tried ADD, but I ended up with an error

WORKDIR /etc/postfix
ADD main.tgz main.cf
ADD master.tgz master.cf

Step 4/5 : ADD main.tgz main.cf
failed to copy files: Error processing tar file(exit status 1): Error setting up pivot dir: mkdir /var/lib/docker/devicemapper/mnt/3ca8b023f15f3efc1515293e9e0523236419ad2a5bba40e1b4c34be7b33ddcc9/rootfs/etc/postfix/main.cf/.pivot_root008051742: not a directory

(Sam) #8

I think you just

ADD main.tgz . <----(dot)

it will unload it to the current directory… assumes the target IS a directory


(Sackerman) #9

I should have thought of that :wink:

but that didn’t work either 'relayhost = ’ is still missing the host on the right side of the ‘=’ sign.


(Sam) #10

ok, my last question… how are u looking at the file? cat, nano, vi?

if you docker cp the file back to the host, then look at it, it is still missing that part?


(Sackerman) #11

I am using cat, and yes, if I copy the file from the container back to the host it is missing that part.


(Sackerman) #12

I think I know why it is appearing the way it is but I don't know how to fix it. If I view the main.cf file without copying my file into the container it has that line 'relayhost = '. So my guess is that when I am copying file file into the container that docker only copies in lines that don't exist. Does that sound correct? and if so how do I change that behavior?Also how do I get this silly editor to ignore the fact that ‘maindotcf’ is not a domain name? It won’t let me post anything with more that what it thinks are 2 links because I am new, or it thinks that it is a bogus URL.


(Sam) #13

makes no sense… but change the file, add a space and save it on the host… IF there is caching, then it will have changed and need to be copied again. then rebuild the image

i don’t know on the forum editor…


(Sackerman) #14

It’s not using the cached version, I have changed several lines and moved them around in different orders and every other line that I change gets reflected in the container except this one. The only thing I can figure is that all other lines that I am copying in the file do not already exist and apparently it won’t overwrite a line that does. Is there anyway that I can delete that file in the container before I copy in the new file?


(Sam) #15

well, you could delete the current image, before creating another one…

docker rmi image_id (or name/tag)

i think something is interpreting that line ([ symbolic_name ]) and replacing it… but I don’t know why…


(Sackerman) #16

I think the problem is that the maindotcf file is automatically populated through the postfix install so I don’t want to delete the entire image just that one file since docker doesn’t seem to want to let me overwrite it using the ‘copy’ command.


(Sam) #17

postfx install — I don’t see that in the dockerfile

do the copy AFTER the postfix install, with cmd or as part of the entrypoint script.

copy it someplace temporarily in the build phase, then do the install, then copy the file over…
and I bet you can cat it from the temporary place at it will be fine

you left that little postfix install step out of all the prior info…


(Sackerman) #18

I am using jasonrivers/nagios:latest as FROM in my docker file and the only thing that my docker file is doing is exposing a port and copying those 2 files. So the files are being copied as the very last step. I guess I am going to have to edit the jasonrivers/nagios:latest docker file. I even tried running a RUN rm maindotcf before the copy commands and it didn’t complain when I built the image but it still didn’t work.


(Sackerman) #19

So I am not adding postfix, it already exists in the jasonrivers dockerfile.


(Sam) #20

ok, i have no idea what the source image does…

bit I think we should step back and see exactly what u are trying to accomplish vs what the image is doing…

I don’t think this is a docker problem now… but a solution design issue…

the image creator assumes that the .cf files are ‘complete’ before running its startup… you apparently want to override that design …

according to the dockerfile, postfix is installed BEFORE your file is copied… (its add/copy/run commands are executed before yours…)

so the final CMD [ “/usr/local/bin/start_nagios” ] so, whatever that does should be looked at…

I copy config files in my Dockerfiles in this design for apache config, but it doesn’t MODIFY the files in its startup…

I think u can prove the startup is modifying your file by doing

docker run -it other_parms  image_name /bin/bash

and from that commandline examine your file… before the startup runs… execute the startup and verify that the file was modified…