Tushar Chutani
Tushar Chutani

Reputation: 1560

Run node Docker without port mapping

I am very to new Docker so please pardon me if this this is a very silly question. Googling hasn't really produced anything I am looking for. I have a very simple Dockerfile which looks like the following

FROM node:9.6.1
RUN mkdir /usr/src/app
WORKDIR /usr/src/app

ENV PATH /usr/src/app/node_modules/.bin:$PATH

# install and cache app dependencies
COPY package.json /usr/src/app/package.json
RUN npm install --silent
COPY . /usr/src/app

RUN npm start
EXPOSE 8000

In the container the app is running on port 8000. Is it possible to access port 8000 without the -p 8000:8000? I just want to be able to do

docker run imageName

and access the app on my browser on localhost:8000

Upvotes: 5

Views: 5115

Answers (2)

cpt.John
cpt.John

Reputation: 173

Old question but someone might find it useful:

  1. First get the IP of the docker container by running

    docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id

  2. Then connect to it from the the browser or using curl using the IP and port exposed :

Note that you will not be able to access the container on 0.0.0.0 because port is not mapped

Upvotes: 2

Mohsen Hosseini
Mohsen Hosseini

Reputation: 51


By default, when you create a container, it does not publish any of its ports to the outside world. To make a port available to services outside of Docker, or to Docker containers which are not connected to the container’s network, use the ‍‍--publish or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host.
Read more: Container networking - Published ports

But you can use docker-compose to set config and run your docker images easily.

  • First installing the docker-compose. Install Docker Compose
  • Second create docker-compose.yml beside the Dockerfile and copy this code on them

    version: '3'
    services:
      web:
        build: .
        ports:
         - "8000:8000"
    
  • Now you can start your docker with this command
    docker-compose up
    If you want to run your services in the background, you can pass the ‍‍-d flag (for “detached” mode) to docker-compose up -d and use `docker-compose ps to see what is currently running.

Docker Compose Tutorial

Upvotes: 2

Related Questions