Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make changes to the code inside a Docker Container?

Tags:

node.js

docker

I'm completely new to Docker and Containers. I'm also new to the entire VM concepts.

I understand that VM and Dockers are a way to make all the dependencies of an application to be available as a single component that you can easily deploy on clouds that support them.

I installed Docker on my local machine (an Ubuntu) and I used used this guide to download and run the Node Official Image.

What I need to know now is how do I make code changes for the files inside of Docker? I made changes to the 'server.js' inside the app and when I navigate to http://locahost:49160 the browser still shows the output from the old server.js. I restarted the container and yet I get the same old output.

I see that I can commit changes and make a new image. But then, isn't the old image completely useless?

Or am I not at all understanding how things with Docker work?

like image 493
Faizuddin Mohammed Avatar asked Nov 23 '16 07:11

Faizuddin Mohammed


People also ask

Can I edit code in Docker container?

Editing files in a running Docker container is only recommended when working in a development environment during conceptualisation and when building proof-of-concepts. Once you've made changes to your project in Docker containers, save a new image with those changes in place.

How do I push changes to a Docker container?

This can be done by using the --message="message to commit" command. As before, there is a shorthand version of this command, -m . To see the list of Docker commit messages, use the docker history command.


2 Answers

You could make changes inside an container and could even commit these changes to the original image or create a new image as well as.

But I recommend you to create a new images or tags.

Review the guide. When you want to make a change on server.js, let do it but build a new image for it also. Don't forget to add a tag (test1).

Example, docker build -t <your username>/node-web-app:test1 .

Then you could run the new image with docker run -p 49160:8080 -d <your username>/node-web-app:test1

like image 124
Tuan Avatar answered Oct 24 '22 07:10

Tuan


In your case. You'll probably need to rebuild your image:

docker build -t my-new-image .

You made an edit to your server.js which is copied inside your image/container. The old image will be useless indeed. You'll need to start a new container with docker run after the build.

In some cases it's possible to mount your code/data to your docker container with docker run -v my-local-volume:/volume-in container ...

When you make edits to your code on your local machine it will be updated inside your container automatically. Without updating the image or restarting the container. But in your case your copying the code inside your application with the COPY command of the dockerfile.

like image 27
lvthillo Avatar answered Oct 24 '22 05:10

lvthillo