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
56
votes
9 answers

How to install Go in alpine linux

I am trying to install Go inside an Alpine Docker image. For that I downloaded tar file from here inside my alpine docker image, untar it using following command: tar -C /usr/local -xzf go1.10.3.linux-amd64.tar.gz exported PATH to have go binary…
Yogesh Jilhawar
  • 5,605
  • 8
  • 44
  • 59
56
votes
10 answers

Rails server is still running in a new opened docker container

I want to deploy my rails project using Docker. So I use Docker-Compose. But I get one weird error message. When run docker-compose up(this contains db-container with postgresql, redis and web container with rails) I get a web_1 | => Booting…
handkock
  • 1,067
  • 2
  • 11
  • 23
56
votes
4 answers

Dockerfile strategies for Git

What is the best strategy to clone a private Git repository into a Docker container using a Dockerfile? Pros/Cons? I know that I can add commands on Dockerfile in order to clone my private repository into a docker container. But I would like to…
Hemerson Varela
  • 24,034
  • 16
  • 68
  • 69
55
votes
2 answers

How do we see full commands in the output of docker history command?

How do we see full commands in the third column (CREATED BY) in the output of docker history command? $ docker history docker.company.net/docker-base-images/image:1.0 IMAGE CREATED CREATED BY …
user674669
  • 10,681
  • 15
  • 72
  • 105
55
votes
3 answers

Docker container doesn't expose ports when --net=host is mentioned in the docker run command

I have a CentOS docker container on a CentOS docker host. When I use this command to run the docker image docker run -d --net=host -p 8777:8777 ceilometer:1.x the docker container get host's IP but doesn't have ports assigned to it. If I run the…
arevur
  • 553
  • 1
  • 4
  • 7
55
votes
6 answers

How to build a docker container for a Java application

What I want to do is build a docker image for my Java application but the following considerations should be true for most compiled languages. problem On my build server I want to produce a docker image for my application as the deliverable. For…
Tobias Kremer
  • 1,531
  • 2
  • 15
  • 21
54
votes
3 answers

Is it possible to cache multi-stage docker builds?

I recently switched to multi-stage docker builds, and it doesn't appear that there's any caching on intermediate builds. I'm not sure if this is a docker limitation, something which just isn't available or whether I'm doing something wrong. I am…
Matthew Goslett
  • 1,127
  • 1
  • 7
  • 6
53
votes
6 answers

How to start apache2 automatically in a ubuntu docker container?

I am trying to create a Dockerfile that will start apache automatically. Nothing has worked. But If I log into the container and run service apache2 start it works. Why can I not run that command from my Dockerfile? FROM ubuntu # File Author /…
Rayhan Muktader
  • 2,038
  • 2
  • 15
  • 32
53
votes
3 answers

How can I overwrite a file through Dockerfile in docker container?

I have to overwrite a file through Dockerfile. In particular on an Ubuntu container with Apache and PHP and I have to overwrite the file php5-cgi.conf. I tried to use the following command: COPY php5-cgi.conf…
Ciro Caiazzo
  • 531
  • 1
  • 4
  • 3
53
votes
2 answers

Docker-compose volume mount before run

I have a Dockerfile I'm pointing at from a docker-compose.yml. I'd like the volume mount in the docker-compose.yml to happen before the RUN in the Dockerfile. Dockerfile: FROM node WORKDIR /usr/src/app RUN npm install --global gulp-cli \ && npm…
DanielM
  • 6,380
  • 2
  • 38
  • 57
52
votes
3 answers

Docker parallel operations limit

Is there a limit to the number of parallel Docker push/pulls you can do? E.g. if you thread Docker pull / push commands such that they are pulling/pushing different images at the same time what would be the upper limit to the number of…
rmoh21
  • 1,405
  • 6
  • 19
  • 36
52
votes
3 answers

Build postgres docker container with initial schema

I'm looking to build dockerfiles that represent company databases that already exist. Similarly, I'd like create a docker file that starts by restoring a psql dump. I have my psql_dump.sql in the . directory. FROM postgres ADD . /init_data run…
Jono
  • 3,393
  • 6
  • 33
  • 48
52
votes
12 answers

Error: "error creating aufs mount to" when building dockerfile

I get this error when I try to build a docker file error creating aufs mount to /var/lib/docker/aufs/mnt /6c1b42ce1a98b1c0f2d2a7f17c196221445f1054566065d4c607e4f1b99930eb-init: invalid argument What does it mean? How do I fix it?
lars
  • 1,976
  • 5
  • 33
  • 47
51
votes
10 answers

Cache Rust dependencies with Docker build

I have hello world web project in Rust + Actix-web. I have several problems. First is every change in code causes recompiling whole project including downloading and compiling every crate. I'd like to work like in normal development - it means cache…
Arek C.
  • 1,271
  • 2
  • 9
  • 17
51
votes
4 answers

Install python package in docker file

In my docker file, I want to install med2image python package (https://github.com/FNNDSC/med2image). I use the following code: FROM ubuntu:16.04 RUN apt-get update && apt-get install -y --no-install-recommends \ python3.5 \ python3-pip \ …
Mehdi
  • 1,146
  • 1
  • 14
  • 27