How to access local file when building from Dockerfile? - docker

I need hello.rpm when I build from Dockerfile, but this rpm file is not available online.
Right now I'm serving the file by firing up a temporary web server, but ideally I'd like to run a build command which makes this local file available inside the container.
Is this possible?
My build command:
docker build -t fredrik/helloworld:1.0 .
My Dockerfile:
FROM centos:6
RUN rpm -ivh hello.rpm

Why don't you COPY the file inside the container before executing RUN?
FROM centos:6
COPY hello.rpm /tmp/hello.rpm
RUN rpm -ivh /tmp/hello.rpm
This assumes that hello.rpm is next to your Dockerfile when you build it.

Otherwise if an internet connection is not a limiting factor while you're working just:
Upload the file a cloud as Dropbox
Go to your docker shell and wget https://www.cloudnameXYZ.com/filename

Related

Creating my first docker image with a local zip

very new to Docker here. I am trying to use a maven 2.1.0 zip to create a docker image.
my
dockerfile.docker file is :
# syntax=docker/dockerfile:1
FROM scratch
LABEL maintainer="Myname"
LABEL maintainer="myemail"
RUN wget HTTP://archive.apache.org/dist/maven/binaries/apache-maven-2.1.0-bin.zip
RUN unzip
I am not exactly sure if I am doing this right
docker build -t apache-maven:2.1.0 .
Essentially I just wanted to create this image locally so I could then push it out to my targeted endpoint. Any help realizing what I did wrong would be appreciated. Whenever I run this build command it tells me it failed to read the dockerfile and that there's no such file or directory.
By default, it will try to find the file with the exact name Dockerfile.
If for any reason, you want to have a different file name like your scenario, you should use next:
docker build -f dockerfile.docker -t apache-maven:2.1.0 .
Detail refers to Specify a Dockerfile (-f)

Source files are updated, but CMD does not reflect

I'm new to docker and am trying to dockerize an app I have. Here is the dockerfile I am using:
FROM golang:1.10
WORKDIR /go/src/github.com/myuser/pkg
ADD . .
RUN curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
RUN dep ensure
CMD ["go", "run", "cmd/pkg/main.go"]
The issue I am running into is that I will update source files on my local machine with some log statements, rebuild the image, and try running it in a container. However, the CMD (go run cmd/pkg/main.go) will not reflect the changes I made.
I looked into the container filesystem and I see that the source files are updated and match what I have locally. But when I run go run cmd/pkg/main.go within the container, I don't see the log statements I added.
I've tried using the --no-cache option when building the image, but that doesn't seem to help. Is this a problem with the golang image, or my dockerfile setup?
UPDATE: I have found the issue. The issue is related to using dep for vendoring. The vendor folder had outdated files for my package because dep ensure was pulling them from github instead of locally. I will be moving to go 1.1 which support to go modules to fix this.
I see several things:
According to your Dockerfile
Maybe you need a dep init before dep ensure
Probably you need to check if main.go path is correct.
According to docker philosophy
In my humble opinion, you should create an image with docker build -t <your_image_name> ., executing that where your Dockerfile is, but without CMD line.
I would execute your go run <your main.go> in your docker run -d <your_image_name> go run <cmd/pkg/main.go> or whatever is your command.
If something is wrong, you can check exited containers with docker ps -a and furthermore check logs with docker logs <your_CONTAINER_name/id>
Other way to check logs is access to the container using bash and execute go run manually:
docker run -ti <your_image_name> bash
# go run blablabla

Build with docker into host directory

I'm fairly new to docker, but I'm trying to see if I can use it to build the frontend app for a project using it and take the built app and hand it off to another tool.
So ideally, I'd like to:
1) Setup environment using Dockerfile.
2) Run npm run build
What i'm not sure is how can I access the build folder from the container from my host?
My docker file is:
FROM kkarczmarczyk/node-yarn:latest
WORKDIR /app
ADD . /app
RUN yarn --ignore-engines
RUN yarn run build
Then I do:
docker build -t build-app
From the prompts it looks like it builds properly, but I don't know how to get the built app from the container. Its building to a /dist folder on the container.
How can I access it from the host?
You need to mount a volume to your host machine, which allows you to share that particular directory, bidirectional with the container.
You could do this something like
docker run -v <host-path>:<container-path> <image-id>
Refer this answer. docker mounting volumes on host

Referencing files inside build (Docker)

I use boot2docker and want to build a simple docker image with the Dockerfile:
# Pull base image.
FROM elasticsearch
# Install Marvel plugin
RUN \
&& export ES_HOME=/usr/share/elasticsearch \
&& cd $ES_HOME \
&& bin/plugin -u file:///c/Users/buliov1/dev/elastic/plugins/marvel-latest.zip -i elasticsearch/marvel/latest
The path /c/Users/buliov1/dev/elastic/plugins/marvel-latest.zip is present and accessible on the machine where I build the dockerfile .
The problem is that inside the build i get
Failed: FileNotFoundException[/c/Users/buliov1/dev/elastic/plugins/marvel-latest.zip (No such file or directory)].
I searched through the documentation and the only solution I see is to use ADD/COPY and copy first the file inside the image and then run the command that uses the file.
I don't know how exactly docker build works but , is there a way to build it without copying the file first?
A docker build process is running inside Docker containers and has no access to the host filesystem. The only way to get files into the build environment is through the use of the ADD or COPY mechanism (or by fetching them over the network using, e.g., curl or wget or something).

Sending docker build contexts

My directory structure is as follows
cassandra
Dockerfile
downloads
225M file
I am inside cassandra directory. My build command is
docker build -t image_cassandra .
I know that it will send all the contents in . current directory. So it takes so much of time to send this 225M file. I need this file in my Dockerfile.
Add downloads/ /tmp/
I want to avoid this much of delay. And I know that, we cannot use ../ in docker ADD command. So is there any way to reduce the size of build context and have this ADD command.
This file is not part of web. So i cannot use any apt-get wget statements. Or isn't possible?
You could separate the project into two docker images, a big one that changes infrequently, and a small one that you can change fast.
Project1/
Dockerfile
bigfile
Project2/
Dockerfile
Project1/Dockerfile would look like this:
FROM ubuntu
RUN apt-get install cassandra
ADD bigfile
Then if you build it and tag it with docker build -t project1 Project1, you can use the result in Project2/Dockerfile:
FROM project1
RUN fast configuration commands

Resources