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

Copy multiple directories with one command

Is there any way to copy multiple directories in one command, to reduce the number of layers? E.g., instead of: COPY dirone ./dirone COPY dirtwo ./dirtwo COPY dirthree ./dirthree I want to do: COPY dirone/ dirtwo/ dirthree/ ./ However, this copies…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
109
votes
5 answers

How to pass arguments within docker-compose?

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 arguments within docker-compose.yml? Please provide an example too, if possible.
meallhour
  • 13,921
  • 21
  • 60
  • 117
108
votes
5 answers

Installing GD in Docker

I am a complete Docker novice but am having to maintain an existing system. The Dockerfile I am using is as below: FROM php:5.6-apache RUN docker-php-ext-install mysql mysqli RUN apt-get update -y && apt-get install -y sendmail RUN apt-get update…
evilscary
  • 2,097
  • 5
  • 23
  • 33
107
votes
11 answers

Docker filling up storage on macOS

(Post created on Oct 05 '16) I noticed that every time I run an image and delete it, my system doesn't return to the original amount of available space. The lifecycle I'm applying to my containers is: > docker build ... > docker run CONTAINER_TAG >…
Franco Rabaglia
  • 1,163
  • 2
  • 9
  • 9
105
votes
2 answers

Docker COPY from ubuntu absolute path

I have following line in my dockerfile COPY /root/url.net/volumes/persistent/url/root /usr/share/nginx/html When I try to build the image with docker-compose I get Service 'frontend' failed to build: lstat …
isADon
  • 3,433
  • 11
  • 35
  • 49
105
votes
13 answers

How to install php-redis extension using the official PHP Docker image approach?

I want to build my PHP-FPM image with php-redis extension based on the official PHP Docker image, for example, using this Dockerfile: php:5.6-fpm. The docs say that I can install extensions this way, installing dependencies for extensions…
starikovs
  • 3,240
  • 4
  • 28
  • 33
104
votes
6 answers

Why the "none" image appears in Docker and how can we avoid it

When I run the docker-compose build command to rebuild an image in Docker because I had changed something in Dockerfile, sometimes I get "none" image tags. How can we avoid this fact? I want to rebuild the image but the none image should not…
Chen Hanhan
  • 1,549
  • 6
  • 15
  • 20
103
votes
10 answers

apt-get update' returned a non-zero code: 100

I am trying to create a docker image from my docker file which has the following content: FROM ubuntu:14.04.4 RUN echo 'deb http://private-repo-1.hortonworks.com/HDP/ubuntu14/2.x/updates/2.4.2.0 HDP main' >> /etc/apt/sources.list.d/HDP.list RUN echo…
roy
  • 6,344
  • 24
  • 92
  • 174
99
votes
5 answers

Docker & Laravel : configure: error: Package requirements (oniguruma) were not met

Can anyone help me with this problem. When i try to create a docker image from a dockerfile for laravel application i get this error: checking for oniguruma... no configure: error: Package requirements (oniguruma) were not met: No package…
Amy Murphy
  • 1,003
  • 1
  • 5
  • 10
98
votes
6 answers

What is the difference between Docker Service and Docker Container?

When do we use a docker service create command and when do we use a docker run command?
Kunal Sehegal
  • 991
  • 1
  • 6
  • 5
97
votes
8 answers

standard_init_linux.go:211: exec user process caused "exec format error"

I am building the Dockerfile for python script which will run in minikube windows 10 system below is my Dockerfile Building the docker using the below command docker build -t python-helloworld . and loading that in minikube docker demon docker save…
Pandit Biradar
  • 1,777
  • 3
  • 20
  • 35
94
votes
3 answers

Docker using gosu vs USER

Docker kind of always had a USER command to run a process as a specific user, but in general a lot of things had to run as ROOT. I have seen a lot of images that use an ENTRYPOINT with gosu to de-elevate the process to run. I'm still a bit confused…
MrE
  • 19,584
  • 12
  • 87
  • 105
93
votes
6 answers

Docker COPY files using glob pattern?

I have a monorepo managed by Yarn, I'd like to take advantage of the Docker cache layers to speed up my builds, to do so I'd like to first copy the package.json and yarn.lock files, run yarn install and then copy the rest of the files. This is my…
Fez Vrasta
  • 14,110
  • 21
  • 98
  • 160
93
votes
6 answers

How to configure different dockerfile for development and production

I use docker for development and in production for laravel project. I have slightly different dockerfile for development and production. For example I am mounting local directory to docker container in development environment so that I don't need to…
Dev Khadka
  • 5,142
  • 4
  • 19
  • 33
92
votes
9 answers

MYSQL_ROOT_PASSWORD is set but getting "Access denied for user 'root'@'localhost' (using password: YES)" in docker container

I have a docker-compose file and a Dockerfile. MySQL is installed properly. I have set MYSQL_ROOT_PASSWORD. But when trying to access mysql db, getting the error - Access denied. I have read the other threads of this site, but couldn't get that much…
Mukit09
  • 2,956
  • 3
  • 24
  • 44