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
70
votes
4 answers

Operation of the mkdir command with dockerfile

I cannot create a directory with the mkdir command in a container with dockerfile. My Dockerfile file is simply ; FROM php:fpm WORKDIR /var/www/html VOLUME ./code:/var/www/html RUN mkdir -p /var/www/html/foo In this way I created a simple php:…
Spartan Troy
  • 949
  • 2
  • 9
  • 13
70
votes
3 answers

Should I minimize the number of docker layers?

The documentation doesn't elaborate on the topic a lot. It says: Minimize the number of layers Prior to Docker 17.05, and even more, prior to Docker 1.10, it was important to minimize the number of layers in your image. The following improvements…
gukoff
  • 2,112
  • 3
  • 18
  • 30
70
votes
8 answers

entrypoint file not found

I have a docker image with this command: FROM ruby:2.4-alpine WORKDIR /usr/src/app COPY Gemfile /usr/src/app/Gemfile COPY Gemfile.lock /usr/src/app/Gemfile.lock RUN bundle config build.nokogiri --use-system-libraries RUN bundle install --without…
Luiz E.
  • 6,769
  • 10
  • 58
  • 98
70
votes
2 answers

Can we mount sub-directories of a named volume in docker?

The docker-compose file https://docs.docker.com/compose/compose-file/#/volumes-volume-driver shows various ways to mount host sub-directories relative to the compose file. For example: volumes: # Just specify a path and let the Engine create a…
nPn
  • 16,254
  • 9
  • 35
  • 58
69
votes
2 answers

Docker build pull access denied, repository does not exist or may require

I am trying to generate a Docker Image without using Visual Studio. I am in the project folder and I execute from windows 10 admin command line docker build . I can't figure out how to make this work. [+] Building 1.3s (8/9) => [internal] load…
greektreat
  • 2,329
  • 3
  • 30
  • 53
69
votes
3 answers

How are Packer and Docker different? Which one should I prefer when provisioning images?

How are Packer and Docker different? Which one is easier/quickest to provision/maintain and why? What is the pros and cons of having a dockerfile?
Abhineetraj
  • 769
  • 1
  • 5
  • 9
69
votes
6 answers

Is it possible to show the `WORKDIR` when building a docker image?

We have a problem with the WORKDIR when we building a docker image. Is it possible to print the value of WORKDIR? We tried: ECHO ${WORKDIR} But there is no such instruction ECHO
Freewind
  • 193,756
  • 157
  • 432
  • 708
68
votes
2 answers

Does putting ARG at top of Dockerfile prevent layer re-use?

If an ARG that is declared at the top of a Dockerfile gets changed, but its value is only used for a RUN command near the end of the Dockerfile, does Docker rebuild the whole image from scratch or is it able to re-use the intermediate image from…
Woodrow Barlow
  • 8,477
  • 3
  • 48
  • 86
68
votes
3 answers

Difference between 'image' and 'build' within docker compose

Please help me understand the difference between 'image' and 'build' within docker compose
meallhour
  • 13,921
  • 21
  • 60
  • 117
67
votes
6 answers

Dockerfile CMD instruction will exit the container just after running it

I want to setup some configuration when my container starts, for this I am using shell scripts. But my container will exits as soon as my scripts ends, I have tried with -d flag / detached mode but It will never run in detached mode. Below is my…
Anand Suthar
  • 3,678
  • 2
  • 30
  • 52
66
votes
7 answers

Externalising Spring Boot properties when deploying to Docker

In my Spring Boot app I want to externalise the properties to run in a Docker container. When first deployed, the properties that are currently in my-server/src/main/resources/application.yml are loaded and used by the application as expected. All…
MeanwhileInHell
  • 6,780
  • 17
  • 57
  • 106
66
votes
3 answers

Reuse inherited image's CMD or ENTRYPOINT

How can I include my own shell script CMD on container start/restart/attach, without removing the CMD used by an inherited image? I am using this, which does execute my script fine, but appears to overwrite the PHP CMD: FROM php COPY start.sh…
Johnathan Elmore
  • 2,156
  • 1
  • 19
  • 25
66
votes
5 answers

Docker - What is proper way to rebuild and push updated image to docker cloud?

What I'm currently doing: Dockerfile: FROM python:3.5.1 ENV PYTHONUNBUFFERED 1 RUN mkdir /www WORKDIR /www ADD deps.txt /www/ RUN pip3 install -r deps.txt ADD . /www/ RUN chmod 0755 /www/docker-init.sh Build command: docker build -t…
Alex T
  • 4,331
  • 3
  • 29
  • 47
65
votes
4 answers

How to give folder permissions inside a docker container Folder

I am creating a folder inside my Dockerfile and I want to give it a write permission. But I am getting permission denied error when I try to do it FROM python:2.7 RUN pip install Flask==0.11.1 RUN useradd -ms /bin/bash admin USER admin COPY app…
Shivanand T
  • 1,093
  • 1
  • 10
  • 18
65
votes
6 answers

Hidden file .env not copied using Docker COPY

I have a Dockerfile and there is a syntax like this COPY ["Gemfile", "Gemfile.lock", "Procfile", ".env", "/huginn/"] I use RUN /bin/bash -l -c "ls -a" to check file cope status, I find .env file doen't be copied to the image. I change the .env file…
Hsiu Chuan Tsao
  • 1,396
  • 1
  • 12
  • 23