8

As far as I'm concerned you can run a command line when building an image with RUN or when running a container with CMD. Is there anyway to do so when starting a docker container?

My goal is to run the gcloud datastore automatically just after typing docker start my_container_name.

If this is possible, which changes should I apply to my Dockerfile?

(I have already installed all the packages required and I can run that command after docker run --name my_container_name -i -t my_image_name but I want it to be run also when starting the container)

6
  • I think you need the docker run command. as an example: docker run -d -p 80:80 my_image service nginx start, see docs for details. Commented Mar 23, 2021 at 10:17
  • I thought that when using docker run each time I am creating a new container. Shouldn't I be able to reuse the same more than once? Commented Mar 23, 2021 at 10:19
  • What's the contents of your Dockerfile? Commented Mar 23, 2021 at 10:21
  • Well, I install al the packages that I am working with (python, bash, gcloud and other dependencies) and later on I am able to perform RUN gcloud... and also CMD gcloud... so there is no problem with that command. However, I want to run that same command after having created the container automatically, is that possible? In the same way that is executed when using docker run, I want it to be executed with docker start Commented Mar 23, 2021 at 10:26
  • 1
    Creating a container isn't expensive. Just delete the old container and start a new one with the new command. You shouldn't usually need docker start. Commented Mar 23, 2021 at 11:06

1 Answer 1

16

Docker executes RUN commands when you build the image.

Docker executes the ENTRYPOINT command when you start the container. CMD goes as arguments to ENTRYPOINT. Both of these can be overridden when you create a container from an image. Their purpose in a Dockerfile is to provide defaults for the future when you or someone else will be creating containers from this image.

Consider the example:

FROM debian:buster

RUN apt update && apt install procps

ENTRYPOINT ["/usr/bin/ps"]
CMD ["aux"]

The RUN command adds the ps executable to the image, ENTRYPOINT and CMD are not executed but they will be when you run the container:

# create a container named 'ps' using default CMD and ENTRYPOINT
docker run --name ps my_image
# equivalent to /usr/bin/ps aux

# start the existing container 'ps'
docker start ps
# equivalent to /usr/bin/ps aux

# override CMD
docker run my_image au
# equivalent to /usr/bin/ps au

# override both CMD and ENTRYPOINT
docker run --entrypoint=/bin/bash my_image -c 'echo "Hello, world!"'
# will print Hello, world! instead of using ps aux

# no ENTRYPOINT, only CMD
docker run --entrypoint="" my_image /bin/bash -c 'echo "Hello, world!"'
# the output is the same as above

Each time you use docker run you create a container. The used ENTRYPOINT and CMD are saved as container properties and executed each time you start the container.

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

5 Comments

Would I also see "Hello, world!" on the terminal if I run docker start container_name with one of the containers created as a result of one of the last two commands? If it does, that would be the solution I was looking for
@anmarlea for that you need to docker start -a <container_name>. This way it will attach output streams and forward signals (like Ctrl+C). See docker start --help for more.
Wow, I was precisely looking for docker start -a <container_name>. I was already able to run the command when using docker run but no with docker start and it was because I was missing the -a argument. Thank you very much!
@anmarlea The thing is that docker did run the command even without -a key, you just didn't see the output. In such case you can retrieve output using docker logs <container_name>.
I was considering that possibility too but I ran some test in order to check it and couldn't confirm it. As I wanted the logs too, I was looking somehow to watch the whole thing in the same terminal, that's why my question might seem a little bit confusing (asking how to run a command after having already run it). Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.