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
2977
votes
16 answers

What is the difference between the 'COPY' and 'ADD' commands in a Dockerfile?

What is the difference between the COPY and ADD commands in a Dockerfile, and when would I use one over the other? COPY The COPY instruction will copy new files from and add them to the container's filesystem at path ADD…
Steve
  • 53,375
  • 33
  • 96
  • 141
1309
votes
21 answers

How do I pass environment variables to Docker containers?

How can one access an external database from a container? Is the best way to hard code in the connection string? # Dockerfile ENV DATABASE_URL amazon:rds/connection?string
AJcodez
  • 31,780
  • 20
  • 84
  • 118
795
votes
69 answers

denied: requested access to the resource is denied: docker

I am following this link to create my first docker Image and it went successfully and now I am trying to push this Image into my docker repository from this link. But whenever I am trying to push this Image into repository, I got this type of…
Keyur Shah
  • 11,043
  • 4
  • 29
  • 48
650
votes
10 answers

Difference between RUN and CMD in a Dockerfile

I'm confused about when should I use CMD vs RUN. For example, to execute bash/shell commands (i.e. ls -la) I would always use CMD or is there a situation where I would use RUN? Trying to understand the best practices about these two similar…
TakeSoUp
  • 7,457
  • 7
  • 16
  • 20
623
votes
12 answers

What's the difference between Docker Compose vs. Dockerfile

I have been reading up and learning about Docker, and am trying to correctly choose the Django setup to use. So far there is either: Docker Compose or Dockerfile I understand that Dockerfiles are used in Docker Compose, but I am not sure if it is…
613
votes
7 answers

How do I make a comment in a Dockerfile?

I am writing a Dockerfile. Is there a way to make comments in this file? Does Docker have a comment option that takes the rest of a line and ignores it?
kpie
  • 9,588
  • 5
  • 28
  • 50
600
votes
6 answers

How to set image name in Dockerfile?

You can set image name when building a custom image, like this: docker build -t dude/man:v2 . # Will be named dude/man:v2 Is there a way to define the name of the image in Dockerfile, so I don't have to mention it in the docker build command?
gvlasov
  • 18,638
  • 21
  • 74
  • 110
563
votes
7 answers

COPY with docker but with exclusion

In a Dockerfile, I have COPY . . I want to exclude an entire directory, in my case, node_modules directory. Something like this: COPY [all but **/node_modules/**] . Is this possible with Docker?
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
519
votes
5 answers

Dockerfile copy keep subdirectory structure

I'm trying to copy a number of files and folders to a docker image build from my localhost. The files are like this: folder1/ file1 file2 folder2/ file1 file2 I'm trying to make the copy like this: COPY files/* /files/ However, all…
user1220022
  • 11,167
  • 19
  • 41
  • 57
512
votes
10 answers

How to add users to Docker container?

I have a docker container with some processes (uwsgi and celery) running inside. I want to create a celery user and a uwsgi user for these processes as well as a worker group that they will both belong to, in order to assign permissions. I tried…
rfj001
  • 7,948
  • 8
  • 30
  • 48
484
votes
6 answers

How to copy multiple files in one layer using a Dockerfile?

The following Dockerfile contains four COPY layers: COPY README.md ./ COPY package.json ./ COPY gulpfile.js ./ COPY __BUILD_NUMBER ./ How to copy these files using one layer instead? The following was tried: COPY [ "__BUILD_NUMBER ./", …
kazhuravlev
  • 5,385
  • 3
  • 15
  • 8
438
votes
4 answers

Difference between links and depends_on in docker_compose.yml

According to the Docker Compose's compose-file documentation: depends_on - Express dependency between services. links - Link to containers in another service and also express dependency between services in the same way as depends_on. I don't…
itsjef
  • 4,659
  • 3
  • 13
  • 12
425
votes
7 answers

Docker: How to use bash with an Alpine based docker image?

I created a docker image from openjdk:8-jdk-alpine and I want to use bash, rather than sh as my shell, however when I try to execute simple commands I get the following errors: RUN bash /bin/sh: bash: not found RUN ./gradlew build env: can't…
iamdeit
  • 5,485
  • 4
  • 26
  • 40
397
votes
6 answers

Change directory command in Docker?

In docker I want to do this: git clone XYZ cd XYZ make XYZ However because there is no cd command, I have to pass in the full path everytime (make XYZ /fullpath). Any good solutions for this?
RParadox
  • 6,393
  • 4
  • 23
  • 33
375
votes
57 answers

An error, "failed to solve with frontend dockerfile.v0"

I was trying to build my Docker image for my Gatsby application. Whenever I run the command docker build . -t gatsbyapp, it gives me an error: failed to solve with frontend dockerfile.v0: failed to build LLB: failed to compute cache key: "/.env" not…
Muhammad Yasir
  • 3,814
  • 2
  • 5
  • 8
1
2 3
99 100