4

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

Tushar Chutani
  • 1,522
  • 5
  • 27
  • 57
  • 1
    https://stackoverflow.com/questions/32740344/how-to-publish-ports-in-docker-files –  Jan 08 '19 at 19:19
  • Possible duplicate of [How to publish ports in docker files](https://stackoverflow.com/questions/32740344/how-to-publish-ports-in-docker-files) – bummi Jan 08 '19 at 20:09
  • The answer is no. The docker file is to build the image and you use CMD [ "npm", "start" ] in the dockerfile to tell it exact what to run when the images starts up. Using docker is to sandbox the image. So you need to tell docker what you want to expose, network type to set , files to mount to disc etc... – chronolegend Jan 09 '19 at 08:14

2 Answers2

1

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

0

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

cpt.John
  • 143
  • 1
  • 9