Home > Software design >  Does the code gets copied from docker image to docker container
Does the code gets copied from docker image to docker container

Time:07-01

I'm new to the docker and I learned that code and environment don't get copied to container from image. Instead, the container just adds a new layer on top of the image.

I have a simple express server that runs on port 80

server.js:

const bodyParser = require('body-parser');

const app = express();

let userGoal = 'Learn Docker!';

app.use(
  bodyParser.urlencoded({
    extended: false,
  })
);

app.use(express.static('public'));

app.get('/', (req, res) => {
  res.send(`
    <html>
      <head>
        <link rel="stylesheet" href="styles.css">
      </head>
      <body>
        <section>
          <h2>My Course Goal!!!!</h2>
          <h3>${userGoal}</h3>
        </section>
        <form action="/store-goal" method="POST">
          <div >
            <label>Course Goal</label>
            <input type="text" name="goal">
          </div>
          <button>Set Course Goal</button>
        </form>
      </body>
    </html>
  `);
});

app.post('/store-goal', (req, res) => {
  const enteredGoal = req.body.goal;
  console.log(enteredGoal);
  userGoal = enteredGoal;
  res.redirect('/');
});

app.listen(80);

Dockerfile:

FROM node

WORKDIR /app

COPY package.json /app

RUN npm install

COPY . /app

EXPOSE 80

CMD ["node", "server.js"]

I've built the image and then ran two containers(one container exposes container port 80 to port 3000 of my laptop and another exposes to port 4000) using the image

I entered into a container using docker exec -it <container_id> bash and made changes to server.js When I saved the file, it doesn't get reflected in the get call of the express app (in both containers) I know for a fact that changing a file in one container should not change that file in all other containers (Otherwise there would be a huge security issue). But I'm just confused about the following items:

  • If the same image is used among all the containers, then how does docker handle when a file is changed in a container?
  • Does everything in the image get copied over to the container?

Please someone explain how a container is created from an image and how the container accesses code and environment that is present in the image

CodePudding user response:

Does everything in the image get copied over to the container?

Yes. A container is like an instance of the image.

If the same image is used among all the containers, then how does docker handle when a file is changed in a container?

The change will only affect the container in which the change was made. It does not affect the image nor any other containers using the same image (unless the change was made to a file on a volume that multiple containers share).

When I saved the file, it doesn't get reflected in the get call of the express app

If you stop and start the container (without recreating it), you should still see your changes persisted. The Express app may need to be restarted for your saved changes to take into effect.

Though generally, if you need to make a change, you should do it to your local files, rebuild the image, and start a new container. This is because the image is what gets distributed, not the container. If the changes are made in the container, then you won't be able to persist them if you need to deploy your image elsewhere.

For development purposes, you can look into using bind mounts, which make it easy for changes made to your local files to propagate to the container.

  • Related