I'm playing around with Google's Dart docker image. I'm trying to build a Hello World app that listens on port 80. I'm running it on Ubuntu Server 14 on Azure.
If I run the google/dart-hello, it all works fine, and I can connect on port 8080.
The google/dart-hello image is based on the google/dart-runtime image, which is in turn, based on google/dart. The base image adds Dart; google/dart-runtime adds a Dockerfile which expects to execute bin/server.dart and expose port 8080, and google/dart-hello supplies the bin/server.dart (and pubspec.yaml) to make it work. google/dart-runtime isn't useful on its own, because it doesn't contain a bin/server.dart or pubspec.yaml.
So, google/dart-runtime is a good base if your server is at bin/server.dart and you want to listen on port 8080. As I want to listen on port 80, I'm using the google/dart image as a base, hoping to squash what's in google/dart-runtime and google/dart-hello into my container, but changed to port 80.
You can find the sources repos for the three Google images here:
google/dart
google/dart-runtime (Dockerfile)
google/dart-hello (Dockerfile)
So, I've taken the Dockerfile from google/dart-runtime and the files from google/dart-hello, so I have the following:
FROM google/dart
WORKDIR /app
ONBUILD ADD pubspec.yaml /app/
ONBUILD ADD pubspec.lock /app/
ONBUILD RUN pub get
ONBUILD ADD . /app
ONBUILD RUN pub get
CMD []
ENTRYPOINT ["/usr/bin/dart", "/app/bin/server.dart"]
EXPOSE 80
In the same directory as this Dockerfile, I have the following files:
bin/server.dart
pubspec.yaml
pubspec.lock
I'm building the image with:
sudo docker build --no-cache -t dart-test .
And here's the output:
danny#linux:~/dart_test$ sudo docker build --no-cache -t dart-test .
Sending build context to Docker daemon 5.632 kB
Sending build context to Docker daemon
Step 0 : FROM google/dart
---> cd92c7fff717
Step 1 : WORKDIR /app
---> Running in d163d2597eba
---> 2802d6769b76
Removing intermediate container d163d2597eba
Step 2 : ONBUILD ADD pubspec.yaml /app/
---> Running in 7b8be2a481c2
---> 096cbe12a2cd
Removing intermediate container 7b8be2a481c2
Step 3 : ONBUILD ADD pubspec.lock /app/
---> Running in 6ae0243b0dee
---> 80f20ebafa87
Removing intermediate container 6ae0243b0dee
Step 4 : ONBUILD RUN pub get
---> Running in 621d4ce5c7f1
---> 89a509d41b11
Removing intermediate container 621d4ce5c7f1
Step 5 : ONBUILD ADD . /app
---> Running in 4de26a33487f
---> b69c65f12441
Removing intermediate container 4de26a33487f
Step 6 : ONBUILD RUN pub get
---> Running in f7cc689f6f81
---> 2ccc79ea6d04
Removing intermediate container f7cc689f6f81
Step 7 : CMD []
---> Running in 10bd31eb6679
---> f828267f00b5
Removing intermediate container 10bd31eb6679
Step 8 : ENTRYPOINT ["/usr/bin/dart", "/app/bin/server.dart"]
---> Running in 013d3ca0f25d
---> a63b59f9fd05
Removing intermediate container 013d3ca0f25d
Step 9 : EXPOSE 80
---> Running in 4301c572e598
---> 75a4317c135c
Removing intermediate container 4301c572e598
Successfully built 75a4317c135c
However, if I try to run this (using sudo docker run --rm -i -t dart-test), I get the following error:
danny#linux:~/dart_test$ sudo docker run -i -t --rm dart-test
Unhandled exception:
Uncaught Error: FileSystemException: Cannot open file, path = '/app/bin/server.dart' (OS Error: No such file or directory, errno = 2)
If I replace the dart execution in the Dockerfile with /bin/bash instead, then when I build and run, I get put into bash at /app/ but the folder is empty.
I've tried this with both the 0.9(?) version and 1.2 (one that came from apt-get docker.io and the other from the more-involved instructions on the Docker website), since I noticed a mention of an ADD fix in the release notes. Both do the same.
I can find lots of info online that people often pipe Dockerfile into STDIN which means there's no context, but you can see in my output that 5KB of data is being sent; though it's possible this is just the Dockerfile and nothing else I guess? They are in the same directory, here's a listing:
danny#linux:~/dart_test$ dir
bin Dockerfile pubspec.lock pubspec.yaml
The ONBUILD instruction is only useful when you create a base image that will be used later by another Dockerfile (see the documentation).
Here because you write the final Dockerfile, you just need to remove the ONBUILD instructions (but keep the raw instructions ADD, RUN, etc).
In your Dockerfile you need to remove the ONBUILD prefixes from your instructions. The ONBUILD prefix is a way to defer execution of certain instructions until this image is referenced by another Dockerfile. Those instructions are stored as part of the metadata for the image that you created but the instructions themselves are not executed until that image is referenced in the FROM field of another Dockerfile.
What you really want is this:
FROM google/dart
WORKDIR /app
ADD pubspec.yaml /app/
ADD pubspec.lock /app/
RUN pub get
ADD . /app
RUN pub get
CMD []
ENTRYPOINT ["/usr/bin/dart", "/app/bin/server.dart"]
EXPOSE 80
The docker image google/dart-runtime is intended to be a base image for your Dart server application. So in your project the Dockerfile should have just the following content
FROM google/dart-runtime
The when you run docker build the ONBUILD commands in the base image will be executed.
If you look at the Dockerfile for google/dart-hello you will see that it has just that one line.
Related
The Dockerfile uses the COPY --from command from the other build Node layer, but the generated directory is not found.
Note 1: This Dockerfile works locally on my machine doing builds normally.
Note 2: In the execution log it mentions the removal of an intermediate container, is that it? Would it be possible to preserve this container so that the copy works?
FROM node:16.16 as build
# USER node
WORKDIR /app
COPY package.json /app
RUN npm install --location=global npm#latest && npm install --silent
COPY . .
ARG SCRIPT
ENV SCRIPT=$SCRIPT
ARG API_URL
ENV API_URL=$API_URL
ARG API_SERVER
ENV API_SERVER=$API_SERVER
CMD ["/bin/sh", "-c", "envsubst < src/proxy.conf.template.js > src/proxy.conf.js"]
RUN npm run ${SCRIPT}
FROM nginx:1.23
VOLUME /var/cache/nginx
EXPOSE 80
COPY --from=build /app/dist/siai-spa /usr/share/nginx/html
COPY ./config/nginx-template.conf /etc/nginx/nginx-template.conf
b9ed43dcc388: Pull complete
Digest: sha256:db345982a2f2a4257c6f699a499feb1d79451a1305e8022f16456ddc3ad6b94c
Status: Downloaded newer image for nginx:1.23
---> 41b0e86104ba
Step 15/24 : VOLUME /var/cache/nginx
---> Running in dc0e24ae6e51
Removing intermediate container dc0e24ae6e51
---> 3b2799dad197
Step 16/24 : EXPOSE 80
---> Running in f30edd617285
Removing intermediate container f30edd617285
---> 21985745ce49
Step 17/24 : COPY --from=build /app/dist/siai-spa /usr/share/nginx/html
COPY failed: stat app/dist/siai-spa: file does not exist
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: exit code 1
I guess, you should use CMD instead of RUN while npm run ${SCRIPT} as this needs to be executed during container running time rather than image build time.
Solved problem!
The difference was that locally I used docker-compose which captures the build arguments from the .env file. The npm run command did not override the ${SCRIPT} variable as the docker command does not use the env file, required to pass through the --build-arg parameters.
i would like to build my first Docker image, containing Apache Tomcat and a deployed web app. My Dockerfile is really small, based on Tomcat:8.0 image and is supposed to copy a WAR file into Tomcat's appBase.
Build of the image reports success, but the file is nowhere to be found in the container.
Copying from host to the container work w/o issues using "docker cp":
[root#edubox dock]# docker cp jdbcdemo_3.war 15dd44bbf992:/usr/local/tomcat/webapps/
My Dockerfile:
# we are extending everything from tomcat:8.0 image ...
FROM tomcat:8.0
MAINTAINER simo
# COPY path-to-your-application-war path-to-webapps-in-docker-tomcat
COPY ./jdbcdemo_3.war /usr/local/tomcat/webapps/
EXPOSE 8082
Image build:
root#edubox dock]# docker image build -t simo/jdbcdemo_3 --tag=recent ./
Sending build context to Docker daemon 10.24 kB
Step 1/4 : FROM tomcat:8.0
---> ef6a7c98d192
Step 2/4 : MAINTAINER simo
---> Using cache
---> 54d824d7258b
Step 3/4 : COPY ./jdbcdemo_3.war /usr/local/tomcat/webapps/
---> Using cache
---> f94330423a93
Step 4/4 : EXPOSE 8082
---> Running in 74b6dd0364b2
---> 9464f11ac18e
Removing intermediate container 74b6dd0364b2
Successfully built 9464f11ac18e
I would expect COPY to place the file where specified or an error message because of which this does not work.
Please try this way,
Keep the "jdbcdemo_3.war" where your Dockerfile exists. And make this change in the Dockerfile.
(remove ./ from your Dockerfile) like,
COPY jdbcdemo_3.war /usr/local/tomcat/webapps/
please check the permission side of the file.
you can give full permission and test once. (user:group)
try this: COPY jdbcdemo_3.war /tmp
In your Dockerfile. And build the image and check in the /tmp directory. If the file copied successfully, give the permission to /usr/local/tomcat/webapps/ Or copy to /tmp first and then copy from /tmp to /usr/local/tomcat/webapps/. Using COPY command in Dockerfile
Hi and many thanks for offering advice. The issue has been trivial in the end. I have not been examining the correct container.
I did not realize one needs to pick the freshly created image, run the container with this image and only afterwards peek for changes described in the Dockerfile in that container.
I have been looking into parent container which i now understand could not have worked.
Sry for wasting your time ;-)
Step1: I have created a local docker image of one NodeJS app.
Here is the dockerfile for this app -
FROM node:8
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm#5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm ci --only=production
# Bundle app source
COPY . .
#EXPOSE 8080
CMD [ "npm", "start" ]
**Step 2:**Then I built a docker image for this Node app. Here is the build command output -
C:\Users\shibathethinker\Documents\GitHub\NodeProjects\API_Proxy_ABN>docker build -t api-proxy .
Sending build context to Docker daemon 8.637MB
Step 1/6 : FROM node:8
---> 0bf36d7ccc1e
Step 2/6 : WORKDIR /usr/src/app
---> Running in 7187d65639f1
Removing intermediate container 7187d65639f1
---> 0e34dc93439c
Step 3/6 : COPY package*.json ./
---> 47c0d0ca8c77
Step 4/6 : RUN npm install
---> Running in d7e5163371df
npm WARN api_proxy#1.0.0 No repository field.
added 98 packages from 91 contributors and audited 194 packages in 8.598s
found 0 vulnerabilities
Removing intermediate container d7e5163371df
---> 72da705ae792
Step 5/6 : COPY . .
---> 0293df6aa27d
Step 6/6 : CMD [ "npm", "start" ]
---> Running in 251e98c0a0ae
Removing intermediate container 251e98c0a0ae
---> a92d8a95b8cd
Successfully built a92d8a95b8cd
Successfully tagged api-proxy:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
**Step 3:**Then, I wanted to use this docker image in another 'React' app.
Here is the Dockerfile of the app -
FROM api-proxy:latest
WORKDIR /app
RUN npm install
CMD [ "npm", "start" ]
# stage: 2 — the production environment
FROM nginx:alpine
#COPY —from=react-build /app/build /usr/share/nginx/html
#COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY /build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Step4: Now I built and ran this docker image generated on step 3.
Question:
It looks like the node app is not running on the newly created docker container.
If I 'ssh' into the docker container I can not see any node server running there.
I also could not find the WORKDIR (/usr/src/app) created in the step1 in this container.
What I am doing wrong?
Please let me know if I can clarify further.
You are doing a multi-stage docker build.You are building your application using nodejs (download dependencies and minification build) and copying and running it on nginx web server.
Nodejs server can be in its own independent container, which i think you have already done. The client would actually be a webserver for e.g. nginx, apache etc. that would serve the build of your react app. In the end, you will have 2 containers running: 1 for nodejs server and 1 for nginx webserver.
To place the build of your react app into this nginx webserver, you will use multi-stage build. In the first stage you will build your react app and in the second stage you will use nginx image and copy the react build from the first stage into the html folder of the nginx image.
I have auto generated the following docker file with sbt for my scala project:
FROM robsonoduarte/8-jre-alpine-bash:latest
WORKDIR /opt/docker
ADD opt /opt
RUN ["chown", "-R", "daemon:daemon", "."]
EXPOSE 6999
USER daemon
ENTRYPOINT ["bin/app"]
CMD []
Yet when I run build . -t app I get the following error:
Sending build context to Docker daemon 166.2MB
Step 1/7 : FROM robsonoduarte/8-jre-alpine-bash:latest
---> 9bbc00a23a9b
Step 2/7 : WORKDIR /opt/docker
Removing intermediate container 817f86d4a46e
---> b648d213f308
Step 3/7 : ADD opt /opt
ADD failed: stat /var/lib/docker/tmp/docker-builder679116314/opt: no such file or directory
I have tried to reinstall docker as suggested in another SO answer, but that did not help. I am also using windows, and I cannot find the /var/lib folder.
The ADD command copies files from <src> to <dest>. In your case, Docker expects a folder called 'opt' from the location where you run build . -t app. Does such a folder exist?
I am using jenkins image to create a docker container. For now I am just trying to create a new directory and copy a couple of files. The image build process runs fine but when I start the container I cannot see the files and the directory.
Here is my dockerfile
FROM jenkins:2.46.1
MAINTAINER MandeepSinghGulati
USER jenkins
RUN mkdir /var/jenkins_home/aws
COPY aws/config /var/jenkins_home/aws/
COPY aws/credentials /var/jenkins_home/aws/
I found a similar question here but it seems different because I am not creating the jenkins user. It already exists with home directory /var/jenkins_home/. Not sure what I am doing wrong
Here is how I am building my image and starting the container:
➜ jenkins_test docker build -t "test" .
Sending build context to Docker daemon 5.632 kB
Step 1/6 : FROM jenkins:2.46.1
---> 04c1dd56a3d8
Step 2/6 : MAINTAINER MandeepSinghGulati
---> Using cache
---> 7f76c0f7fc2d
Step 3/6 : USER jenkins
---> Running in 5dcbf4ef9f82
---> 6a64edc2d2cb
Removing intermediate container 5dcbf4ef9f82
Step 4/6 : RUN mkdir /var/jenkins_home/aws
---> Running in 1eb86a351beb
---> b42587697aec
Removing intermediate container 1eb86a351beb
Step 5/6 : COPY aws/config /var/jenkins_home/aws/
---> a9d9a28fd777
Removing intermediate container ca4a708edc6e
Step 6/6 : COPY aws/credentials /var/jenkins_home/aws/
---> 9f9ee5a603a1
Removing intermediate container 592ad0031f49
Successfully built 9f9ee5a603a1
➜ jenkins_test docker run -it -v $HOME/jenkins:/var/jenkins_home -p 8080:8080 --name=test-container test
If I run the command without the volume mount, I can see the copied files and the directory. However with the volume mount I cannot see the same. Even if I empty the directory on the host machine. Is this the expected behaviour? How can I copy over files to the directory being used as a volume ?
Existing volumes can be mounted with
docker container run -v MY-VOLUME:/var/jenkins_home ...
Furthermore, the documentation of COPY states:
All new files and directories are created with a UID and GID of 0.
So COPY does not reflect your USER directive. This seems to be the second part of your problem.