skip to Main Content

I am testing docker with official doc. just following official doc.
I am using official code

https://github.com/docker/getting-started/tree/master/app

and then this Dockerfile

# syntax=docker/dockerfile:1
FROM node:12-alpine
RUN apk add --no-cache python2 g++ make
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
EXPOSE 3000

and then build command

docker build -t getting-started .

and then run command

docker run -dp 3000:3000 getting-started

It’s perfect. but I just want to modified a little code more simple.
like this. This is nodejs code.

const http = require('http');

const hostname = '0.0.0.0';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

and then Dockerfile like this

FROM node:12-alpine
WORKDIR /app
COPY . .
CMD ["node", "src/index.js"]
EXPOSE 3000

and then build and run same command.
I checked my running container with this command "docker ps".I couldn’t see my container is running.

I ran "docker ps -a" command. I was able to see the container with no port mapping container. So I wasn’t able to connect my container.

I added both of docker state.
I ran same command but firstcontainer don’t have port mapping. What’s wrong?

docker run -dp 80:3000 getting-started   -> this is official app
docker run -dp 3000:3000 firstcontainer  -> my simple app
docker ps -a

CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS  PORTS
fea13c68893f   firstcontainer    "docker-entrypoint.s…"   10 seconds ago   Exited (1) 10 seconds ago  
2f552d1e9f55   getting-started   "docker-entrypoint.s…"   26 seconds ago   Up 25 seconds  
 0.0.0.0:80->3000/tcp, :::80->3000/tcp

2

Answers


  1. Your EXPOSE instruction is written after CMD. Change it to come before CMD. The CMD or ENTRYPOINT MUST be the last instruction in your Dockerfile. There is nothing to "run" in this image otherwise.

    Login or Signup to reply.
  2. You can run the docker command in foreground to see the logs (without the d flag):

    docker run -p 3000:3000 firstcontainer  -> my simple app
    

    and see what’s the error

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search