how to config autostart service[nginx] in a running docker container - docker

my docker install my windows10 PC . the below step is which I created the container from the powershell window.
docker run -p 80 --name web -i -t ubuntu /bin/bash
#>apt-get update
#>apt-get install -y nginx
now, I found there are 2 questions when my pc restart.
the container 'web' is not running
when start 'web' container , the 'nginx' service is not running
the first question is resolved:
docker update --restart=always web
but the second question how to do? please help me

The best option in your case is running dedicated container for nginx instead of generic ubuntu with nginx being installed each time.
Use following command to run Nginx alpine release:
docker run -p 80 --name web-Nginx -d --restart always nginx:1.15.8-alpine

Related

Can't acces apache on docker from my network

I have this Dockerfile :
FROM ubuntu:20.04
EXPOSE 80
After installing apache2 package in the container I can't acces the default page of apache from the network. Also docker is in a virtual machine with debian 10. If I try the official apache image (https://hub.docker.com/_/httpd) everything works fine but I want to know why installing it manually doesn't work.
To build the container from the image I use this command :
sudo docker run --name ubuntu -p 80:80 -it ubuntu /bin/bash
I have run the exactly same test on my virtual centos machine and found working.
I've build the image using your dockerfile and run apache installation using below command.
docker build -t ubuntu
docker run --name ubuntu -p 80:80 -it ubuntu /bin/bash
and In terminal opened by the above mentioned command, i ran the below command.
apt-get update
apt-get install apache2
service apache2 start
After that opened another ssh terminal keeping the current running as i have not run the Ubuntu container in detached mode and checked by using.
docker ps -a
and found container is running with exposing 0.0.0.0:80 and checked
curl localhost
Please make sure you have not stoped docker container before running curl command or hit in the browser as its not run in detached mode or background.

why do i keep seeing nginx index.html on localhost when i run my docker image

I installed and run nginx on my linux machine to understand the configurations etc. After a while i decided to remove it safely by following this thread in order to use it in docker
By following this documentaion i run this command
sudo docker run --name ngix -d -p 8080:80 pillalexakis/myrestapi:01
And i saw ngix's homepage at localhost
Then i deleted all ngix images & stopped all containers and i also run this command
sudo docker system prune -a
But now restarted my service by this command
sudo docker run -p 192.168.2.9:7777:8085 phillalexakis/myfirstapi:01 and i keep seeing at localhost ngix index.html
How can i totally remove it ?
Note: I'm new with docker and i might have missed a lot of things. Let me know what extra docker commands should i run in order provide better information.
Assuming your host have been preparing as below
your files (index.html, js, etc) under folder - /myhost/nginx/html
your nginx configuration - /myhost/nginx/nginx.conf
Solution
map your files (call volume) on the fly from outside docker image via docker cli
This is the command
docker run -it --rm -d -p 8080:80 --name web \
-v /myhost/nginx/html:/usr/share/nginx/html \
-v /myhost/nginx/nginx.conf:/etc/nginx/nginx.conf \
nginx
copy your files into docker image by build your own docker image via Dockerfile
This is your Dockerfile under /myhost/nginx
FROM nginx:latest
COPY ./html/index.html /usr/share/nginx/html/index.html
This is the command to build your docker image
cd /myhost/nginx
docker build -t pillalexakis/nginx .
This is the command to run your docker image
docker run -it --rm -d -p 8080:80 --name web \
pillalexakis/nginx

Unable to run nginx inside docker

I am trying to run an nginx image inside a Docker container. I have tried these steps
ssh inside ubuntu docker image docker run -v /var/run/docker.sock:/var/run/docker.sock -it ubuntu:latest bash
Installed Docker
Run Nginx image docker run -d -p 80:80 nginx
curl localhost:80 gives Connection refused
Mapping docker.sock to containers means you will be using the docker daemon of the host machine, not the container's.
So when you run docker run -d -p 80:80 nginx command, the nginx container is created and run in host machine (sibling of the ubuntu container). Hence, 80:80 maps in the host machine.
Validate this by running docker ps in ubuntu container and in the host machine. The result should match. And I guess, you can do curl localhost in the host machine and hit the nginx server as well.

