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
264
votes
20 answers

Connect to mysql in a docker container from the host

In a nutshell I want to run mysql in a docker container and connect to it from my host. So far, the best I have achieved is: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) More details I'm…
gturri
  • 13,807
  • 9
  • 40
  • 57
254
votes
3 answers

What is the use of PYTHONUNBUFFERED in docker file?

I was watching a tutorial to dockerize my Django application. I did not understand why we use PYTHONUNBUFFERED as an environment variable in Dockerfile. Can anyone explain?
MayankBudhiraja
  • 2,763
  • 2
  • 7
  • 11
250
votes
2 answers

/bin/sh: apt-get: not found

I am trying to change a dockerFile to work with aspell. I have a bash script that I want to wrap in a dock Step 4: Wrap the script in a Docker container. The sample SDK we downloaded earlier contains an example of an action wrapped in a Docker…
Hüseyin Orkun Elmas
  • 2,613
  • 2
  • 9
  • 6
244
votes
6 answers

How can I use a variable inside a Dockerfile CMD?

Inside my Dockerfile: ENV PROJECTNAME mytestwebsite CMD ["django-admin", "startproject", "$PROJECTNAME"] Error: CommandError: '$PROJECTNAME' is not a valid project name What is the quickest workaround here? Does Docker have any plan to "fix" or…
david
  • 6,303
  • 16
  • 54
  • 91
241
votes
5 answers

Multiple FROMs - what it means

I want to build a docker image, which requires both a Neo4j database and Node.js to run. My first approach was to declare a base image for my image, containing Neo4j. The reference docs do not define "base image" in any helpful manner: Base…
ekkis
  • 9,804
  • 13
  • 55
  • 105
227
votes
2 answers

How do I set environment variables during the build in docker

I'm trying to set environment variables in docker container during the build but without success. Setting them when using run command works but I need to set them during the build. Dockerfile FROM ubuntu:latest ARG TEST_ENV=something Command I'm…
Michał Pietraszko
  • 5,666
  • 3
  • 21
  • 27
222
votes
11 answers

How do I use Docker environment variable in ENTRYPOINT array?

If I set an environment variable, say ENV ADDRESSEE=world, and I want to use it in the entry point script concatenated into a fixed string like: ENTRYPOINT ["./greeting", "--message", "Hello, world!"] with world being the value of the environment…
Psycho Punch
  • 6,418
  • 9
  • 53
  • 86
219
votes
3 answers

what is docker run -it flag?

I was doing some complex stuff with docker, but as turn out I don't know what -it flag means. Recently I've come across on some example of docker run command which has confused me a little. docker run -itd ubuntu:xenial /bin/bash My question is…
Alex
  • 3,923
  • 3
  • 25
  • 43
213
votes
8 answers

apt-get install tzdata noninteractive

When I try to apt-get install -y tzdata the command line option for picking timezone shows up. I am trying to use this in a script to do some setup, how can I make the apt-get run without user input? I know to reconfigure the tzdata I can do echo…
PYA
  • 8,096
  • 3
  • 21
  • 38
213
votes
9 answers

Conditional COPY/ADD in Dockerfile?

Inside of my Dockerfiles I would like to COPY a file into my image if it exists, the requirements.txt file for pip seems like a good candidate but how would this be achieved? COPY (requirements.txt if test -e requirements.txt; fi)…
derrend
  • 4,176
  • 4
  • 27
  • 40
210
votes
2 answers

What is .build-deps for apk add --virtual command?

What is .build-deps in the following command? I can't find an explanation in the Alpine docs. Is this a file that is predefined? Is see this referenced in many Dockerfiles. RUN apk add --no-cache --virtual .build-deps \ gcc \ freetype-dev…
gdbj
  • 16,102
  • 5
  • 35
  • 47
195
votes
4 answers

ARG or ENV, which one to use in this case?

This could be maybe a trivial question but reading docs for ARG and ENV doesn't put things clear to me. I am building a PHP-FPM container and I want to give the ability for enable/disable some extensions on user needs. Would be great if this could…
ReynierPM
  • 17,594
  • 53
  • 193
  • 363
191
votes
11 answers

How to update /etc/hosts file in Docker image during "docker build"

I want to update my /etc/hosts file during "docker build". I added below line in Dockerfile but it's neither updating /etc/hosts file nor giving any error. RUN echo "192.168.33.11 mynginx" >> /etc/hosts I need to update /etc/hosts. Can anyone…
Prakash
  • 2,532
  • 6
  • 17
  • 21
189
votes
2 answers

Alpine Dockerfile advantages of --no-cache vs. rm /var/cache/apk/*

When creating Dockerfiles using an Alpine image, I have often seen the use of either apk add --no-cache, or apk add followed by an rm /var/cache/apk/* statement. I am curious to know whether making use of the --no-cache flag eliminates the need to…
Angel S. Moreno
  • 3,469
  • 3
  • 29
  • 40
187
votes
8 answers

Dockerfile - set ENV to result of command

Is it possible to set a docker ENV variable to the result of a command? Like: ENV MY_VAR whoami i want MY_VAR to get the value "root" or whatever whoami returns
Sultanen
  • 3,084
  • 5
  • 25
  • 46