Run docker image without specifying port - docker

I have a node project and it has Dockerfile and docker-compose.yml as well.
Dockerfile
FROM node:lts-alpine as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM nginx:stable-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
docker-compose.yml
version: '3'
services:
my-service-name:
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:80"
restart: unless-stopped
I uploaded the image to Docker Hub. When I tried to pull the image and run it, I needed to specify the port like this docker run -p 8080:80 my-username/my-image-name so I can open the project in localhost:8080 from NGINX expose 80.
What I want to do is run the image without specifying the port since I already specified the port in Dockerfile and docker-compose. I've been confused with how to achieve this. Does this mean my docker-compose is not uploaded to the Docker Hub and I should do so? Or is my current way is already correct?

When you use a docker-compose file, you have to run it with the docker-compose executable. What you are doing is bypassing the compose file altogether.

You are misinterpreting the meaning of EXPOSE in the Dockerfile. From the documentation:
The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the -p flag on docker run to publish and map one or more ports, or the -P flag to publish all exposed ports and map them to high-order ports.
So feel free to run containers without specifying exposed ports on the docker command line or in Docker-Compose or anywhere else The containers will run but it's like they are behind a firewall.

Related

Dockerfile expose 80 port

I am trying to create a simple docker container with go mod and 1.18.
my app runs in 8080 port but i wanna run in :80 port
Dockerfile
FROM golang:1.18
WORKDIR /usr/src/app
# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
COPY go.mod go.sum ./
RUN go mod download && go mod verify
COPY . .
RUN go build -o server
CMD [ "./server" ]
so i run docker build:
docker build -t go-k8s .
and docker run
docker run --rm -p 8080:8080 go-k8s:latest
And nothing happens :(
As larsks says, you need to bind from the external port 80 to the internal port 8080 using the 80:8080 syntax.
Something else to consider is making sure that your app is listening on all interfaces in your development environment.
This question seems at least vaguely related to yours

How does the command key in docker-compose file work

I am trying to understand the docker sample application 'example-voting-app'. I am trying to build the app with docker-compose. I am confused with the behaviour of 'command' key in docker compose file and the CMD Instruction in Dockerfile. The application consists of a service called 'vote'. The configuration for the vote service in docker-compose.yml file is:
services: # we list all our application services under this 'services' section.
vote:
build: ./vote # specifies docker to build the
command: python app.py
volumes:
- ./vote:/app
ports:
- "5000:80"
networks:
- front-tier
- back-tier
The configuration of the Dockerfile provided in ./vote directory is as below:
# Using official python runtime base image
FROM python:2.7-alpine
# Set the application directory
WORKDIR /app
# Install our requirements.txt
ADD requirements.txt /app/requirements.txt
RUN pip install -r requirements.txt
# Copy our code from the current folder to /app inside the container
ADD . /app
# Make port 80 available for links and/or publish
EXPOSE 80
# Define our command to be run when launching the container
CMD ["gunicorn", "app:app", "-b", "0.0.0.0:80", "--log-file", "-", "--access-logfile", "-", "--workers", "4", "--keep-alive", "0"]
My doubt here is which command ( 'python app.py' or 'gunicorn app:app -b ...') will be executed when i try building the application using docker-compose up
The Docker Compose command:, or everything in a docker run invocation after the image name, overrides the Dockerfile CMD.
If the image also has an ENTRYPOINT, the command you provide here is passed as arguments to the entrypoint in the same way the Dockerfile CMD does.
For a typical Compose setup you shouldn't need to specify a command:. In a Python/Flask context, the most obvious place it's useful is if you're also using a queueing system like Celery with the same shared code base: you can use command: to run a Celery worker off of the image you build, instead of a Flask application.

How to access WebAPi after deploying on docker

I have my Dockerfile:
FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /build
COPY . .
RUN dotnet restore
RUN dotnet publish -c Release -o output
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY --from=build /build/output .
ENTRYPOINT ["dotnet","TestDockerApi.dll"]
I am creating an image using :
docker build -t testdocker/api .
and then running a container from image using :
docker run testdocker/api
I can see following message on my console:
Hosting environment: Production
Content root path: /app
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.
I am trying to access using http://localhost/app/TestDockerApi/Values , but it does not work.
Do I need to use docker image IP to access that .
I can see few tutorials suggesting to do this in Entrypoint :
ENTRYPOINT ["dotnet","TestDockerApi.dll","--server.urls","http://0.0.0.0:5000"]
And then while running the container, mapping the port:
docker run -p 80:5000 testdocker/api
Is there any way I could access the API with out using portforwarding? I am just trying to get the basics right , why and what should I do.
The Dockerfile does not manage network configuration outside of the container at all. If you want docker to listen on your host port of 80, you need to bind it when you run your container.
docker run -80:80 testdocker/api
For more description about mapping and exposing ports, you can read here:
- https://www.ctl.io/developers/blog/post/docker-networking-rules/
Alternatively you can create your own service composition where you specify these details and specify this in a docker-compose.yml file
api:
image: testdocker/api
ports:
- "80:80"
And then you can simply run with
docker-compose up
More information is at:
https://docs.docker.com/compose/reference/overview/#command-options-overview-and-help

How to make a docker compose file for an existing image?

What I am trying to do is use a Docker image I found online timwiconsulting/ionic-v1.3, and run my ionic project within Docker. I want to mount my ionic project in Docker and forward my ports so I can run the emulator in a browser.
I want to ask how do I create a docker-compose.yml file for an existing container?
I found a Docker image timwiconsulting/ionic-v1.3 that I want to run, which has the correct version of the tools that I want.
Now I want to create a compose file to forward the ports to my computer, and mount the project files. I create this docker-compose.yml file:
version: '3'
services:
web:
build: .
ports:
- "8100:8100"
- "35729:35729"
volumes:
- /Users/leetcat/project/:/project
But every time I try to do docker-compose up I get the error:
~/user: docker-compose up
Building web
Step 1/6 : FROM timwiconsulting:ionic-v1.3
ERROR: Service 'web' failed to build: pull access denied for timwiconsulting, repository does not exist or may require 'docker login
I am doing something wrong. I think I want to be creating a docker-compose.yml file for the container timwiconsulting/ionic-v1.3. Feel free to tell me I am totally off the mark with what docker is.
Here is my Dockerfile:
# Use an official Python runtime as a parent image
FROM timwiconsulting:ionic-v1.3
# Set the working directory to /app
WORKDIR /project
# Copy the current directory contents into the container at /app
ADD . /project
# Install any needed packages specified in requirements.txt
# RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 8100
EXPOSE 35729
# Define environment variable
ENV NAME World
# Run app.py when the container launches
# CMD ["python", "app.py"]
# docker exec -it <container_hash> /bin/bash/

create docker-compose file from Dockerfile

I have this simple Dockerfile:
FROM node:boron
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install
# Bundle app source
COPY . /usr/src/app
EXPOSE 8080
CMD [ "npm", "start" ]
I would like to use docker-compose so that I can simply say docker-compose up or docker-compose down.
I am struggling to find a simple docker-compose example of how I would use docker-compose, all I can find are examples like this which cover more ground than I need.
How could I create a simple docker-compose file from the above?
You write following in docker-compose.yml file to run your docker container using compose:
version: '3'
services:
app:
build: .
ports:
- 8080:8080
Docker compose file is made of multiple services but in your case it is enough to define one service. build option specifies from where to pick up the Dockerfile to build the image and ports will allow port forwarding from the container to you host OS.
To start the service you can use:
docker-compose up
And to stop the service:
docker-compose down
You can find more documentation about the compose file here

Resources