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
79
votes
19 answers

Docker-Compose file has yaml.scanner.ScannerError

compose.yml file, which looks like this: version: '2' services: discovery-microservice: build: discovery-microservice context: /discovery-microservice/target/docker dockerfile: Dockerfile ports: - "8761:8761" While I am…
Skeffington
  • 1,281
  • 2
  • 10
  • 13
78
votes
5 answers

Error response from daemon: Dockerfile parse error Unknown flag: mount

There is a previous question (Docker Unknown flag --mount) facing the same error that was due to having an out-of-date version of Docker running. I have an up-to-date version of Docker running. I have the following Dockerfile: FROM…
PMende
  • 5,171
  • 2
  • 19
  • 26
77
votes
4 answers

How do I run a Bash script in an Alpine Docker container?

I have a directory containing only two files, Dockerfile and sayhello.sh: . ├── Dockerfile └── sayhello.sh The Dockerfile reads FROM alpine COPY sayhello.sh sayhello.sh CMD ["sayhello.sh"] and sayhello.sh contains simply echo hello The Dockerfile…
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
77
votes
2 answers

/bin/sh: 1: apk: not found while creating docker image

I have the below Dockerfile for zookeeper and I am trying to create an image for it, but it is giving me an error. I have recently started working with Docker, and started playing with a Zookeeper setup, so I am not able to understand. What does…
john
  • 11,311
  • 40
  • 131
  • 251
77
votes
4 answers

How to ADD all files/directories except a hidden directory like .git in Dockerfile

One of things we do often is to package all source code in Dockerfile when we build a Docker image. ADD . /app How can we avoid including the .git directory in simple way ? I tried the Unix way of handling this using ADD [^.]* /app/ Complete…
Larry Cai
  • 55,923
  • 34
  • 110
  • 156
75
votes
11 answers

Wordpress Docker won't increase upload limit

I am trying to increate the upload limit of my Dockerized Wordpress instance to 150M. [filename] exceeds the maximum upload size for this site. I have created an uploads.ini file and attached it by volume to the instance. Yet I still can't get the…
75
votes
3 answers

How to add user with dockerfile?

How can I add a user with Dockerfile - the following does not work. USER vault WORKDIR /usr/local/bin/vault My full Dockerfile: FROM alpine:3.4 RUN apk update && apk add curl unzip RUN useradd -ms /bin/bash vault USER vault WORKDIR…
Atlantic0
  • 3,271
  • 5
  • 17
  • 24
74
votes
2 answers

Docker compose with name other than dockerfile

I have used docker to create CLI interfaces where I test my code. These are named reasonably as: proj_root/.../docks/foo.dockerfile proj_root/.../docks/bar.dockerfile Because there is more than one dock involved, the top level "Dockerfile" at the…
Chris
  • 28,822
  • 27
  • 83
  • 158
74
votes
6 answers

"docker build" requires exactly 1 argument(s)

I am trying to build an image from a specific Dockerfile, and tag it at the same time; I am following the online instructions fordocker build, but I get the following error: "docker build" requires exactly 1 argument(s) My directory…
Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341
73
votes
10 answers

alpine package py-pip missing

Im trying to install python pip in my alpine using Docker compose file but get the following error. ERROR: unsatisfiable constraints: py-pip (missing): required by: world[py-pip] ERROR: Service 'web' failed to build: The command '/bin/sh -c…
user1050619
  • 19,822
  • 85
  • 237
  • 413
71
votes
7 answers

How to run kubectl commands inside a container?

In a container inside a pod, how can I run a command using kubectl? For example, if i need to do something like this inside a container: kubectl get pods I have tried this : In my dockerfile, I have these commands : RUN curl -LO…
Dreams
  • 5,854
  • 9
  • 48
  • 71
71
votes
16 answers

Maven docker cache dependencies

I'm trying to use docker to automate maven builds. The project I want to build takes nearly 20 minutes to download all the dependencies, so I tried to build a docker image that would cache these dependencies, but it doesn't seem to save it. My…
Daniel Watrous
  • 3,467
  • 2
  • 36
  • 48
71
votes
8 answers

How to prevent Dockerfile caching git clone

I have a Dockerfile trying to package and deploy a web app to a container. The code of app fetches from git repository during Docker image building. Here's the Dockerfile snapshot: ........ RUN git clone --depth=1 git-repository-url $GIT_HOME/ RUN…
Raindy
  • 821
  • 1
  • 6
  • 6
71
votes
6 answers

Docker image error: "/bin/sh: 1: [python,: not found"

I'm building a new Docker image based on the standard Ubuntu 14.04 image. Here's my Dockerfile: FROM ubuntu:14.04 RUN apt-get update -y RUN apt-get install -y nginx git python-setuptools python-dev RUN easy_install pip ADD . /code WORKDIR /code RUN…
Joe Mornin
  • 8,766
  • 18
  • 57
  • 82
70
votes
11 answers

toomanyrequests: You have reached your pull rate limit. You may increase the limit by authenticating and upgrading

Why does this happen, when I want to build an image from a Dockerfile in CodeCommit with CodeBuild? I get this Error: toomanyrequests: You have reached your pull rate limit. You may increase the limit by authenticating and upgrading:…
vasil001
  • 2,501
  • 4
  • 8
  • 22