Is good idea to deploy production server using docker in ups?

I created a docker file that install my application for php and dependencies then use composer to install vendor packages all in docker container.
This container will link to MongoDB and Nginx to run.
It's ok for development but my question is it's ok to deploy my production env?
Consider in my production server I will install docker then run below command:
docker run --name MongoDB -d --rm mongodb:latest
docker run --name app --link MongoDb:mongodb -p 9000:9000 -d --rm myrepo/myapp:latest
docker run --link app:app --name Nginx --rm -d Nginx:latest
And then enter my domain.com and my production server using this dockers to run my app.
It's ok and stable?
You may want to use docker compose to setup the three as services and deploy the whole thing as one stack using docker-compose up
See this page for a sample

How to use docker container as apache server?

I just started using docker and followed following tutorial: https://docs.docker.com/engine/admin/using_supervisord/
FROM ubuntu:14.04
RUN apt-get update && apt-get upgrade
RUN apt-get install -y openssh-server apache2 supervisor
RUN mkdir -p /var/lock/apache2 /var/run/apache2 /var/run/sshd /var/log/supervisor
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
EXPOSE 22 80
CMD ["/usr/bin/supervisord"]
and
[supervisord]
nodaemon=true
[program:sshd]
command=/usr/sbin/sshd -D
[program:apache2]
command=/bin/bash -c "source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND"
Build and run:
sudo docker build -t <yourname>/supervisord .
sudo docker run -p 22 -p 80 -t -i <yourname>/supervisord
My question is, when docker runs on my server with IP http://88.xxx.x.xxx/, how can I access the apache localhost running inside the docker container from the browser on my computer? I would like to use a docker container as a web server.
You will have to use port forwarding to be able to access your docker container from the outside world.
From the Docker docs:
By default Docker containers can make connections to the outside world, but the outside world cannot connect to containers.
But if you want containers to accept incoming connections, you will need to provide special options when invoking docker run.
So, what does this mean? You will have to specify a port on your host machine (typically port 80) and forward all connections on that port to the docker container. Since you are running Apache in your docker container you probably want to forward the connection to port 80 on the docker container as well.
This is best done via the -p option for the docker run command.
sudo docker run -p 80:80 -t -i <yourname>/supervisord
The part of the command that says -p 80:80 means that you forward port 80 from the host to port 80 on the container.
When this is set up correctly you can use a browser to surf onto http://88.x.x.x and the connection will be forwarded to the container as intended.
The Docker docs describes the -p option thoroughly. There are a few ways of specifying the flag:
# Maps the provided host_port to the container_port but only
# binds to the specific external interface
-p IP:host_port:container_port
# Maps the provided host_port to the container_port for all
# external interfaces (all IP:s)
-p host_port:container_port
Edit: When this question was originally posted there was no official docker container for the Apache web server. Now, an existing version exists.
The simplest way to get Apache up and running is to use the official Docker container. You can start it by using the following command:
$ docker run -p 80:80 -dit --name my-app -v "$PWD":/usr/local/apache2/htdocs/ httpd:2.4
This way you simply mount a folder on your file system so that it is available in the docker container and your host port is forwarded to the container port as described above.
There is an official image for apache. The image documentation contains instructions in how you can use this official images as a base for a custom image.
To see how it's done take a peek at the Dockerfile used by the official image:
https://github.com/docker-library/httpd/blob/master/2.4/Dockerfile
Example
Ensure files are accessible to root
sudo chown -R root:root /path/to/html_files
Host these files using official docker image
docker run -d -p 80:80 --name apache -v /path/to/html_files:/usr/local/apache2/htdocs/ httpd:2.4
Files are accessible on port 80.

Resources