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
92
votes
2 answers

docker run pass arguments to entrypoint

I am able to pass the environment variables using the -e option. But i am not sure how to pass command line arguments to the jar in entrypoint using the docker run command. Dockerfile FROM openjdk ADD . /dir WORKDIR /dir COPY ./test-1.0.1.jar…
nad87563
  • 3,672
  • 7
  • 32
  • 54
92
votes
4 answers

What does minikube docker-env mean?

In the Kubernetes minikube tutorial there is this command to use Minikube Docker daemon : $ eval $(minikube docker-env) What exactly does this command do, that is, what exactly does minikube docker-env mean?
Satyajit Das
  • 2,740
  • 5
  • 16
  • 30
92
votes
1 answer

Docker multiline CMD or ENTRYPOINT

I have a really long command line for the default process due to a number of arguments. I think the easiest would be to create a script (for eg.run.sh) and then call this script in your ENTRYPOINT or CMD. I'm wondering if there is a way to make…
donnie
  • 2,981
  • 2
  • 16
  • 24
91
votes
6 answers

Docker command/option to display or list the build context

Is there a command/option to display or list the context which is sent to the Docker daemon for building an image? $ docker build -t "image-name" Sending build context to Docker daemon 8.499 MB ... Files and directories can be excluded from the…
Steve
  • 1,618
  • 3
  • 17
  • 30
91
votes
4 answers

How can I see Dockerfile for each docker image?

I have the following docker images. $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest 48b5124b2768 2 months ago 1.84 kB docker/whalesay …
Lone Learner
  • 18,088
  • 20
  • 102
  • 200
91
votes
5 answers

How to pass ARG value to ENTRYPOINT?

Docker 1.9 allows to pass arguments to a dockerfile. See link: https://docs.docker.com/engine/reference/builder/#arg How can i pass the same arugments within ENTRYPOINT Instruction?? My dockerfile has ARG $Version=3.1 ENTRYPOINT…
meallhour
  • 13,921
  • 21
  • 60
  • 117
90
votes
7 answers

Activate python virtualenv in Dockerfile

I have a Dockerfile where I tried to activate python virtualenv so that, it should install all dependencies within this env. However, everything still gets installed globally. I used different approaches and none of them worked. I am also not…
igsm
  • 1,280
  • 2
  • 17
  • 19
90
votes
12 answers

How to mount local volumes in docker machine

I am trying to use docker-machine with docker-compose. The file docker-compose.yml has definitions as follows: web: build: . command: ./run_web.sh volumes: - .:/app ports: - "8000:8000" links: - db:db - rabbitmq:rabbit …
jdcaballerov
  • 1,452
  • 1
  • 12
  • 16
88
votes
8 answers

Use docker run command to pass arguments to CMD in Dockerfile

I'm new to Docker and I'm having a hard time to setup the docker container as I want. I have a nodejs app can take two parameters when start. For example, I can use node server.js 0 dev or node server.js 1 prod to switch between production mode and…
Jaaaaaaay
  • 1,895
  • 2
  • 15
  • 24
87
votes
1 answer

How does the new Docker --squash work

In Docker 1.13 the new --squash parameter was added. I'm now hoping to reduce the size of my images as well as being able to "hide" secret files I have in my layers. Below you can now see the difference from doing a build with and without the…
Fore
  • 5,726
  • 7
  • 22
  • 35
86
votes
8 answers

rebuild docker image from specific step

I have the below Dockerfile. FROM ubuntu:14.04 MAINTAINER Samuel Alexander RUN apt-get -y install software-properties-common RUN apt-get -y update # Install Java. RUN echo oracle-java8-installer…
sag
  • 5,333
  • 8
  • 54
  • 91
84
votes
14 answers

Why won't my docker-entrypoint.sh execute?

My ENTRYPOINT script doesn't execute and throws standard_init_linux.go:175: exec user process caused "no such file or directory". Why so? Doesn't Work $ docker build -t gilani/trollo . && docker run gilani/trollo Sending build context to Docker…
Amin Shah Gilani
  • 8,675
  • 5
  • 37
  • 79
83
votes
2 answers

Execute command on host during docker build

Is it possible to create Dockerfile that executes a command on host when image is being build? Now I'm doing: ./script_that_creates_magic_file.sh docker build . with Dockerfile: FROM alpine COPY magic_file I want to be able to do: docker build…
82
votes
9 answers

Docker create network should ignore existing network

My docker containers are running in a local network, called my_local_network. To assure the network exists, every build script starts with: docker network create --driver bridge my_local_network This works fine. If the network does not exist, it is…
Anna Ira Hurnaus
  • 1,680
  • 3
  • 19
  • 29
81
votes
3 answers

Multiple commands on docker ENTRYPOINT

I'm trying to build a custom tcserver docker image. But I'm having some problems starting the webserver and the tomcat. As far as I understand I should use ENTRYPOINT to run the commands I want. The question is, is it possible to run multiple…
radicaled
  • 2,369
  • 5
  • 30
  • 44