Trying to run nginx image with php-fpm - docker

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;"]

Related

I can't get access to an exposed port in docker

I'm using Ubuntu 20.04 and running Python 3.8. Here is my dockerfile:
FROM python:3.8
WORKDIR /usr/src/flog/
COPY requirements/ requirements/
RUN pip install -r requirements/dev.txt
RUN pip install gunicorn
COPY flog/ flog/
COPY migrations/ migrations/
COPY wsgi.py ./
COPY docker_boot.sh ./
RUN chmod +x docker_boot.sh
ENV FLASK_APP wsgi.py
EXPOSE 5000
ENTRYPOINT ["./docker_boot.sh"]
and my docker_boot.sh
#! /bin/sh
flask deploy
flask create-admin
flask forge
exec gunicorn -b 0.0.0.0:5000 --access-logfile - --error-logfile - wsgi:app
I ran docker run flog -d -p 5000:5000 in my terminal. And I couldn't get my app working by typing localhost:5000 but it worked quite well when I typed 172.17.0.2:5000 (the docker machine's ip address). But I want the app to run on localhost:5000.
I'm sure there is nothing wrong with the requirements/dev.txt and the code because it works well when I run flask run directly in my terminal.
Edit on 2021.3.16:
Add docker ps information when docker run flog -d -p 5000:5000 is running:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ff048e904183 flog "./docker_boot.sh -d…" 8 seconds ago Up 6 seconds 5000/tcp inspiring_kalam
It is strange that there's no mapping of the hosts. I'm sure the firewall is off.
Can anyone help me? Thanks.
Use docker run -d -p 0.0.0.0:5000:5000 flog.
The arguments and the flags that are after the image name are passed as arguments to the entrypoint of the container created from that image.
Run docker ps and you need to see something like
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
565a97468fc7 flog "docker_boot.sh" 1 minute ago Up 1 minutes 0.0.0.0:5000->5000/tcp xxxxxxxx_xxxxxxx

Is it possible to run nginx automatically on every startup in a docker container?

I'm new to docker.
I'm setting up nginx server to serve static files inside a docker container. I'd like to enable nginx to start automatically on every startup inside a docker container.
I've tried changing ENTRYPOINT, CMD and crontab when building DockerImage. But these settings to run nginx on every startup works only the first time I "run" a container. When I "stop" the container and "start" it again, nginx does not start automatically inside the container.
I'm looking for a way to start nginx on every startup of a container and my first question is "is it possible to do this?"
My second question is about a container cycle. Given that there are not many discussions on this subject (all discussions are about automatically running a script or sth else at the moment of "run"), I wonder if it is more efficient to "run" and "kill" a container each time than just "stopping" and "starting" a container.
Here are the lines of code I tried for DockerImage (with crontab), which was my first try.
RUN apt-get install -y cron
COPY run_server /etc/cron.d/run_server
RUN chmod 0644 /etc/cron.d/run_server
RUN crontab /etc/cron.d/run_server
RUN touch /var/log/cron.log
CMD cron && tail -f /var/log/cron.log
run_server is a simple crontab config file which includes:
#reboot service nginx start
Since this was not the solution I was looking for (it worked only when I "ran" a conainer, not "stopped" and "started" a container) I tried with supervisor, too.
RUN apt-get -y install supervisor && \
mkdir -p /var/log/supervisor && \
mkdir -p /etc/supervisor/conf.d
ADD supervisor.conf /etc/supervisor.conf
CMD ["supervisord", "-c", "/etc/supervisor.conf"]
supervisor.confg contains:
[supervisord]
nodaemon=true
[program:run_server]
command=/usr/bin/python3.6 /home/server.py
autostart=true
directory=/home
redirect_stderr=true
But neither of them worked the way I wanted ..
my Dockerfile , container ( CentOS 8 and nginx ) linux mint 19.3 Docker version 19.03.4
# howto: Dockerfile
# CentOS 8 and nginx
# docker build -t centose .
# docker run -it -p 80:80 centose
# curl localhost
FROM centos:latest
# MAINTAINER їван
RUN yum -y install nginx
EXPOSE 80
CMD ["/usr/sbin/nginx", "-g", "daemon off;"]
#WORKDIR /usr/sbin/
I would try to update my docker file and enable the nginx service so it will be started during the next reboot.
Here is a couple of different ways:
RUN systemctl enable nginx
RUN service nginx start
Another way would be to add a bootstrap script that starts the service:
#!/bin/bash
sudo service nginx start
tail -f /var/log/nginx/error.log
Make sure the bootstrap.sh is executable i.e sudo chmod +x bootstrap.sh.
Then update your docker file:
COPY boostrap.sh /bin/.
CMD ["bootstrap.sh"]
you can apply that using docker restart policy
you can simply set it when creating the container or updating the created ones
examples: docker run -t -d --restart unless-stopped nginx
to update the created ones: docker update --restart unless-stopped {container ID}

