Questions tagged [dockerfile]

A Dockerfile is a file containing instructions to build a Docker image.

A image is a self-contained immutable package that typically contains everything required to run an application, including its library dependencies and the application source code or compiled binary. A Dockerfile is a reproducible recipe to built that image.

From the documentation:

Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.

Use this tag for questions about the Dockerfile syntax or semantics. This is a core part of and it will usually be correct to use this tag on Dockerfile questions as well.

Key Links

All links to official documentation on https://docs.docker.com/:

Example

This is a typical example Python application. The final application image is built on the Docker Hub python image but includes the library dependencies and the actual application code.

# Name the base image to be used for this image.
FROM python:3.11

# Install the application in its own directory.  Does not need
# to be an FHS standard directory.  Creates the directory if
# required.
WORKDIR /app

# Install the library dependencies.  Doing this first in a separate
# step makes rebuilds more efficient.
COPY requirements.txt ./
RUN pip install -r requirements.txt

# Copy the rest of the application code in.  (A compiled language
# would need to actually build it here.)
COPY ./ ./

# Document the port the application uses.  No practical effects
# beyond documentation, but considered good practice.
EXPOSE 8000

# The default command to use to run the main container process.
CMD ["./app.py"]

To create a Docker image from this Dockerfile, I issue this command within the same directory that contains it:

docker build --tag myapp .

Now I execute the application by running:

docker run -d -p 8000:8000 myapp

This launches the container in the background, running the image's CMD as the main container process. Host port 8000 is forwarded to container port 8000.

Note that this docker run invocation makes no reference to the host system. If one were to docker push the built image to a registry and docker run it on a remote host, since the entire application is included in the image, they can run it without separately downloading the code or Python runtime on to the host system.

15194 questions
130
votes
3 answers

Docker Copy and change owner

Given the following Dockerfile FROM ubuntu RUN groupadd mygroup RUN useradd -ms /bin/bash -G mygroup john MKDIR /data COPY test/ /data/test data RUN chown -R john:mygroup /data CMD /bin/bash In my test directory, which is copied I have set the file…
Christian Metzler
  • 2,971
  • 5
  • 24
  • 30
127
votes
2 answers

Share variable in multi-stage Dockerfile: ARG before FROM not substituted

I'm writing a multi-stage Dockerfile for the darshan utils: ARG DARSHAN_VER=3.1.6 FROM fedora:29 as build RUN dnf install -y \ gcc \ make \ bzip2 bzip2-devel zlib zlib-devel RUN curl -O…
Alberto Chiusole
  • 2,204
  • 2
  • 16
  • 26
127
votes
4 answers

Can we pass ENV variables through cmd line while building a docker image through dockerfile?

I am working on a task that involves building a docker image with centOs as its base using a Dockerfile . One of the steps inside the dockerfile needs http_proxy and https_proxy ENV variables to be set in order to work behind the proxy. As this…
Aniketh
  • 1,411
  • 3
  • 12
  • 15
126
votes
2 answers

Dockerfile: Setting multiple environment variables in single line

I was under the impression that environmental variables could be set on a single line as follows so as to minimize intermediary images. FROM alpine:3.6 ENV RUBY_MAJOR 2.4 \ RUBY_VERSION 2.4.1 \ RUBY_DOWNLOAD_SHA256…
BrianWilson
  • 1,653
  • 2
  • 12
  • 16
124
votes
8 answers

How to copy file from host to container using Dockerfile

I have written a Dockerfile which looks like this FROM ubuntu:12.04 RUN apt-get update RUN apt-get install -y wget Now I'm having a file called abc.txt in my host machine. How can I copy it to this container. Is there any step that I can add in…
Sasikiran Vaddi
  • 2,199
  • 4
  • 23
  • 29
121
votes
5 answers

Can a Dockerfile extend another one?

I have a Dockerfile for PHP like this : FROM php:7-fpm ENV DEBIAN_FRONTEND noninteractive RUN apt-get update && \ apt-get install -y git libicu-dev libmagickwand-dev libmcrypt-dev libcurl3-dev jpegoptim RUN pecl install imagick && \ …
Sylvain
  • 2,742
  • 5
  • 21
  • 34
121
votes
11 answers

Single file volume mounted as directory in Docker

Docker documentation says that it's possible to mount a single file into a Docker container: The -v flag can also be used to mount a single file - instead of just directories - from the host machine. $ docker run --rm -it -v…
TTT
  • 6,505
  • 10
  • 56
  • 82
120
votes
1 answer

Install packages in Alpine docker

How do I write Dockerfile commands to install the following in alpine docker image: software-properties-common openjdk-8-jdk python3 nltk Flask
Ankur100
  • 1,209
  • 2
  • 8
  • 7
120
votes
8 answers

Conditional ENV in Dockerfile

Is it possible to conditionally set an ENV variable in a Dockerfile based on the value of a build ARG? Ex: something like ARG BUILDVAR=sad ENV SOMEVAR=if $BUILDVAR -eq "SO"; then echo "hello"; else echo "world"; fi Update: current usage based on…
Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128
119
votes
22 answers

Can't create a docker image for COPY failed: stat /var/lib/docker/tmp/docker-builder error

I want to create a docker image. This is my work directory: Dockerfile.in test.json test.py And this is my Dockerfile: COPY ./test.json /home/test.json COPY ./test.py /home/test.py RUN python test.py When i launch this command: docker build…
EdoBen
  • 1,676
  • 2
  • 14
  • 24
119
votes
8 answers

ARG substitution in RUN command not working for Dockerfile

In my Dockerfile I have the following: ARG a-version RUN wget -q -O /tmp/alle.tar.gz http://someserver/server/$a-version/a-server-$a-version.tar.gz && \ mkdir /opt/apps/$a-version However when building this with: --build-arg…
user3139545
  • 6,882
  • 13
  • 44
  • 87
115
votes
1 answer

How to run multiple commands in one Github Actions Docker

What is the right way for running multiple commands in one action? For example: I want to run a python script as action. Before running this script I need to install the requirements.txt. I can think of several options: Create a Dockerfile with the…
baruchiro
  • 5,088
  • 5
  • 44
  • 66
115
votes
13 answers

"The headers or library files could not be found for jpeg" installing Pillow on Alpine Linux

I'm trying to run Python's Scrapy in a Docker container based on python:alpine. It was working before, but now I'd like to use Scrapy's Image Pipeline which requires me to install Pillow. As a simplified example, I tried the following…
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
114
votes
5 answers

Dockerfile: Output of RUN instruction into a Variable

I am writing a dockerfile and want to put the output of the "ls" command into a variable as shown below: $file = ls /tmp/dir Here, "dir" only has one file inside it. The following RUN instruction within a dockerfile is not working RUN $file = ls…
meallhour
  • 13,921
  • 21
  • 60
  • 117
113
votes
3 answers

Editing Files from dockerfile

I need to add several lines to /etc/sysctl.conf in a Docker image. Is there an idempotent way to do this via a Dockerfile rather than editing manually and using the docker commit approach?
Myles McDonnell
  • 12,943
  • 17
  • 66
  • 116