Docker Community Forums

Share and learn in the Docker community.

Building images in docker


(Anvika1) #1

I want to build the gnu compiler image inside the ubuntu image. So i created the basefile, ubuntu now I have to install the gnu compiler and several other components in that. For example, like installing a software on the linux virtual machine. How can do this ? Also, in docker can the components be built by executing the source code and not running the exe or dmg file ?


(Kostasmistos) #2

First of all i will answer with question. Why don’t you use an already/prebuild image of GNU??

That being said, for building from source code you can do it. Put the source code root dir, somewhere under the folder structure containing your Dockerfile, COPY that to the container and create a shell script with your installation instructions. Then just RUN sh and if you have managed correctly all prerequisites the build should be successful.
Do not forget to remove the source code after you build it.


(Anvika1) #3

It was specified to build from source code.
Thank you for the answer. I appreciate your time, but I am new to docker it would be great if you can elaborate or explain a bit more. You already have a well explained answer, but just in case if you can elaborate any part you like.


(Kostasmistos) #4

Yes i will try to make it more clear:

Let’s start from the Dockerfile first. The Docker build which references the Dockerfile is a powerful tool.
You can basically chain commands as you where about to execute then in a blank terminal.
I suggest for your case is better to prepare the installation steps you need as you where about to execute the installation normally and not inside the container. For example let’s assume you need the autoconf and automake tools first, think how you want to install them (by a package-manager or by source), in the first case you will do something like “apt-get install autoconf automake” in the second you first need the source code, so you get it as a zip archive or form git directly or your own code, in each case you do something else to acquire the source, peek your preferable way and continue to make the build.

So getting the above to the container.

Your Dockerfile should be similar to this:

Form ubuntu:14.0

RUN apt-get -y update && apt-get install -y wget
git
build-essential \

COPY /sourceCode /home/username/sourceCode
RUN “commands to build the sources here”

Notice that the sourceCode folder on the host must be in the same folder or in subfolder to the one your Docker file is. And in case you want to download from git you need the ssh certificates.

Hope this is more helpful!!
Just keep in mind that you can execute commands from a Dockerfile as on a normal terminal and you be doing fine.
Later on you should see docs for optimization of the Dockerfile creation here


(Nathan Le Claire) #5

As @kostasmistos noted, use a sequence of RUN commands in the Dockerfile to compile software from source that will be used in the final image.

Your Dockerfile will look something like this:

FROM ubuntu

RUN apt-get update && apt-get install -y build-essential gcc wget git
RUN git clone https://github.com/user/source.git
WORKDIR /source
RUN ./configure
RUN make 
RUN make install
ENTRYPOINT ["myprogram"]

(Anvika1) #6

Thank you so much for the elaborated reply. I will try to implement this.
Would reach out if i face any issues. -nathanleclaire - kostasmistos


(Anvika1) #7

Hi @nathanleclaire ! I tried to use your solution today and make the dockerfile. Everything seems okay, except the “myprogram” part in ENTRYPOINT.

FROM ubuntu

RUN apt-get update && apt-get install -y build-essential gcc wget git
RUN git clone https://github.com/gcc-mirror/gcc.git
WORKDIR /gcc
RUN ./configure
RUN make
RUN make install
ENTRYPOINT [“myprogram”]

So this is what mydockerfile looks as of now. Kindly guide me through the missing steps.


#8

Comment out the ENTRYPOINT in the Dockerfile, and build the image again. Then, RUN the image with an interactive shell, and you can validate that your source code compiled and installed correctly. This should get you to a shell prompt in your container.

docker run -it myImage /bin/bash

Depending on what you need to do next with the compiled code will better determine if you need a ENTRYPOINT, CMD or neither.


(Anvika1) #9

Let me try this. Thanks for the reply.


(Nathan Le Claire) #10

In ENTRYPOINT ["myprogram"], ‘myprogram’ should be replaced by whichever binary you’re going to invoke (the compiled artifact).


(Anvika1) #11

Can you suggest any readings about this. I don’t want to ask silly questions and waste your time. i want to understand all the concepts deeply. Any reference from where you learnt this, would be helpful.

Frankly speaking, I thought apt-get install (first line in the docker file) would take the source code and then the make and make install would install it. Now i don’t understand why do we need ENTRYPOINT. My question would be naive, but i am sure if i learn to build one component I can build the rest.

Thanks for your patience.


#12

Have a look at the Dockerfile reference. This should give you a little more depth.

Here is a post discussing the difference between ENTRYPOINT & CMD. It helped me understand it better.