Cant get apache2 started in docker container

My Dockerfile:
FROM ubuntu:latest
RUN apt-get update
RUN apt-get install -y apache2
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf
CMD ["/usr/sbin/apache2ctl","start"]
But when I build image and run the container, I check apache status and get:
root#79074bf56be4:/# service apache2 status
* apache2 is not running
And if I start it manually inside the container, it works:
root#79074bf56be4:/# /usr/sbin/apache2ctl start
root#79074bf56be4:/#
root#79074bf56be4:/# service apache2 status
* apache2 is running
root#79074bf56be4:/#
What am I doing wrong? How to make the apache2 start automatically on container start?
Excuse me, I didn't google good
Here is the same question
Solved by replacing CMD line to this:
CMD apache2ctl -D FOREGROUND
And cuntainer run command to this:
docker run --detach my_image

Running multiple commands after docker create

I want to make a script run a series of commands in a Docker container and then copy a file out. If I use docker run to do this, I don't get back the container ID, which I would need for the docker cp. (I could try and hack it out of docker ps, but that seems risky.)
It seems that I should be able to
Create the container with docker create (which returns the container ID).
Run the commands.
Copy the file out.
But I don't know how to get step 2. to work. docker exec only works on running containers...
If i understood your question correctly, all you need is docker "run exec & cp" -
For example -
Create container with a name --name with docker run -
$ docker run --name bang -dit alpine
Run few commands using exec -
$ docker exec -it bang sh -c "ls -l"
Copy a file using docker cp -
$ docker cp bang:/etc/hosts ./
Stop the container using docker stop -
$ docker stop bang
All you really need is Dockerfile and then build the image from it and run the container using the newly built image. For more information u can refer to
this
A "standard" content of a dockerfile might be something like below:
#Download base image ubuntu 16.04
FROM ubuntu:16.04
# Update Ubuntu Software repository
RUN apt-get update
# Install nginx, php-fpm and supervisord from ubuntu repository
RUN apt-get install -y nginx php7.0-fpm supervisor && \
rm -rf /var/lib/apt/lists/*
#Define the ENV variable
ENV nginx_vhost /etc/nginx/sites-available/default
ENV php_conf /etc/php/7.0/fpm/php.ini
ENV nginx_conf /etc/nginx/nginx.conf
ENV supervisor_conf /etc/supervisor/supervisord.conf
#Copy supervisor configuration
COPY supervisord.conf ${supervisor_conf}
# Configure Services and Port
COPY start.sh /start.sh
CMD ["./start.sh"]
EXPOSE 80 443

brief overview of current docker workflow with nginx

I have currently installed docker 1.9 and I want to create and work on a nginx instance locally on osx and deploy the nginx instance to ubuntu.
All I can find online are conflicting posts from earlier versions of docker.
Can anyone give me a brief overview of how my workflow should be with docker 1.9 to accomplish this?
You can do this by having a simple nginx Dockerfile:
FROM ubuntu:14.04
RUN echo "Europe/London" > /etc/timezone
RUN dpkg-reconfigure -f noninteractive tzdata
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update
RUN apt-get install -y nginx
RUN apt-get install -y supervisor
ADD supervisor.nginx.conf /etc/supervisor.d/nginx.conf
ADD path/to/your/nginx/config /etc/nginx/sites-enabled/default
EXPOSE 80
CMD /usr/bin/supervisord -n
And a simple supervisor.nginx.conf:
[program:nginx]
command=/usr/sbin/nginx
stdout_events_enabled=true
stderr_events_enabled=true
Then building your image:
docker build -t nginx .
Then running your nginx container:
docker run -d -v /path/to/nginx/config:/etc/nginx/sites-enabled/default -p 80:80 nginx
This is assuming that you don't have anything running on port 80 on your host - if you do, you can change 80:80 to something like 8000:80 (in the format hostPort:containerPort.
Using -v and mounting your nginx config from your host is useful to do locally as it allows you to make changes to it without having to go into your container / rebuild it every time, but when you deploy to your server you should run a container that uses a config from inside your image so it's completely repeatable on another machine.

Resources