Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dockerfile: how use CMD or ENTRYPOINT from base image

Tags:

I have several base docker images which are not owned by me (so I cannot modify them). However, I'm creating new images from them with additional things installed.

What I can't figure out is how to tell dockerfile to copy the CMD (or ENTRYPOINT) of the base image. Something like this:

FROM other:latest RUN my-extra-install CMD <use-the-CMD-from-base-image> 

I don't think there's any direct syntax for the CMD command to do what I want. I'm wondering if there's a workaround.

like image 552
Dave C Avatar asked Apr 05 '17 16:04

Dave C


People also ask

Can I have ENTRYPOINT and CMD in Dockerfile?

Introduction. There are CMD and ENTRYPOINT as setting items of Dockerfile, but these differences are often confusing for those new to Docker. This is because both of these items can execute the command you want to run on the container when the container is executed.

Can I use both ENTRYPOINT and CMD?

Both ENTRYPOINT and CMD allow you to specify the startup command for an image, but there are subtle differences between them. There are many times where you'll want to choose one or the other, but they can also be used together. We'll explore all these scenarios in the sections below.

What is the difference between CMD and ENTRYPOINT in Dockerfile?

The ENTRYPOINT instruction looks almost similar to the CMD instruction. However, the main highlighting difference between them is that it will not ignore any of the parameters that you have specified in the Docker run command (CLI parameters).

Does ENTRYPOINT override CMD in Docker?

ENTRYPOINT is the other instruction used to configure how the container will run. Just like with CMD, you need to specify a command and parameters. However, in the case of ENTRYPOINT we cannot override the ENTRYPOINT instruction by adding command-line parameters to the `docker run` command.


1 Answers

If you left it blank in your new Dockerfile, it will inherit the one from the base image.

For example:

base

FROM ubuntu CMD ["echo", "AAA"] 

layer1

FROM base 

If you build above images and run layer1 you will get the following:

$ sudo docker run -it layer1 AAA 
like image 155
Vor Avatar answered Sep 19 '22 14:09

Vor