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

Docker CMD exec-form for multiple command execution

Here is a silly example of running multiple commands via the CMD instruction in shell-form. I would prefer to use the exec-form, but I don't know how to concatenate the instructions. shell-form: CMD mkdir -p ~/my/new/directory/ \ && cd…
Zak
  • 12,213
  • 21
  • 59
  • 105
63
votes
2 answers

Differences Between Dockerfile Instructions in Shell and Exec Form

What is the difference between shell and exec form for CMD: CMD python my_script.py arg vs. CMD ["python", "my_script.py", "arg"] ENTRYPOINT: ENTRYPOINT ./bin/main vs. ENTRYPOINT ["./bin/main"] and RUN: RUN npm start vs. RUN ["npm",…
Sheena
  • 15,590
  • 14
  • 75
  • 113
63
votes
6 answers

Using docker-compose to set containers timezones

I have a docker-compose file running a few Dockerfiles to create my containers. I don't want to edit my Dockerfiles to set timezones because they could change at any time by members of my team and I have a docker-compose.override.yml file to make…
nobody
  • 7,803
  • 11
  • 56
  • 91
62
votes
1 answer

Docker compose how to mount path from one to another container?

I've nignx container and one asset container which have all my assets build from grunt or some other tools. Now in docker compose file, i want to mount asset container's 's folder path into nginx container so nginx can serve that files. How can we…
dev.meghraj
  • 8,542
  • 5
  • 38
  • 76
62
votes
4 answers

Docker-compose: deploying service in multiple hosts

I have a docker-compose file that deploys 8 different docker services in the same host. Is it possible to deploy it in different hosts?, I would like to deploy some service in one hosts and another ones in other host remotelly. Would I need to use…
Asier Gomez
  • 6,034
  • 18
  • 52
  • 105
59
votes
3 answers

How to push Docker containers managed by Docker-compose to Heroku?

I currently have a locally tested and working web app that consists of 4 docker containers: Java MVC, NodeJS, Flask, and MongoDB. I have 4 Dockerfiles, one for each, and I manage the builds with docker-compose.yml. However, now I want to push my…
AspiringMat
  • 2,161
  • 2
  • 21
  • 33
59
votes
5 answers

Using Docker I get the error: "SQLSTATE[HY000] [2002] No such file or directory"

I'm using Docker to create a container to test my web app built on PHP and MySQL on my Mac. My PHP app is built using Fat-Free Framework for MVC and routing. I have two Dockerfiles, one for MySQL and one for PHP. I've used test Docker…
roundtheworld
  • 2,651
  • 4
  • 32
  • 51
59
votes
2 answers

Run jar file in docker image

I created a Docker image with java, and am copying the jar file into the image. My Dockerfile is : FROM anapsix/alpine-java MAINTAINER myNAME COPY testprj-1.0-SNAPSHOT.jar /home/testprj-1.0-SNAPSHOT.jar RUN java -jar…
Svetoslav Angelov
  • 667
  • 1
  • 6
  • 10
59
votes
3 answers

Dockerfile: $HOME is not working with ADD/COPY instructions

Before filing a bug I would like to ask someone to confirm the weird docker build behavior I have recently faced with. Consider we have a simple Dockerfile where we're trying to copy some files into home directory of a non-root user: FROM…
Vitaly Isaev
  • 5,392
  • 6
  • 45
  • 64
58
votes
2 answers

Dockerfile replicate the host user UID and GID to the image

Similar to the SO post about replicating UID/GID in container from host but how do you build the image with a user with replicate UID and GID? Preferably, how do you do it with a dockerfile? I can do it with a bash script: #!/bin/bash # current…
minghua
  • 5,981
  • 6
  • 45
  • 71
58
votes
3 answers

Is it redundant in a Dockfile to run USER root since you're already root?

Looking at this Dockerfile it starts with: FROM sequenceiq/pam:centos-6.5 MAINTAINER SequenceIQ USER root Now that seems redundant, since by default you'd already be root. But for argument's sake - let's look at the parent Dockerfile....that…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
58
votes
6 answers

How to copy files from dockerfile to host?

I want to get some files when dockerfile built successfully, but it doesn't copy files from container to host. That means when I have built dockerfile, the files already be host.
Allen
  • 583
  • 1
  • 4
  • 7
57
votes
6 answers

How to install docker in docker container?

This is my Dockerfile: FROM golang # RUN cat /etc/*release RUN apt-get update RUN apt-get -y install apt-transport-https ca-certificates curl gnupg2 software-properties-common RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add…
Jolly23
  • 879
  • 1
  • 8
  • 13
57
votes
10 answers

Docker COPY not updating files when rebuilding container

I have a docker-compose-staging.yml file which I am using to define a PHP application. I have defined a data volume container (app) in which my application code lives, and is shared with other containers using…
Alex H
  • 759
  • 1
  • 5
  • 10
57
votes
6 answers

Installing Java in Docker image

This is my very first try to create a Docker image and I'm hoping someone can help me out. My Dockerfile looks roughly like this: FROM mybaseimage:0.1 MAINTAINER ... ENV JAVA_HOME /usr/lib/jvm/java-7-openjdk-amd64 RUN sed 's/main$/main universe/'…
Yana K.
  • 1,926
  • 4
  • 19
  • 27