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
186
votes
18 answers

Docker-compose check if mysql connection is ready

I am trying to make sure that my app container does not run migrations / start until the db container is started and READY TO accept connections. So I decided to use the healthcheck and depends on option in docker compose file v2. In the app, I…
John Kariuki
  • 4,966
  • 5
  • 21
  • 30
185
votes
2 answers

Docker images - types. Slim vs slim-stretch vs stretch vs alpine

I am looking to pick up a docker image to build a java app and looking at the variants of the OpenJDK images available. I am looking here https://github.com/docker-library/openjdk/tree/master/8/jdk and see alpine, slim and windows. What are the…
mailtobash
  • 2,310
  • 3
  • 15
  • 17
179
votes
8 answers

Deploying a minimal flask app in docker - server connection issues

I have an app whose only dependency is flask, which runs fine outside docker and binds to the default port 5000. Here is the full source: from flask import Flask app = Flask(__name__) app.debug = True @app.route('/') def main(): return…
Dreen
  • 6,976
  • 11
  • 47
  • 69
176
votes
14 answers

npm ERR! Tracker "idealTree" already exists while creating the Docker image for Node project

I have created one node.js project called simpleWeb. The project contains package.json and index.js. index.js const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('How are you…
Jaypal Sodha
  • 2,238
  • 2
  • 10
  • 22
174
votes
3 answers

Dockerfile build - possible to ignore error?

I've got a Dockerfile. When building the image, the build fails on this error: automake: error: no 'Makefile.am' found for any configure output Error build: The command [/bin/sh -c aclocal && autoconf && automake -a] returned a non-zero code:…
Oliver
  • 3,981
  • 2
  • 21
  • 35
171
votes
6 answers

Run a script in Dockerfile

I'm trying to run a script during my building process in my Dockerfile, but it doesn't seems to work. I tried that way: FROM php:7-fpm ADD bootstrap.sh / ENTRYPOINT ["/bin/bash", "/bootstrap.sh"] Also this way: FROM php:7-fpm ADD bootstrap.sh…
Kevin
  • 4,823
  • 6
  • 36
  • 70
163
votes
4 answers

docker-compose, run a script after container has started?

I have a service that I am bringing up through Rancher via docker-compose. The issue I am running into is that I need to set a password after the container has been deployed. The way rancher secrets work, is that I set my secret in and rancher will…
Blooze
  • 1,987
  • 4
  • 16
  • 19
161
votes
7 answers

Docker build gives "unable to prepare context: context must be a directory: /Users/tempUser/git/docker/Dockerfile"

I have a Dockerfile that is supposed to build an Ubuntu image. But whenever I run docker build -t ubuntu-test:latest ./Dockerfile it shows the following error on the console unable to prepare context: context must be a directory:…
Damien-Amen
  • 7,232
  • 12
  • 46
  • 75
154
votes
3 answers

Docker how to run pip requirements.txt only if there was a change?

In a Dockerfile I have a layer which installs requirements.txt: FROM python:2.7 RUN pip install -r requirements.txt When I build the docker image it runs the whole process regardless of any changes made to this file. How do I make sure Docker only…
Prometheus
  • 32,405
  • 54
  • 166
  • 302
153
votes
5 answers

How to cache the RUN npm install instruction when docker build a Dockerfile

I am currently developing a Node backend for my application. When dockerizing it (docker build .) the longest phase is the RUN npm install. The RUN npm install instruction runs on every small server code change, which impedes productivity through…
ohadgk
  • 3,463
  • 3
  • 11
  • 10
153
votes
14 answers

Docker follow symlink outside context

Yet another Docker symlink question. I have a bunch of files that I want to copy over to all my Docker builds. My dir structure is: parent_dir - common_files - file.txt - dir1 - Dockerfile - symlink ->…
Ravi
  • 3,719
  • 6
  • 28
  • 40
151
votes
27 answers

Failed to solve with frontend Dockerfile

I am pretty new to Docker and am trying to build a Docker image with plain HTML, but I have this error message, saying failed to solve with frontend dockerfile.v0: failed to read dockerfile: open…
helloWORLD
  • 1,681
  • 2
  • 5
  • 6
149
votes
9 answers

Is there a way to combine Docker images into 1 container?

I have a few Dockerfiles right now. One is for Cassandra 3.5, and it is FROM cassandra:3.5 I also have a Dockerfile for Kafka, but t is quite a bit more complex. It is FROM java:openjdk-8-fre and it runs a long command to install Kafka and…
David
  • 7,028
  • 10
  • 48
  • 95
149
votes
10 answers

How to copy folders to docker image from Dockerfile?

I tried the following command in my Dockerfile: COPY * / and got mighty surprised at the result. Seems the naive docker code traverses the directories from the glob and then dumps the each file in the target directory while respectfully ignoring my…
jonalv
  • 5,706
  • 9
  • 45
  • 64
147
votes
5 answers

What is the difference between `docker-compose build` and `docker build`?

What is the difference between docker-compose build and docker build? Suppose in a dockerized project path there is a docker-compose.yml file: docker-compose build And docker build
Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150