docker nginx container not starting - docker

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

Related

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.

Ngnix Docker not serving content

I'm trying to build a docker container out of an angular 4 application. When running it with
docker run -p 80:80 -v /Users/mles/Documents/devicelab/dist/devicelab:/usr/share/nginx/html:ro nginx
I can see the index.html in the browser with localhost:80.
To make it portable I've made a DOCKERFILE:
FROM nginx
COPY dist/devicelab /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
I'm building it with
docker build --no-cache --rm -t dockerregistry.test.com/devicelab/devicelab .
then running it with
docker run dockerregistry.test.com/devicelab/devicelab:latest
Now when I hit localhost:80 nothing is happening. Nothin shows up in the browser and there is no log on the console. What is wrong with my Dockerfile?
You forgot the port on docker run
docker run -p 80:80 dockerregistry.test.com/devicelab/devicelab:latest
#rafaelncarvalho was correct in regards to the -p param, but for clarification the -p is to publish the already exposed port to the outside the network. Without the -p nginx will be accessible if you exec -it bash in, but not outside. The -p aka --publish also allows gives you control for mapping the container port to what you choose. ex docker container run -p 8080:80 nginx will publish the nginx server on port 8080

In Docker tomcat container tomcat is not running

i have made a Dockerfile for tomcat so that when I run container then container automatically start.
But after running container i checked tomcat is not started but container is running.
my Dockerfile is below:
FROM ubuntu
USER root
RUN mkdir -p /opt/soft/apache-tomcat-8.0.30
RUN mkdir -p /opt/soft/jdk1.8.0_65
COPY apache-tomcat-8.0.30 /opt/soft/apache-tomcat-8.0.30
COPY jdk1.8.0_65 /opt/soft/jdk1.8.0_65
ENV JAVA_HOME /opt/soft/jdk1.8.0_65
ENV PATH $PATH:$JAVA_HOME/bin:/opt/soft/apache-tomcat-8.0.30/bin
VOLUME ["/opt/soft/apache-tomcat-8.0.30/"]
EXPOSE 7070
WORKDIR /opt/soft/apache-tomcat-8.0.30/bin
CMD ["catalina.sh", "start"]
Your CMD should be like this.
CMD ["catalina.sh", "run"]
As the CMD is the main process running inside your container. Once it stops/exits/completes its execution the container will be stopped. So you need to select the CMD which will run infinitely. Here run will start the tomcat and tail the logs infinitely.
Since you are not changing default HTTP port for tomcat you need to use 8080.
EXPOSE 8080
You can access it with 7070 if you explicitly run the container
with
docker run -d -p 7070:8080 <tomcat_image>
If you start your container with docker run -d tomcat bash, bash will be your CMD in the container and the CMD you given in Dockerfile will be overriden. So you need to run your container without overriding the default CMD.
docker run -d -p 7070:8080 <IMAGE> <CMD>
docker run -d -p 7070:8080 mytomcat
In case your container running and still you are not able to access, check for the tomcat logs.

What's wrong with this dockerfile

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.

How can I expose more than 1 port with Docker?

So I have 3 ports that should be exposed to the machine's interface. Is it possible to do this with a Docker container?
To expose just one port, this is what you need to do:
docker run -p <host_port>:<container_port>
To expose multiple ports, simply provide multiple -p arguments:
docker run -p <host_port1>:<container_port1> -p <host_port2>:<container_port2>
Step1
In your Dockerfile, you can use the verb EXPOSE to expose multiple ports.
e.g.
EXPOSE 3000 80 443 22
Step2
You then would like to build an new image based on above Dockerfile.
e.g.
docker build -t foo:tag .
Step3
Then you can use the -p to map host port with the container port, as defined in above EXPOSE of Dockerfile.
e.g.
docker run -p 3001:3000 -p 23:22
In case you would like to expose a range of continuous ports, you can run docker like this:
docker run -it -p 7100-7120:7100-7120/tcp
if you use docker-compose.ymlfile:
services:
varnish:
ports:
- 80
- 6081
You can also specify the host/network port as HOST/NETWORK_PORT:CONTAINER_PORT
varnish:
ports:
- 81:80
- 6081:6081
Use this as an example:
docker create --name new_ubuntu -it -p 8080:8080 -p 15672:15672 -p 5432:5432 ubuntu:latest bash
look what you've created(and copy its CONTAINER ID xxxxx):
docker ps -a
now write the miracle maker word(start):
docker start xxxxx
good luck
Only one point to add. you have the option to specify a range of ports to expose in the dockerfile and when running it:
on dockerfile:
EXPOSE 8888-8898
Build image:
docker build -t <image_name>:<version> -f dockerfile .
When running the image:
docker run -it -p 8888-8898:8888-8898 -v C:\x\x\x:/app <image_name>:<version>
If you are creating a container from an image and like to expose multiple ports (not publish) you can use the following command:
docker create --name `container name` --expose 7000 --expose 7001 `image name`
Now, when you start this container using the docker start command, the configured ports above will be exposed.

Resources