What's wrong with this dockerfile - docker

What's wrong with my dockerfile?
The dockerfile is in the rootfolder of my repo and the dist-folder too.
FROM nginx
# copy folder
COPY dist /usr/share/nginx/html
EXPOSE 8080
CMD ["nginx"]
I build the image:
docker build -f Dockerfile.nginx -t localhost:5000/test/image:${version} .
The image is there after performing docker images
It looks so simple but when I try to run the image as a container:
docker run -d -p 80:8080 localhost:5000/test/image:15
545445f961f4ec22becc0688146f3c73a41504d65467020a3e572d136354e179
But: Exited (0) About a minute ago
The docker logs shows nothing

Default nginx behaviour is run as a daemon. To prevent this run nginx with parameter daemon off.
CMD ["nginx", "daemon off"]

By default, Nginx will fork into the background and -- as the original foreground process has terminated -- the Docker container will stop immediately. You can have a look at how the original image's Dockerfile handles this:
CMD ["nginx", "-g", "daemon off;"]
The flag -g "daemon off;" causes Nginx to not fork, but continue running in the foreground, instead. And since you're already extending the official nginx image, you can drop your CMD line altogether, as it will be inherited from the base image, anyway.

Related

Trying to run nginx image with php-fpm

I'm just learning Docker and have a problem with running nginx together with php. This is my Dockerfile
FROM nginx
RUN apt-get update -y
RUN apt-get install php7.4-fpm -y
ADD start.sh /
RUN chmod +x /start.sh
CMD ["/start.sh"]
start.sh content:
#!/bin/bash
service php7.4-fpm start
nginx -g 'daemon off;'
If I omit the last line CMD ["/start.sh"], accessing files from the host machine works but php files aren't processed because php7.4-fpm is not running. But when I add this line nginx stops serving any files. Through I can confirm that nginx and php are running inside the container with docker exec nginx-custom service nginx status and docker exec nginx-custom service php7.4-fpm status. The nginx error log is empty.
This is the CMD of the original nginx image, which I thought is the only thing that gets overwritten? I guess I have some basic problems in understanding how Docker works at this point.
CMD ["nginx", "-g", "daemon off;"]

NGINX Docker container won't stay running?

I've Googled and looked thru several answers on SO but nothing I'm trying seems to work
I have a Dockerfile which downloads PHP 7 (cli+fpm) and installs NGINX as a final step with this command in attempt to keep the container running:
RUN apt-get update && apt-get -y install nginx
CMD ["service","nginx", "start", "-g", "daemon off;"]
What am I not understanding about containers? I previously used the PHP binary itself as the web server, the final command would fire the built int server and the container stayed running and everything worked great.
NGINX exists with code 0?
Thoughts?
Try
CMD ["nginx", "-g", "daemon off;"]
When you run service nginx start, the command is responsible to only start the service. After starting nginx the command finishes its job successfully, with exit code 0.
As a result the container exits, since it's main process exited
You can see this by running
docker logs container_name
The logs will end with following line
Starting nginx: nginx.
Instead, if you run the command proposed it will iniate the nginx process without exiting

Passing environment variables from host to docker

I know it is a question that many people have asked. But I don't know what I'm doing wrong. I have this Dockerfile
FROM nginx
ENV USER=${USER}
COPY proof.sh .
RUN chmod 777 proof.sh
CMD echo ${USER} >> /usr/share/nginx/html/index.html
CMD ["nginx", "-g", "daemon off;"]
EXPOSE 80
When I execute the env command in Linux, I got USER=fran and after that I run these commands:
sudo docker run --entrypoint "/bin/sh" 5496674f99e5 ./prueba.sh and as well I run this other docker run --env USER -d -p 89:80 prueba . If I have understood well, doing the last command this environment variable from host it should be passed to the docker, but I don't get anything. Why?. Could you help me?. Thanks in advance
This should be like
FROM nginx
ARG USER=default
ENV USER=${USER}
RUN echo ${USER}
CMD ["nginx", "-g", "daemon off;"]
EXPOSE 80
So now if you build with some build-args like
docker build --no-cache --build-arg USER=fran_new -t my_image .
You will see in logs like fran_new
or if you want to have user from host OS at run time then pass it as an environment variable at run time then
docker run --name my_container -e USER=$USER my_image
So the container user will be the same as in host.

docker nginx container not starting

This is my Dockerfile
FROM nginx
EXPOSE 8080
CMD ["nginx", "-g", "daemon off;"]
I use docker build . and then docker run -it 603030818c86 to start my nginx container. But when I go to http://localhost:8080 it doesn't give me the nginx homepage. What am I doing wrong?
You exposing port inside docker network, try to use:
docker run -it -p 8080:8080 603030818c86

Passing arguments from CMD in docker

I have got below Dockerfile.
FROM node:boron
# Create app directory
RUN mkdir -p /usr/src/akamai
WORKDIR /usr/src/akamai
# Install app dependencies
COPY package.json /usr/src/akamai/
RUN npm install
# Bundle app source
COPY . /usr/src/akamai
#EXPOSE 8080
CMD ["node", "src/akamai-client.js", "purge", "https://www.example.com/main.css"]
Below is the command which I run from CMD after the docker image build
docker run -it "akamaiapi" //It executes the CMD command as given in above Dockerfile.
CMD ["node", "src/akamai-client.js", "purge", "https://www.example.com/main.css"] //I want these two arguments directly passed from docker command instead hard-coded in Dockerfile, so my Docker run commands could be like these:
docker run -it "akamaiapi" queue
docker run -it "akamaiapi" purge "https://www.example.com/main.css"
docker run -it "akamaiapi" purge-status "b9f80d960602b9f80d960602b9f80d960602"
You can do that through a combination of ENTRYPOINT and CMD.
The ENTRYPOINT specifies a command that will always be executed when the container starts.
The CMD specifies arguments that will be fed to the ENTRYPOINT.
So, with Dockerfile:
FROM node:boron
...
ENTRYPOINT ["node", "src/akamai-client.js"]
CMD ["purge", "https://www.example.com/main.css"]
The default behavior of a running container:
docker run -it akamaiapi
would be like command :
node src/akamai-client.js purge "https://www.example.com/main.css"
And if you do :
docker run -it akamaiapi queue
The underlying execution in the container would be like:
node src/akamai-client.js queue

Resources