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
145
votes
5 answers

Docker - image operating system "windows" cannot be used on this platform

I tried this on my Windows 10 machine: Dockerfile: From microsoft/nanoserver CMD ["echo", "Hello World"] PS C:\FSD\Docker\Trial1> docker build -t lea/leatest . Sending build context to Docker daemon 2.048kB Step 1/2 : FROM…
Lea A
  • 1,511
  • 2
  • 10
  • 9
145
votes
7 answers

How to write commands with multiple lines in Dockerfile while preserving the new lines?

I want to write the following RUN command in the Dockerfile. But, docker is not preserving the new lines. RUN echo "[repo] \ name = YUM Repository \ baseurl = https://example.com/packages/ \ enabled = 1 \ gpgcheck =…
Venkata Jaswanth
  • 1,661
  • 2
  • 11
  • 12
144
votes
4 answers

E: Package 'mysql-client' has no installation candidate in php-fpm image build using docker compose

Im fairly new to docker and so im trying to learn more about it using a laravel project, im following this tutorial: https://www.digitalocean.com/community/tutorials/how-to-set-up-laravel-nginx-and-mysql-with-docker-compose Ive adjusted the…
surgiie
  • 3,947
  • 3
  • 12
  • 11
143
votes
1 answer

Docker expose all ports or range of ports from 7000 to 8000

Can I specify a port range in a Dockerfile EXPOSE 7000-8000 and when running the container bind all these exposed ports to the same ports on the host machine? docker run -p 7000-8000:7000-8000
DarVar
  • 16,882
  • 29
  • 97
  • 146
142
votes
7 answers

How to pass environment variable to docker-compose up

I am trying to run a container. I already have the image uploaded to private Docker registry. I want to write a compose file to download and deploy the image. But I want to pass the TAG name as a variable from the docker-compose run command.My…
Abhi.G
  • 1,801
  • 5
  • 20
  • 35
141
votes
10 answers

npm WARN old lockfile The package-lock.json file was created with an old version of npm

I have a dockerfile as shown below, and when i execute it, I get a warning that i can't figure out in the RUN npm ci step: npm WARN old lockfile The package-lock.json file was created with an old version of npm I tried npm install instead of npm…
semural
  • 3,583
  • 8
  • 37
  • 67
141
votes
7 answers

Docker: Using --password via the CLI is insecure. Use --password-stdin

I have the following warning when I log in to my registry during a continuous integration (CI) process: WARNING! Using --password via the CLI is insecure. Use --password-stdin. Should I just replace --password with --password-stdin?
Dimitri Kopriwa
  • 13,139
  • 27
  • 98
  • 204
140
votes
4 answers

Rebuild Docker container on file changes

For running an ASP.NET Core application, I generated a dockerfile which build the application and copys the source code in the container, which is fetched by Git using Jenkins. So in my workspace, I do the following in the dockerfile: WORKDIR…
Lion
  • 16,606
  • 23
  • 86
  • 148
140
votes
8 answers

How to correctly link php-fpm and Nginx Docker containers?

I am trying to link 2 separate containers: nginx:latest php:fpm The problem is that php scripts do not work. Perhaps the php-fpm configuration is incorrect. Here is the source code, which is in my repository. Here is the file…
Victor Bocharsky
  • 11,930
  • 13
  • 58
  • 91
138
votes
5 answers

How to remove entrypoint from parent Image on Dockerfile

I want to remove entrypoint from Dockerfile, but parent image has a entrypoint. how do I can remove it?
fsword
  • 1,505
  • 2
  • 10
  • 8
138
votes
9 answers

How can I set Bash aliases for docker containers in Dockerfile?

I am new to Docker. I found that we can set environment variables using the ENV instruction in the Dockerfile. But how does one set Bash aliases for long commands in Dockerfile?
np20
  • 1,935
  • 3
  • 16
  • 24
137
votes
4 answers

Docker: What is the default WORKDIR in a Dockerfile?

We can use WORKDIR to set a directory as work directory in Dockerfile, but what's the default value if we don't set?
Freewind
  • 193,756
  • 157
  • 432
  • 708
135
votes
1 answer

How can I use a local file on container?

I'm trying create a container to run a program. I'm using a pre configurate image and now I need run the program. However, it's a machine learning program and I need a dataset from my computer to run. The file is too large to be copied to the…
zanini
  • 1,463
  • 2
  • 10
  • 9
135
votes
4 answers

Difference between Docker ENTRYPOINT and Kubernetes container spec COMMAND?

Dockerfile has a parameter for ENTRYPOINT and while writing Kubernetes deployment YAML file, there is a parameter in Container spec for COMMAND. I am not able to figure out what's the difference and how each is used?
tusharfloyd
  • 1,732
  • 3
  • 14
  • 18
133
votes
4 answers

How to view logs for a docker image?

In the docker world, one can easily see logs for docker container (that is, a running image). But during image creation, one usually issues multiple commands. For example npm install commands in node projects. It would be beneficial to see logs for…
Ville Miekk-oja
  • 18,749
  • 32
  • 70
  • 106