0

I have a python code for which I want to create a docker image. Now as per my understanding we need a Dockerfile and our python code code.py. Inside a Dockerfile we need to write:

FROM python:3

ADD code.py /

RUN pip3 install imapclient

CMD [ "python", "./code.py" ]

My first question is about this Dockerfile. First we have mentioned FROM python:3 because we want to use python3. Next we have added our code. In RUN we can write a dependency of our code. So for example if our code need python package imapclient we can mention it here so that it will be installed before docker file is build. But what if our code do not have any requirements.? Is this line RUN important. Can we exclude it when we don't need it.?

So now let's say we have finally created our docker image python-hello-world by using command docker build -t python-hello-world .. I can see it using command docker images -a. Now when I do docker ps, it is not listed there because the container is not running. Now to start it, I'll have to do docker run python-hello-world. This will start the code. But I want it to be running always in the background like a Linux service. How to do that.?

1 Answer 1

1

Is this line RUN important? Can we exclude it when we don't need it?

Yes if your code doesn't need the packages then you can exclude it.

But I want it to be running always in the background like a Linux service. How to do that?

If you want to run it as background then use below command.

docker run -d --restart=always python-hello-world

This will start container in background and will start automatically when system reboots.

Sign up to request clarification or add additional context in comments.

3 Comments

I actually used --restart flag but didnt used -d so it gave me an error. Well thanks. One more question, when we use docker build command why we use . at the end
. referes to current directory in linux, docker build . means its using dockerfile present in current directory
And if we are running the command from another directory we need to give path instead of . .?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.