My Dockerfile looks like this:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
EXPOSE 5000
ADD target/*.jar app.jar
ENV JAVA_OPTS=""
ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/.urandom -jar /app.jar"]
I would like to pass a couple of environment variables like RDS_HOSTNAME to the docker container. How should I modify this file to do that?
You can pass ENV during
Build Time
Run Time
To Set ENV during build time you will need modification in Dockerfile.
ARG RDS_HOSTNAME
ENV RDS_HOSTNAME="${RDS_HOSTNAME}"
And pass the RDS_HOSTNAME ENV during build time.
docker build --build-arg RDS_HOSTNAME=$RDS_HOSTNAME -t my_image .
Run Time:
As mentioned in the comment you can just pass
docker run -ti -e RDS_HOSTNAME=$RDS_HOSTNAME yourimage:latest
With the second approach, your Docker container will not container information if someone gets access but you will need to pass every time you run the container, while you first you just need to pass once during build time.
Related
I am having a problem running Docker container after upgrading from NodeJS 8.2 to 9.1. This is the message I am getting.
I used the Dockerfile I found in Docker Hub but got an error of not being able to find package.json. So I commented it out and use the one I found on NodeJS website.
Below is the Docker File:
Dockerfile
FROM node:9.1.0
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
ONBUILD ARG NODE_ENV
ONBUILD ENV NODE_ENV $NODE_ENV
ONBUILD COPY package*.json ./
ONBUILD RUN npm install && npm cache clean --force
ONBUILD COPY . /usr/src/app
CMD [ "npm", "start" ]
I would appreciate help from more experienced users.
Your docker run command syntax is wrong. Everything after the image name is used to override the command run in your container. So docker run myimage -d will try to run -d inside the container, while docker run -d myimage will run your container with the -d option to docker run (detached mode).
The Dockerfile you referenced is meant to be used as parent image for an easy dockerization of your application.
So to dockerize your nodejs application, you'd need to create a dockerfile using the docker image created by said dockerfile.
The ONBUILD instruction gets executed whenever a new image is built with this particular image as parent image (FROM instruction). More info
I've never used an image like this, but from the looks of it, it should be enough to reference the image with the FROM instruction and then provide the NODE_ENV via build args.
The dockerfile to add into your project:
FROM this_image:9.1
How to build your application image:
docker build -t IMAGE_NAME:TAG --build-arg NODE_ENV=production .
I have a GitlabCI stage for building Docker image of a Python app. It uses a dynamic S3 variable name which is defined at stage level like this:
Build Container:
stage: package
variables:
S3_BUCKET: <value>
I tried using it inside Dockerfile like this as I want to use it during build time:
FROM python:3.8
EXPOSE 8080
WORKDIR /app
COPY requirements.txt ./requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
RUN SOMETHING
ARG S3_BUCKET_NAME=${S3_BUCKET}
RUN echo "$S3_BUCKET_NAME"
RUN python3 some_Script.py
CMD streamlit run app.py
But this variable is not able to fetch value. Is there some other way of using it inside Dockerfile.
How do you build the image? You will need to ensure that the flag --build-arg passes S3_BUCKET_NAME in.
If you wish to persist the value within the image, such that the value is available during runtime, you will need to export it via a RUN command, or an ENV command. For example
# expect a build-time variable
ARG A_VARIABLE
# use the value to set the ENV var default
ENV an_env_var=$A_VARIABLE
I'm working on a container to use megacmd (CLI syncing utility from Mega.nz, storage provider).
Relatively new to Dockerfiles, I've successfully made a dockerfile that will install MegaCMD, and login, but once it does that, it stops the container.
In my compose file I have set tty: true, thinking that would keep it alive, but it does not.
FROM ubuntu:groovy
ENV email=email#example.com
ENV password=notyourpassword
RUN apt-get update \
....more stuff here
COPY megalogin.sh /usr/bin/local/megalogin.sh
ENTRYPOINT ["sh", "/usr/bin/local/megalogin.sh"]
####Works up to here but the container still stops when finished the login script
megalogin.sh
#!/bin/sh
mega-login ${email} ${password}
mega-whoami
What do I need to do to make this thing to stay running?
I have tried the exec "$#" at the end of the script but that didnt make any difference.
When you run your container append the tail -f /dev/null to the docker run command e.g.
docker run -d [container-name] tail -f /dev/null
You should then be able to exec into the running container using docker exec [container-name] /bin/bash
So not the exact best solution, but in the compose file I put:
And it worked.
tty: true
stdin_open: true
I'm trying to deploy a container and dynamically set the correct configuration (dev, qa, prod) at run time
basically I want to do this....(which doesn't work)
FROM nginx:alpine
COPY nginx.conf /etc/nginx/nginx.conf
WORKDIR /usr/share/nginx/html
COPY dist/ .
CMD cp configuration.$env.json configuration.json
ENTRYPOINT [ "nginx", "-g", "daemon off;" ]
And then when I start the container I pass the env that I want
docker run -d -p 8080:80 --name MyUI -e env=qa myimage
Ideally I want the CMD to be a shell command, so I get the environment var and the ENTRYPOINT to be exec form (if this is possible)
Have you tried using ARG ? Also, if these are just qa, prod etc then okay but if you are planning to use secrets that way then better using env_file and encrypt it as ARG leaves traces
I'm trying to put my new ASP Website (MCV5) on the AWS server. For this I used EC2 and I created a virtual machine on linux. I also build my docker and now I'm trying to run it with:
sudo docker run -t -d -p 80:5004 myapp
But each time I just get a random number like this :
940e7abfe315d32cc8f5cfeb0c1f13750377fe091aa43b5b7ba4
When I try to know if my docker is running with:
sudo docker ps
no information is showed...
For information, when I put sudo docker images, I get my application created.
My Dockerfile contain:
FROM microsoft/aspnet
COPY . /app
WORKDIR /app
RUN ["dnu", "restore"]
EXPOSE 5004
ENTRYPOINT ["dnx", "-p", "project.json", "kestrel"]
-d means to run detached. The number you see is the container id.
If you want to get information about the container, you can use that container id
docker inspect <container id>
docker logs <container id>
If you'd like the container to run and print logs to the terminal, remove the -d