This is the flask app:
`From flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Flask Dockerized'
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0')`
This is the docker file:
FROM ubuntu:14.04
MAINTAINER Ashish John Stanley "a*********#gmail.com"
RUN apt-get update -y RUN apt-get install -y python-pip python-dev build-essential
COPY . /app WORKDIR /app RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["app.py"]
Command to build docker file:
docker build -t flask-container:latest. -f --file="~/Documents/web/requirements.txt"
The execution of the command gives the following error terminal screenshot
--file should be pointed to a directory that contains Dockerfile
Here is a command to create docker image for your case:
docker build -t flask-container:latest .
Make sure you are running it from the directory that contains Dockerfile
Docker documentation:
--file , -f Name of the Dockerfile (Default is ‘PATH/Dockerfile’)
By default the docker build command will look for a Dockerfile at the
root of the build context. The -f, --file, option lets you specify the
path to an alternative file to use instead. This is useful in cases
where the same set of files are used for multiple builds. The path
must be to a file within the build context. If a relative path is
specified then it is interpreted as relative to the root of the
context.
Here is more info about Docker options
https://docs.docker.com/engine/reference/commandline/build/#options.
Related
I want to preface this in saying that I am very new to docker and have just got my feet wet with using it. In my Docker file that I run to build the container I install a program that sets some env variables. Here is my Docker file for context.
FROM python:3.8-slim-buster
COPY . /app
RUN apt-get update
RUN apt-get install wget -y
RUN wget http://static.matrix-vision.com/mvIMPACT_Acquire/2.40.0/install_mvGenTL_Acquire.sh
RUN wget http://static.matrix-vision.com/mvIMPACT_Acquire/2.40.0/mvGenTL_Acquire-x86_64_ABI2-2.40.0.tgz
RUN chmod +x ./install_mvGenTL_Acquire.sh
RUN ./install_mvGenTL_Acquire.sh -u
RUN apt-get install -y python3-opencv
RUN pip3 install USSCameraTools
WORKDIR /app
CMD python3 main.py
After executing the build docker command the program "mvGenTL_Acquire.sh" sets env inside the container. I need these variables to be set when executing the run docker command. But when checking the env variables after running the image it is not set. I know I can pass them in directly but would like to use the ones that are set from the install in the build.
Any help would be greatly appreciated, thanks!
For running a bash script when your container is creating:
make an script.sh file:
#!/bin/bash
your commands here
If you are using an alpine image, you must use #!/bin/sh instead of #!/bin/bash on the first line of your bash file.
Now in your Dockerfile copy your bash file in the container and use the ENTRYPOINT instruction for running this file when the container is creating:
.
.
.
COPY script.sh /
RUN chmod +x /script.sh
.
.
.
ENTRYPOINT ["/script.sh"]
Notice that in the ENTRYPOINT instruction use your bash file address in your image.
Now when you create a container, the script.sh file will be executed.
I am trying to build my Dockerfile from a child folder context.
this is my build file in build.sh
#!/bin/bash -ex
docker build -t app:latest -f ../Dockerfile .
This is my Dockerfile
FROM app:latest
WORKDIR /app
# This path must exist as it is used as a mount point for testing
# Ensure your app is loading files from this location
FROM ubuntu:20.04
RUN apt update
RUN apt install -y python3-pip
# Install Dependencies
COPY requirements.txt .
RUN pip3 install --no-cache-dir -r requirements.txt
This is my requirements.txt
Flask
pandas
requests
gunicorn
pytest
When I attempt to run build.sh within the scripts folder I get this error
#8 ERROR: "/requirements.txt" not found: not found
------
> [stage-1 4/7] COPY requirements.txt .:
------
failed to compute cache key: "/requirements.txt" not found: not found
When I just do the docker build command in the command line in the app directory I will do this:
docker build -t app:latest -f Dockerfile .
This will work, however going into the child directory and attempting to build it using the bash script will fail with the requirements.txt caching issue.
How do I successfully build my docker container from the child folder?
The docker build command takes a path to a context directory
docker build [... options ...] .
# ^ this path
When the Dockerfile COPY requirements.txt . it is always relative to the path at the end of the docker build command. It doesn't matter where the Dockerfile itself is physically located.
If you want to build an image from a parent directory, where the Dockerfile is located in that parent directory, you need to specify the path. If the Dockerfile is named Dockerfile and is in the root of the context directory (the standard recommended location) then you do not need a docker build -f option.
cd $HOME/testing-docker
docker build -t app .
cd $HOME/testing-docker/scripts
docker build -t app ..
# ^^ build the parent directory
# but no -f option
# the Dockerfile is in the default place
# Any way to specify the path will work
cd
docker build -t app $HOME/testing-docker
I want to run docker container with sidecar by this tutorial.
For example, i have java spring boot application. Then i made such Dockerfile:
# Dockerfile for GitLab CI/CD
FROM maven:3.5.2-jdk-8-alpine AS MAVEN_BUILD
ARG SPRING_ACTIVE_PROFILE
MAINTAINER SlandShow
COPY pom.xml /build/
COPY src /build/src/
WORKDIR /build/
RUN mvn clean install -Dspring.profiles.active=$SPRING_ACTIVE_PROFILE && mvn package -B -e -Dspring.profiles.active=$SPRING_ACTIVE_PROFILE
FROM openjdk:8-alpine
WORKDIR /app
COPY --from=MAVEN_BUILD /build/target/task-0.0.1-SNAPSHOT.jar /app/task-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java", "-jar", "task-0.0.1-SNAPSHOT.jar"]
After that i build docker image and run it:
$ docker build .
$ docker container run -p 8010:8010 <imageId>
Docker-CLI returns hash for started contaner. For example- cc82fa748a62de634893f5594a334ada2854f0be0dff8149efb28ae67c98191c.
Then i'am trying to start sidecar:
docker run -pid=container:cc82fa748a62de634893f5594a334ada2854f0be0dff8149efb28ae67c98191c -p 8080:8080 brendanburns/topz:db0fa58 /server --addr=0.0.0.0:8080
And get:
docker: invalid publish opts format (should be name=value but got '8080:8080').
What's wrong with it?
My fault, i forgot - before -p...
new to dockers.
running this docker build .
this is the Dockerfile
FROM gcr.io/google_appengine/python
# Create a virtualenv for dependencies. This isolates these packages from
# system-level packages.
RUN virtualenv /env
# Setting these environment variables are the same as running
# source /env/bin/activate.
ENV VIRTUAL_ENV /env
ENV PATH /env/bin:$PATH
# Copy the application's requirements.txt and run pip to install all
# dependencies into the virtualenv.
#ADD requirements.txt /app/requirements.txt
#RUN pip install -r /app/requirements.txt
# Add the application source code.
ADD . /app
# Run a WSGI server to serve the application. gunicorn must be declared as
# a dependency in requirements.txt.
CMD gunicorn -b :$PORT main:app
CMD bash1 "while true; do echo hello; sleep 1;done"
CMD ["sh", "while true; do echo hello; sleep 1;done"]
CMD "echo" "Hello docker!"
but after, when I run docker ps I don't see the image.
To build an image you have to use:
docker build -t username/imagename .
You have to use -t to tag your image and give it a name, from the docs:
-t, --tag value Name and optionally a tag in the
'name:tag' format (default [])
Then you can see the list of your images using:
docker images
You are using docker ps which is for listing containers not images.
More info about about images and containers.
Check the documentation on docker build.
use 'docker run ' to create and run a container. all docker build does is create an image.
Context
So I'm trying to execute build a polymer project inside a docker container as a volume (to access it I'm using docker run (...) --volume="/var/www/html:/var/www/html" --volumes-from="my-polymer-image-name" my-nginx-image).
And I tried execute the following Dockerfile, but declaring the volume last, but the volume was empty when I tried to access it from "my-nginx-container" (docker exec -ti my-nginx-image-name /bin/sh).
So I thought I had to declare the volume before using using it.
Problem
But when I tried to install my bower components, I noticed that no bower_components directory was being created.
########################################################
# Dockerfile to build Polymer project and move to server
# Based on oficial node Dockerfile
########################################################
FROM node:6
VOLUME /var/www/html
# Install polymer and bower
RUN npm install -g \
polymer-cli \
bower
# Add project to a temp folder to build it
RUN mkdir -p /var/www/html/temp
COPY . /var/www/html/temp
WORKDIR /var/www/html/temp
RUN ls -la
RUN bower install --allow-root # here is where I try to build my project
RUN polymer build
# Move to release folder
WORKDIR /var/www/html
RUN mv /var/www/html/temp/build/unbundled/* /var/www/html
RUN bower install --allow-root
# Remove temporary content
RUN rm -rf /var/www/html/temp
Volume mount when docker image build done.
in last row in Docker file add
ENTRYPOINT ["/bin/bash", "/etc/entrypoint.sh"]
Use entripoint script like this.
#!/bin/bash
set -e #if error bash script will exit and stop docker image
cd /var/www/html/
bower install --allow-root
polymer build
mv /var/www/html/temp/build/unbundled/* /var/www/html
rm -rf /var/www/html/temp