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
Related
I am unable to run my docker image on Port 4000, even though I can run it on Port 80. What am I doing wrong here?
FROM node:latest as build
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM nginx:latest
COPY --from=build /usr/src/app/dist/admin /usr/share/nginx/html
EXPOSE 4200
I'm creating the image using the following command:
docker build --pull --rm -f "DockerFile" -t admin1:v1 "."
When I run it on port 80, I'm able to use it:
docker run --rm -d -p 4200:4200/tcp -p 80:80/tcp admin1:v1
However, when I run the following command, I'm unable to use it:
docker run --rm -d -p 4200:4200/tcp -p 4000:4000/tcp admin1:v1
I have researched similar questions online, but I haven't been able to fix the problem. Any suggestion will be greatly appreciated!
You need to map the docker container port to the docker host port.
Try the following Command
docker run --rm -d -p 4200:4200/tcp -p 4000:80/tcp admin1:v1
The following is the extract from the Docker Documentation
-p 8080:80 Map TCP port 80 in the container to port 8080 on the Docker host.
You can refer the link for further information.
Docker Documentation
I am unable to load swagger api using Docker where as the jar file runs fine: java -jar abc.jar
My swagger api doc: http://localhost:8080/api-docs/swagger.json
Docker File
FROM openjdk:14.0.2
RUN mkdir /opt/app
COPY ./build/libs/OrderManagementSystem-1.0-SNAPSHOT.jar /opt/app
EXPOSE 8080
CMD ["java", "-jar", "/opt/app/OrderManagementSystem-1.0-SNAPSHOT.jar"]
Command
docker run -p 3333:8080 order-price
I am unable to load the page http://localhost:8080/swagger
it seems you are hitting the wrong URL.
it should be,
http://localhost:3333/swagger
or
http://localhost:3333/api-docs/swagger.json
when you are binding ports while running docker, it is docker run -p host-port:container-port
If you run your container like that:
docker run -p 3333:8080 order-price
You are telling docker to expose internal port 8080 to port 3333 on your host.
So if you wan't to access Swagger API, you have to use :
http://localhost:3333/swagger
If you really want to user port 8080 on your host, then you have to launch your container like that :
docker run -p 8080:8080 order-price
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
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.
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.