Docker Community Forums

Share and learn in the Docker community.

How to write the dockerfile to run docker container from the dockerfile


(kumardst) #1

Hi Team,

I created docker image for DB2. if i run the container and then started, but i can’t start the DB2instance…getting error “shared memory allocated”. for that error i got solution run container with “docker run -P --privileged=true -it db2-express-c” it working.

But, i want to run this command “docker run -P --privileged=true -it db2-express-c” from inside the dockerfile

cons:- I want run container from inside the dockerfile

Thanks
Pavankumar


(Sam Saffron) #2

What I usually do is mount the socket for docker in the container and then I can access docker directly via that socket.


(kumardst) #3

you Meant say while start the DB2 image… specific with volume?


(Sam Saffron) #4

just reread …

There is no way for you to specify in a Dockerfile that an image must be run in privileged mode, you would need to fish for that information and crash out.

Do you know why db2 requires privileged mode to work?


(kumardst) #5

yes… it required to operate “sysctl” command’s on host. other wise, it will read mode only…
1.) is there any possibility while building the image, at any point of time can we start that image(with Image ID)?


(Sam Saffron) #6

Images and running images are 2 diff things, once you create an image you can run it privileged or unprivileged, with volumes or without.


(Joe) #7

Hi Pavankumar,

A Dockerfile will hold “instructions” to build a Docker image.

One such instruction is called RUN, with which you can “run” specific commands.

So instead of trying to run a container to do a specific task, can you not try to run a command?

Please try that and see if that works.

Cheers,
Joe


(kumardst) #8

Hi Joe,
I tried with “RUN” but it failed to start the image on the process of building image… in more detailed
1.) i need to start a image to a container… at the end point of image building in a process.

Here, while building docker image with dockerfile i need to start that same image before build complete( by using image ID$ )… task to do is when running command “docker build -t db2 .” the vale have to take from host machine…
now at step 9 is

docker run -i -t `docker images | grep -v "REPOSITORY" | head -1 | awk '{print $3}'` /bin/bash 

Regards
Pavankumar


(Joe) #9

HI Pavankumar,

Can you please explain to us, why you want to run a container while creating an image (using the docker build command and giving it a Dockerfile)?

If you want a container to run an application the moment it is instantiated from an image, ENTRYPOINT is the command to use inside the Dockerfile.

Try this approach and see if that helps.

Cheers,
Joe


(kumardst) #10

Hi Joe,
Because i need to run two, three application in one Dockerfile. So, Like eg:- Apache and DB2 at a time. in one Dockerfile…

The ENTRYPOINT is like a start the application while container start’s right? any way i will try that option also

Regards
Pavankumar


(Joe) #11

Got it Pavankumar.

The general philosophy is this - “one app per container”. You will see this idea manifested in many places, for example, multiple ENTRYPOINT or CMD lines in a Dockerfile will be ignored and only the last entry will be accepted.

So I would recommend you change your design to accommodate this idea - “one app per container”.

But if you still want to run more than one app in a container, one idea I can think of, is to list them in a shell script and make that as an ENTRYPOINT.

Cheers,
Joe


(kumardst) #12

HI Joe,
it’s not container…it’s image( earlier replay)

I tried with giving the shell script in ENTRYPOINT…But the Error is “SQL1042C An unexpected system error occurred.” for the db2.( that error due to un-authorized to start service)
{Docker command not working inside the container’s (at ENTRYPOINT)}

  1. why am asking is, for DB2 need to run the image in “–privileged=true” other wise it can’t start the instances.
  2. For building Sonarqube and jira… web services need the DB. to config and install the application.
    trying for automachine. eg:- building image from jenkins using Dockerfile, in case JIRA
    2.1)after completing of DB2 login to DB2 image then create the instances, later committing the container.
    2.2) Now one more file with hard coded value’s file to build the JIRA…
  3. Same instances not used for multiple application. so, if same Dockerfile run’s in different Docker engine. the output will be error’s right?

Regards
Kumar


(Joe) #13

Hi Kumar,

Here is what I suggest:

  • Keep the DB2 in a separate image.
  • Enter these in its Dockerfile:

ENTRYPOINT [“path to DB2 startup script”]
CMD ["–privileged=true"]

  • docker run --name=db2_db -P db2-express-c

Confirm that this works.

  • Now run your apps in a separate containers and link them with the db2_db container

You should be good to go.

-Joe


(Joe) #14

Hi Kumar,

The more I think about your issue here, the more I think that it is a simple USER issue, meaning the ibmcom/db2express-c image might not have been setup to run as root.

In the Dockerfile, do this:

FROM ibmcom/db2express-c


USER root

I am unable to test this, as ibmcom/db2express-c is not available now.

Let us know what you see.

-Joe


(kumardst) #15

Hi Joe,
Sorry for later reply… i was hospitalized
I have tried with that option earlier it self , but the issue… when you specifying in Dockerfile
(Ex:- From db2-express:1 )
that image not stating with “privileged”. and also specifyed the script to start the instance

Step 0 : FROM testdb2

—> e52568fc5c39
Step 1 : ADD db2.sh /tmp/db2.sh
—> b8a25ea9b523
Removing intermediate container cf6837013b0e
Step 2 : RUN chmod +x /tmp/db2.sh && /tmp/db2.sh
—> Running in 416f3fdd556b
SQL1220N The database manager failed to allocate shared memory.
INFO[0063] The command [/bin/sh -c chmod +x /tmp/db2.sh && /tmp/db2.sh] returned a non-zero code: 2

And one thing am running with User root

Is there any option to build with “privileged”… that will help me to resolve this issue…

Thanks
Pvankumar


(Joe) #16

Hi,

“privileged” mode is a run time option for Docker Containers.

I think the best solution for you is to debug the Dockerfile step by step.

For example, I would recommend, building a container till the point you run the db2.sh command, then bash into that container and try that command. Make sure that, that works. Then proceed.

-Joe


(kumardst) #17

Hi Joe,
In that Case… automation is not possible right ?

Thankyou
Pavankumar


(Joe) #18

Sorry, that is not what I meant.

I believe that anything that can be done manually consistently, can be automated, to the most part! :smile:

What I meant here is that, go as far as you can go with the steps in Dockerfile and when it fails, create a Container with until the previous step, then bash into that Container and run the command and see what is failing and why it is failing. Fix that and move forward with automation. If it does not work with manual steps, it will not work with automation.

Cheers,
Joe


(kumardst) #19

Hi Joe,
i tried that db2 not working without --privileged=true. So, i chose my env with mysql

Thank you

Regards
Pavankumar