Answers: <p>You can use a <code>Makefile</code> for this:</p>
  
  <pre><code>PHPBB_VERSION := 3.2.4
  
  build:
      docker build \
          --build-arg PHPBB_VERSION=${PHPBB_VERSION} \
          --tag phpbb:${PHPBB_VERSION} - &lt; Dockerfile
  </code></pre>
  
  <p><code>Dockerfile</code>:</p>
  
  <pre><code>FROM php:7.2-cli                                                                           
  
  ARG PHPBB_VERSION                                                                          
  
  RUN wget -nv -O- https://github.com/phpbb/phpbb/archive/release-${PHPBB_VERSION}.tar.gz | \
      tar xzf - --strip-components=1                                                         
  </code></pre>
  
  <p>Then, in a shell: <code>make build</code>.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-11-06T10:09:28.960Z
Answers: 2019-11-06T10:09:28.960Z
Answers: Hedi Nasr
Answers: <p>Why would you want to pass ARG as docker tag? Obviously it will at least be two times, first time with --build-arg and second time with --tag. So what you can do is that run this command inside a shell script so that you don't type this command again and again and just change value of $PHPBB_VETSION and run shell script. </p>
  
Answers: CC BY-SA 4.0
Answers: 2019-02-08T19:08:41.143Z
Answers: 2019-02-08T19:08:41.143Z
Answers: deosha
Body: <p>I use and argument (<code>ARG</code>) in my Dockerfile to download a particular version of an application. I would like to tag my image with that version number. I end up writing something like that:</p>
  
  <pre><code>sh$ PHPBB_VERSION=3.2.4 &amp;&amp;
      sudo docker build --build-arg PHPBB_VERSION=${PHPBB_VERSION} \
                        --tag phpbb:${PHPBB_VERSION} - &lt; dockerfiles/bootstrap
  </code></pre>
  
  <p>But I don't find that very elegant since you have to type the <em>word</em> <code>PHPBB_VERSION</code> no less than four times in this command, with all the possible errors introduced by a typo.</p>
  
  <p><strong>So, is there a more elegant solution to tag an image with the value of a Dockerfile argument?</strong></p>
  
  <hr>
  
  <p>FWIW, here is the relevant part of my Dockerfile:</p>
  
  <pre><code>sh$ cat dockerfiles/bootstrap
  FROM php:7.2-cli
  
  ARG PHPBB_VERSION
  RUN echo "You are building phpBB version ${PHPBB_VERSION:?undefined}"
  ...
  RUN wget -nv -O- \
        https://github.com/phpbb/phpbb/archive/release-${PHPBB_VERSION}.tar.gz | \
      tar xzf - --strip-components=1
  </code></pre>
  
Comments: CC BY-SA 4.0
Comments: 2019-03-12T17:33:43.583Z
Comments: Have you considered using a CI tool like Jenkins to build and tag images? Parameters can easily be set to a job and the process will be much more elegant than your current approach.
Comments: KatariaA
Comments: CC BY-SA 4.0
Comments: 2019-11-06T23:06:23.493Z
Comments: FYI you can omit the && by passing -e to sudo. A good approach would be to create a shell script taking the version as argument
Comments: Tensibai
Comments: CC BY-SA 4.0
Comments: 2019-11-07T00:46:47.450Z
Comments: Thanks for the comment. Actually, since I asked the question I chose to use a Makefile-based solution somewhat along the lines of the current top answer. A previous suggestion was to use a CI tool. But I must admit I lack knowledge in that area. Wouldn't that use a sledgehammer to kill a fly?
Comments: Sylvain Leroux
Tags: <automation><dockerfile>
Title: Is there an elegant way to use a Dockerfile argument to tag an image?
UserDisplayName: Sylvain Leroux
ThreadText: # Is there an elegant way to use a Dockerfile argument to tag an image?
  
  (Asked by: username_0 on 2019-02-08)
  
  I use and argument (`ARG`) in my Dockerfile to download a particular version of an application. I would like to tag my image with that version number. I end up writing something like that:
  
  ```
  sh$ PHPBB_VERSION=3.2.4 &&
      sudo docker build --build-arg PHPBB_VERSION=${PHPBB_VERSION} \
                        --tag phpbb:${PHPBB_VERSION} - < dockerfiles/bootstrap
  
  ```
  
  But I don't find that very elegant since you have to type the *word* `PHPBB_VERSION` no less than four times in this command, with all the possible errors introduced by a typo.
  
  **So, is there a more elegant solution to tag an image with the value of a Dockerfile argument?**
  
  ---
  
  FWIW, here is the relevant part of my Dockerfile:
  
  ```
  sh$ cat dockerfiles/bootstrap
  FROM php:7.2-cli
  
  ARG PHPBB_VERSION
  RUN echo "You are building phpBB version ${PHPBB_VERSION:?undefined}"
  ...
  RUN wget -nv -O- \
        https://github.com/phpbb/phpbb/archive/release-${PHPBB_VERSION}.tar.gz | \
      tar xzf - --strip-components=1
  
  ```
  
  **Tags:** automation, dockerfile
  
  > **Comments:**
  > **username_1**: Have you considered using a CI tool like Jenkins to build and tag images? Parameters can easily be set to a job and the process will be much more elegant than your current approach.  
  > **username_2**: FYI you can omit the && by passing -e to sudo. A good approach would be to create a shell script taking the version as argument  
  > **username_0**: Thanks for the comment. Actually, since I asked the question I chose to use a Makefile-based solution somewhat along the lines of the current top answer. A previous suggestion was to use a CI tool. But I must admit I lack knowledge in that area. Wouldn't that use a sledgehammer to kill a fly?  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2019-11-06. Score: 2
  
  You can use a `Makefile` for this:
  
  ```
  PHPBB_VERSION := 3.2.4
  
  build:
      docker build \
          --build-arg PHPBB_VERSION=${PHPBB_VERSION} \
          --tag phpbb:${PHPBB_VERSION} - < Dockerfile
  
  ```
  
  `Dockerfile`:
  
  ```
  FROM php:7.2-cli                                                                           
  
  ARG PHPBB_VERSION                                                                          
  
  RUN wget -nv -O- https://github.com/phpbb/phpbb/archive/release-${PHPBB_VERSION}.tar.gz | \
      tar xzf - --strip-components=1                                                         
  
  ```
  
  Then, in a shell: `make build`.
  
  
  ---
  
  ### Answer by username_4 on 2019-02-08. Score: 0
  
  Why would you want to pass ARG as docker tag? Obviously it will at least be two times, first time with --build-arg and second time with --tag. So what you can do is that run this command inside a shell script so that you don't type this command again and again and just change value of $PHPBB\_VETSION and run shell script.
  
  
  ---
  
  