COPY and ADD

At first glance, COPY and ADD look like they are doing the same task; however, there are some important differences. The COPY command is the more straightforward of the two:

COPY files/nginx.conf /etc/nginx/nginx.conf
COPY files/default.conf /etc/nginx/conf.d/default.conf

As you have probably guessed, we are copying two files from the files folder on the host we are building our image on. The first file is nginx.conf, which contains a basic NGINX configuration file:

    user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user
[$time_local] "$request" '
'$status $body_bytes_sent
"$http_referer" '
'"$http_user_agent"
"$http_x_forwarded_for"'
;
access_log /var/log/nginx/access.log main;
sendfile off;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}

This will overwrite the NGINX configuration that was installed as part of the APK installation in the RUN command. The next file, default.conf, is the most simple virtual host we can configure and has the following content:

    server {
location / {
root /usr/share/nginx/html;
}
}

Again, this will overwrite any existing files. So far, so good, so why use the ADD command?

ADD files/html.tar.gz /usr/share/nginx/

As you can see, we are adding a file called html.tar.gz, but we are not actually doing anything with the archive to uncompress it in our Dockerfile. This is because ADD automatically uploads, uncompresses, and puts the resulting folders and files at the path we tell it to, which in our case is /usr/share/nginx/, giving us our web root of /usr/share/nginx/html/ as we defined in the virtual host block.

The ADD command can also be used to add content from remote sources:

ADD http://www.myremotesource.com/files/html.tar.gz /usr/share/nginx/

This, for example, would download html.tar.gz from http://www.myremotesource.com/files/ and place the file in the /usr/share/nginx/ folder on the image. Archive files from a remote source are treated as files and are not uncompressed, so you will have to take this into account when using them.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset